kaygee79
(Kaygee79)
December 2, 2010, 5:00am
1
Is it possible to add a rule in urlManager to route all fist level folder traffic to the same controllers?
I need www.root.com/test1/user/create and www.root.com/test2/user/create to both point to the same controller/action
currently trying:
‘/<slug:[A-Z]+>/<controller:\w+>/<action:\w+>’=>’<controller>/<action>’,
basically it’s almost like I need the urlManager to ignore the first folder, kinda like a wildcard
www.root.com/*/controller/action
is this possible? am I on the right track? Yes I’m a noob here, please be gentle
jacmoe
(Jacob Moena)
December 2, 2010, 5:49am
2
It’s probably easy enough:
Prepend your existing rules with test1 and test2 - and that’s it:
'/test1/<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
'/test2/<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
But, if you want it to be totally variable, you simply need to pass ‘slug’ around as get parameter.
Then what you already wrote (slug) would work (AFAIK).
kaygee79
(Kaygee79)
December 2, 2010, 4:33pm
3
That’s what I thought too, but I get an error:
Error 404
Unable to resolve the request "test2/user/create"
Not sure if it has something to do with the fact that the yii isn’t in the root directory but in its own folder root/yii. Still when I do
‘/yii/<slug:[A-Z]+>/<controller:\w+>/<action:\w+>’=>’<controller>/<action>’,
I still get the same error…
kaygee79
(Kaygee79)
December 2, 2010, 5:06pm
5
andy_s:
test1 won’t match [A-Z]+
Right, /test/ doesn’t work either, case sensitivity is off, however /TEST/ doesn’t work either…
jacmoe
(Jacob Moena)
December 2, 2010, 6:01pm
6
If slug is defined, then it should work.
You need to call your actions with a slug parameter otherwise the UrlManager does not see the route.
kaygee79
(Kaygee79)
December 2, 2010, 6:06pm
7
jacmoe:
If slug is defined, then it should work.
You need to call your actions with a slug parameter otherwise the UrlManager does not see the route.
Can you explain "You need to call your actions with a slug parameter" please? In the url?
jacmoe
(Jacob Moena)
December 2, 2010, 6:34pm
8
Just like:
CHtml::link('Glorious Action', $this->createUrl('do/something',
array('slug' => 'test1', 'id' => $model->id)));
Your actionSomething in the DoController should take a slug parameter.
I believe you should see the right url then.
Can’t hurt to try anyway.
jacmoe
(Jacob Moena)
December 2, 2010, 6:39pm
9
For reference, here’s my UrlManager rules:
'rules' => array(
'/welcome/' => 'site/index',
'/projects/' => 'project/index',
'/projects/<identifier>' => 'project/view',
'/projects/<identifier>/issues' => 'issue/index',
'/projects/<identifier>/issue/<_a:(create)>' => 'issue/<_a>',
'/projects/<identifier>/member/<_a:(create)>' => 'member/<_a>',
'/projects/<identifier>/version/<_a:(create)>' => 'version/<_a>',
'/projects/<identifier>/repository/<_a:(create)>' => 'repository/<_a>',
'/projects/<identifier>/issueCategory/<_a:(create)>' => 'issueCategory/<_a>',
'/projects/<identifier>/issue/<action:\w+>/<id:\d+>' => 'issue/<action>',
'/projects/<identifier>/member/<action:\w+>/<id:\d+>' => 'member/<action>',
'/projects/<identifier>/version/<action:\w+>/<id:\d+>' => 'version/<action>',
'/projects/<identifier>/repository/<action:\w+>/<id:\d+>' => 'repository/<action>',
'/projects/<identifier>/issueCategory/<action:\w+>/<id:\d+>' => 'issueCategory/<action>',
'/projects/<identifier>/<_a:(activity|roadmap|issues|newissue|code|settings)>' => 'project/<_a>',
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
'comment/feed'=>array('comment/feed', 'urlSuffix'=>'.xml', 'caseSensitive'=>false),
'/issues/' => 'issue/index',
),
I pass ‘identifier’ around and that makes it possible to have urls like:
/projects/myproject/issue/view/31, etc.
That should be the same principle as your slug parameter, I think.
I don’t get those nice urls if I don’t pass identifier in the url.
kaygee79
(Kaygee79)
December 2, 2010, 9:37pm
10
Hmm, so I changed the rules to
‘<slug>/<controller:\w+>/<id:\d+>’=>’<controller>/view’,
‘<slug>/<controller:\w+>/<action:\w+>/<id:\d+>’=>’<controller>/<action>’,
‘<slug>/<controller:\w+>/<action:\w+>’=>’<controller>/<action>’,
so now www.root.com/test2/site/index works fine and so does www.root.com/test2/site/contact and all the default pages that Yii initially sets up. However I generated a user model using gii, and so user doesn’t resolve properly. www.root.com/test2/user get a Error 404 Unable to resolve the request “test2/user”… Am I missing something in the UserController?
jacmoe
(Jacob Moena)
December 2, 2010, 9:57pm
11
Try adding these:
'/<slug>/user/<_a:(create)>' => 'user/<_a>',
'/<slug>/user/<action:\w+>/<id:\d+>' => 'user/<action>',
As you can see, I had to do all kinds of hacks to get my routes to work.
I don’t know why Yii couldn’t sort out the catch-all rules, but if that’s the way Yii wants it…
kaygee79
(Kaygee79)
December 3, 2010, 3:36am
12
Thanks jacmoe! That totally works, but now I’m worried about all these rules I have to write, I think I read somewhere that having a lot of rules downgrades performance?
jacmoe
(Jacob Moena)
December 3, 2010, 11:19am
13
A bit, but don’t worry if you don’t have a lot of rules.
I’ve seen much worse.
Maybe you could remove the general case rules. If you know what controllers you’ll use the slug in, you don’t need the catch-all rules.
kaygee79
(Kaygee79)
December 3, 2010, 2:54pm
14
jacmoe:
A bit, but don’t worry if you don’t have a lot of rules.
I’ve seen much worse.
Maybe you could remove the general case rules. If you know what controllers you’ll use the slug in, you don’t need the catch-all rules.
No I don’t, since they aren’t working Thanks for your help again!