GSTAR
(Omzy83)
February 22, 2010, 3:30pm
1
In my CMenu widget I have the following:
array('label'=>'Home', 'url'=>array('/site/index')),
array('label'=>'About', 'url'=>array('/site/page', 'view'=>'about')),
array('label'=>'Events', 'url'=>array('/events/index')),
I currently use the following rules to take out the ‘site’ portion from the ‘Home’ link:
'rules'=>array(
'index'=>'site/index',
),
I want these links to be displayed as follows:
Home - /webapp/
About - /webapp/about/
Events - webapp/events/
So basically I want to take out the ‘index’ portion from the ‘Home’ and ‘Events’ links and take out the ‘page’ and ‘view’ portions from the About link.
jayrulez
(Waprave)
February 22, 2010, 3:37pm
2
GSTAR
(Omzy83)
February 22, 2010, 3:54pm
3
Thanks jayrulez
I’ve already looked at that guide, can’t find anything that helps.
I could obviously hardcode the links for the ‘url’ attribute in CMenu but then I lose the active highlighting.
The problem is I have specified a urlSuffix, so the Events link for example currently appears as /webapp/events.html
It would be a lot better if it was /webapp/events/ and then sub-pages can be /webapp/events/id/100
mdomba
(Maurizio Domba Cerin)
February 25, 2010, 11:43am
5
You should first consider every different URL you will need and than decide for he URL format…
for your example /webapp/events/id/100 => is that events/update or events/view…
For the first post:
'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName'=>false,
'urlSuffix'=>'',
'rules'=>array(
'/'=>'site/index',
'<view:(about)>'=>'site/page',
'<_c:(Events)>'=>'<_c>\index',
)
),
so urlFormat is path and .htaccess should be set too (you already should have done that)
urlSuffix - if you want a suffix like “.html” write here… if you don’t want it, delete this line
‘/’ => ‘site/index’ - translates root of webapp to site/index
‘<view:(about)>’=>‘site/page’ - translates to site/page?view=about, if you have more static pages just add them in the parentesis, eg. if you have static pages info and sitemap you would put : ‘<view:(about|info|sitemap)>’=>‘site/page’
‘<_c:(Events)>’=>’<_c>\index’ (not tested) - translates Events to events/index, same as above if you have more models like User or Post you can add them here like this
‘<_c:(Events|User|Model)>’=>’<_c>\index’
GSTAR
(Omzy83)
March 3, 2010, 12:47pm
6
Thanks mdomba. The first two solutions you posted above work perfectly - thanks!
The last solution does not seem to work. Basically what I have is the following:
Menu:
array('label'=>'Events', 'url'=>array('/events/index')),
Config:
'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName'=>false,
'rules'=>array(
'events' => 'events/index',
),
'urlSuffix'=>'.html',
),
What I want to acheive:
Main Menu Events page link: /webapp/events/ (this is currently displaying as /webapp/events.html)
Events page sub links: /webapp/events/view/id/1.html (this is currently displaying OK)
GSTAR
(Omzy83)
March 3, 2010, 3:40pm
7
Anyone able to advise regarding my above query? Thanks!
andy_s
(Arekandrei)
March 3, 2010, 3:45pm
8
You can set urlSuffix for a particular rule like that:
'blablabla' => array(..., 'urlSuffix'=>'.html')
or don’t set it…
GSTAR
(Omzy83)
March 3, 2010, 4:04pm
9
Fantastic! Man I love Yii, it is so flexible!
I was struggling to get the about page to work. I’ve fixed it, but thought I would post the reason.
My rules read:
'rules'=>array(
'/' => 'site/index',
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
'<view:(about)>' => 'site/page',
),
but the view needs to be before the controller section. I presume the controller is overriding it.
'rules'=>array(
'/' => 'site/index',
'<view:(about)>' => 'site/page',
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
),
story
(Petestory)
April 22, 2011, 4:51pm
11
SystemicPlural:
I was struggling to get the about page to work. I’ve fixed it, but thought I would post the reason.
My rules read:
'rules'=>array(
'/' => 'site/index',
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
'<view:(about)>' => 'site/page',
),
but the view needs to be before the controller section. I presume the controller is overriding it.
'rules'=>array(
'/' => 'site/index',
'<view:(about)>' => 'site/page',
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
),
Yii applies the rules in order, so if you apply the controller rule first, the view rule no longer matches.