Reputation: 1639
Suppose my yii site base domain is http://ii.local
.
All controller actions can show on on base domain except
I need http://events.ii.local
to process all actions of controller event
With the call $this->createUrl('/event/index',array('code' => 'guruevent'))
I need the url generated as http://events.ii.local/guruevent
With the call $this->createUrl('/site/login')
I need the url generated as http://ii.local/site/login
.
'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName'=>false,
'caseSensitive'=>false,
'rules'=>array(
'http://events.ii.local/<code:\w+>'
=> '<controller:event>/<action:index>',
'http://events.ii.local/<code:\w+>/<action:\w+>'
=> '<controller:event>/<action>/<code>',
'<controller:\w+>/<action:\w+>'
=> '<controller>/<action>',
),
),
Urls for event subdomain are getting generated using /event/index?code=guruevent
and not using events.ii.local subdomain.
Single domain routing is working fine but I need to use few subdomains for few controllers and modules. I also was trying to set request->baseUrl but as soon as I set it to http://ii.local/
all routing stops and the same homepage opens for all urls.
Please suggest the fix.
Upvotes: 1
Views: 2404
Reputation: 5187
Swap your first and second rules. The rules are processed in order, and the first to match is used. The UrlManager does not look for best matches, but rather goes first come first serve.
Since
$this->createUrl('/event/index',array('code' => 'guruevent'))
matches the pattern provided by the first rule, it will use that rule, regardless of the 'extra' data of the code
being present.
A good rule of thumb is to always put the most specific rules first, to make sure they match, and then finish up with general rules to catch any that fail the specifics.
Upvotes: 1
Reputation: 8597
You could use a custom function to create your URLs. For example url($route, $params=array())
. You'd use Yii::app()->createUrl()
inside and depending on $route
would prepend it with your other hostname. Then in your project always use this function to create URLs.
If you feel confident you can also enhance your implementation and add more arguments to that function like $schema=''
or $absolute=false
. This way you could use this function for all kind of URL creation you will ever need in your project.
An alternative would be to write your custom urlManager
component which extends from CUrlManager
and then override createUrl()
. There you could do the same as suggested above: Inspect the $route
and create the apropriate URL.
Upvotes: 0