- Yii 1.1 Application Development Cookbook
- Alexander Makarov
- 475字
- 2021-04-02 18:41:01
Providing your own URL rules at runtime
When you are developing an application with pluggable module architecture, you most likely need to somehow inject your module-specific rules into an existing application.
Getting ready
- Set up a new application using
yiic webapp
. - Add
.htaccess
, shown in official URL Management guide to yourwebroot
. - Add
'showScriptName' => false
to your URL manager configuration. - Generate the
page
module using Gii. - Don't forget to add your new module to the modules list in your application configuration.
The Yii code generator is shown in the following screenshot:

How to do it...
- Create
ModuleUrlManager.php
in yourprotected/components
directory with the following code inside:<?php class ModuleUrlManager { static function collectRules() { if(!empty(Yii::app()->modules)) { foreach(Yii::app()->modules as $moduleName => $config) { $module = Yii::app()->getModule($moduleName); if(!empty($module->urlRules)) { Yii::app()->getUrlManager()->addRules($module->urlRules); } } } return true; } }
- In your application configuration, add the following line:
'onBeginRequest' => array('ModuleUrlManager', 'collectRules'),
- Now, in your page module, you can add custom rules. To do so, open
PageModule.php
and add:public $urlRules = array( 'test' => 'page/default/index', );
- To test if it works, open your browser and go to
http://example.com/test
. This page should look like the one shown in the following screenshot:This is the view content for action "index". The action belongs to the controller "DefaultController" in the "page" module.
- You still can override URL rules from your main application configuration file. So, what you specify in module's
urlRules
is used only when the main application rules are not matching.
How it works...
Let's review the ModuleUrlManager::collectRules
method.
If there are modules defined in our application, then we are checking if urlRules
public property exists. If it does, then there are some rules defined in the module and they are added using CUrlManager::addRules
.
CUrlManager::addRules
description says "In order to make the new rules effective, this method must be called before CWebApplication::processRequest
".
Now, let's check how our application works. In our index.php
, we have the following line:
Yii::createWebApplication($config)->run();
After being initialized with configuration, we are calling CWebApplication::run()
:
public function run() { if($this->hasEventHandler('onBeginRequest')) $this->onBeginRequest(new CEvent($this)); $this->processRequest(); if($this->hasEventHandler('onEndRequest')) $this->onEndRequest(new CEvent($this)); }
As we can see, there is an onBeginRequest
event raised just before calling processRequest
. That is why we are attaching our class method to it.
There's more...
As instantiating all application modules on every request is not good for performance, it is good to cache module rules. Caching strategy can vary depending on your application. Let's implement a simple one:
<?php class ModuleUrlManager { static function collectRules(){ if(!empty(Yii::app()->modules)) { $cache = Yii::app()->getCache(); foreach(Yii::app()->modules as $moduleName => $config) { $urlRules = false; if($cache) $urlRules = $cache->get('module.urls.'.$moduleName); if($urlRules===false){ $urlRules = array(); $module = Yii::app()->getModule($moduleName); if(isset($module->urlRules)) $urlRules = $module->urlRules; if($cache) $cache->set('module.urls.'.$moduleName, $urlRules); } if(!empty($urlRules)) Yii::app()->getUrlManager()->addRules($urlRules); } } return true; } }
This implementation caches URL rules per module. So, adding new modules is not a problem but changing existing ones requires you to flush cache manually using Yii::app()->cache->flush()
.
See also
- The recipe named Configuring URL rules in this chapter
- AutoCAD 2010中文版基礎教程(第2版)
- 中文版After Effects 2021入門教程
- SOLIDWORKS Visualize 實例詳解(微視頻版)
- Android User Interface Development: Beginner's Guide
- 中文版3ds Max 2021完全自學教程
- 中文版CorelDRAW X6基礎培訓教程(第2版)
- NetLogo多主體建模入門
- Capture One 22 Pro高級實戰(zhàn)教程
- 中文版Photoshop CS6全能修煉圣經(移動學習版)
- 三維建模與3D打印從入門到精通
- Instant Flask Web Development
- Origin科技繪圖與數據分析
- Photoshop+CorelDRAW平面設計案例實戰(zhàn):從入門到精通(視頻自學全彩版)
- PPT設計與制作實戰(zhàn)教程
- 金融精英PPT實操手冊:世界知名公司這樣展示研究報告