Add url rules with modular site

Hi,

I want to add url-rules on the fly inside a module. I’ve found a topic about this, but i dont understand the way how he did this.

I’ve added




Yii::app()->urlManager->rules = array_merge(

	Yii::app()->urlManager->rules, array(    

		'/'.Yii::t('newsModule.articles','news') => '/news',

		'/'.Yii::t('newsModule.articles','news').'/<id:\S+>' => '/news/articles'

	));

	Yii::app()->urlManager->init();



inside the init function of the newsModule class

But this doesn’t work properly. When i go to /nieuws (because the target language was set to nl) it should get the /news controller/action; but it doesnt. I get a object not found error.

I think doing this in the module’s init() method is too late, since the rule already needs to be invoked before Yii arrives at a module it can init. Maybe hook it up to the onBeginRequest event?

Ohh of course lol. That was some bad thinking from me, but now you’re pointing me in the right direction. Thank you.

With your hint, I’ve found this post. But how do i do this finaly?

Where do i call that onbeginrequest thing? It needs to be loaded (of course!!) before the news module is loaded, because it cant find the news module as it isn’t set in the url rules. But how do i add ‘automaticly’ these rules to the urlmanager? I dont want to manualy add these rules to the config file every time i (or some newbe who cant script at all!) install a module.

I’m sorry for bumping, but im stuck now for at least 12 hours.

I have no idea how i have to create ‘dynamic’ url rules. All i want is add a UrlRule from a module.

if my target language is dutch (nl) this will call the news module:

site.com/nieuws/124123/hier-een-nieuwsbericht.html

when my target language is english (en) this will call the news module:

site.com/news/124123/here-a-news-article.html

Just based on the ‘target’ language.

But, i want to make my modules completely independend. So this means that i do NOT want to set the url rules of the news module inside the application config file. Thats a bit weird right? So, how do i ‘add’ new urlRules to the existing ones, without having to call the whole module in the complete application.

Do i need to create a urlrules file for each module, inside that particular module? I have no idea where to start. Even the topic i posted above, i dont understand it. Is there some kind of example about this?\

And this brings me to another big question i have. Do i really have to ‘register’ the modules manualy inside the config file? what if i want to make my application ‘modular’ for noobs? What if i want my customer to be able to just drop my created module inside the modules directory, and install it from a ‘backend’ panel. After that, it should just work. I do not want them to open up/copy any php config files by hand…

Thank you

The simplest way I can suggest to you is to create a little function and put it in a config file. This function should scan you modules’ directory and append an existing config array (modules, url rules, imports, …) with values defined in these modules’ config files.

Hi,

I have thought about this, but this doesnt work as the translation of Yii will not work yet (because the modulePath has not yet been ‘set’ as the urlRules are in the same configuration array…).

So a url rule like:


'/'.Yii::t('NewsModule.general', 'news').'/<id:\S+>'

will not work because of that.

I think i need to do something with that onbeginrequest thing, but i dont understand that function as there are no ‘guides’ on this.

Yii uses the Observer pattern to provide an event-driven system similar to the PRADO framework.

The online definitive guide docs and the PDF version have different content. Event components are described in the PDF file. From what I can understand, it appears that a component needs to be created extended from CEvent and that component must be preloaded in the main.php config file. I find this a bit odd since modules should contain all files required to use the module. I would imagine that the Event component could be stored in the module’s components/ directory and preloaded from that directory in main.php.

The onbeginRequest($event) method is at line 166 of CApplication. CApplication processes incoming requests. The $event parameter is a CEvent object type, and CEvent is extended from CComponent. If you read about application life cycles in the definitive guide, onbeginRequest and onEndRequest are breifly mentioned there and this will give you a basic idea of the general application flow as a request is processed.

The onBeginRequest and onEndRequest methods are basically hook points in the application life cycle. A hook point in the life cycle allows additional actions to be performed as part of the normal application life cycle.

Seen this thread? It has some examples on what you can do with events:

http://www.yiiframework.com/forum/index.php?/topic/799-events-and-hooks-system/

Thank you, i’ll take a look at it.