Yii Framework Forum: Add url rules with modular site - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Add url rules with modular site Rate Topic: -----

#1 User is offline   Nique 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 41
  • Joined: 10-May 09
  • Location:the Netherlands

Posted 14 January 2010 - 10:51 AM

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.
0

#2 User is offline   Sander 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 169
  • Joined: 03-November 09
  • Location:Amsterdam, Netherlands

Posted 14 January 2010 - 11:04 AM

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?
0

#3 User is offline   Nique 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 41
  • Joined: 10-May 09
  • Location:the Netherlands

Posted 14 January 2010 - 11:10 AM

View PostSander, on 14 January 2010 - 11:04 AM, said:

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?
0

#4 User is offline   Nique 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 41
  • Joined: 10-May 09
  • Location:the Netherlands

Posted 14 January 2010 - 12:00 PM

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.
0

#5 User is offline   Nique 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 41
  • Joined: 10-May 09
  • Location:the Netherlands

Posted 14 January 2010 - 08:36 PM

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
0

#6 User is offline   andy_s 

  • Random Member Title
  • Yii
  • Group: Moderators
  • Posts: 1,526
  • Joined: 22-June 09
  • Location:Russia, Kostroma

Posted 15 January 2010 - 02:49 AM

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.
0

#7 User is offline   Nique 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 41
  • Joined: 10-May 09
  • Location:the Netherlands

Posted 15 January 2010 - 07:12 AM

View Postandy_s, on 15 January 2010 - 02:49 AM, said:

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.
0

#8 User is offline   cmx 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 42
  • Joined: 21-November 09
  • Location:California USA

Posted 15 January 2010 - 08:53 AM

View PostNique, on 15 January 2010 - 07:12 AM, said:

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.
0

#9 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,016
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 15 January 2010 - 09:36 AM

View PostNique, on 15 January 2010 - 07:12 AM, said:

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


Seen this thread? It has some examples on what you can do with events:
http://www.yiiframew...d-hooks-system/
0

#10 User is offline   Nique 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 41
  • Joined: 10-May 09
  • Location:the Netherlands

Posted 16 January 2010 - 10:37 PM

Thank you, i'll take a look at it.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users