createUrl for index pages returning 'xxx/index'

Starting with Yii 1.1.1 (I think), the URL manager seems to generate URLs not quite as pretty as before.




CHtml::link('Dashboard', array('dashboard/index'))



This results in a URL like ‘/dashboard/index’ - I want simply ‘/dashboard’, how come the ‘index’ part is tagged on there now?




CHtml::link('Home', array('site/index'))



This results in a URL like ‘/site/index’, rather than simply ‘/’.

This worked before - what changed? Am I doing something wrong?

Thanks

Nothing is changed…it all depends on your url rules.

All I have in my routing table is a few custom rules - do I need a particular set of rules or something to get these to work normally again?

I looked at previous projects that run on older versions of Yii, and I don’t see anything particular in the routing table, just a few custom rules…

For have just /dashboard, you can simpy write redirect(array(’/dashboard’)).

It will generate the correct url and will be invoked the corrected action.

Do you mean that in some previous revision Yii were automathically generating an index "/dashboard" by passing him "/dashboard/index"?

Yes, precisely! What happened to that?

Could you share your rules with us?

BTW, If you prefix ‘/’ in the url and then change the rules it will change according to the new rules. So if you had a rule:




'dashboard' => 'site/dashboard/index'



And you created a rule using:




$this->createUrl('/dashboard');



Then it will redirect to ‘site/dashboard/index’ then when you change the rule from the above to something like




'dash' => 'site/dashboard/index'



if you used to create the rules like the above it will still generate ‘dashboard’ instead of the new ‘dash’ rule. So you need to create urls according to their paths, So when changing them they will automatically change in all places you used them.

Thank you, Vince - here’s my url manager config:




    'urlManager'=>array(

      'showScriptName' => false, // do not show "index.php" in URLs

      'urlFormat' => 'path',     // use "path" format, e.g. "post/show/id/4" rather than "r=post/show&id=4"

      'rules'=>array(

        'reports' => 'reports/index',

        'reports/<id:\d+>' => 'reports/show',

        'images/cached/<folder:\d+>/<attachment:\d+>-<hash:\w+>-<transform:\w+>.jpg' => 'image/cache',

        '<_c:\w+>/<_a:\w+>/<id:\d+>' => '<_c>/<_a>',

      ),

    ),



The ‘reports’ rule is a work-around - without it, I get ‘reports/index’ when I create the URL for controller/action ‘reports/index’.

You didn’t used to need this workaround in previous versions of Yii, as far as I know?

Perhaps there would be some way to write a general rule to emulate this behavior? Like maybe…




'<_c:\w+>' => '<_c>/index'



But that would work only for controllers where the default action is actually named "actionIndex", which is not always the case.

Ideas?

If the action is not index then you would want it in the url right?

Just a note: From a quick look i saw that you should change the order of your reports, as the first matching rule will be used. So the order should be from more specific to more general:


'rules'=>array(

        'reports/<id:\d+>' => 'reports/show',

        'reports' => 'reports/index',

        'images/cached/<folder:\d+>/<attachment:\d+>-<hash:\w+>-<transform:\w+>.jpg' => 'image/cache',

        '<_c:\w+>/<_a:\w+>/<id:\d+>' => '<_c>/<_a>',

      ),



try adding this:




'urlmanager' =>array(

	...

	'showScriptName' => false,

	...

),



Look again, I’ve already got that :wink:

I added the following rule near the end of my list though:




'<_c:\w+>' => '<_c>/index',



This works in 90% of all cases, e.g. any controller where the default action is named ‘index’ - it’ll do :slight_smile:

… ok … I have my eyes open now … I always work quite late on my Yii projects…

I remember I had a similar issue when I just started working with Yii (V1.011 at the time).

I solved it by adding some basic rewrite rules to my .htaccess file.

Could that be the case with you?

Here’s what I’ve got in my .htaccess:




RewriteEngine on

RewriteCond %{REQUEST_FILENAME} -s [OR]

RewriteCond %{REQUEST_FILENAME} -l [OR]

RewriteCond %{REQUEST_FILENAME} -d

RewriteRule ^.*$ - [NC,L]

RewriteRule ^.*$ index.php [NC,L]



I have a similar rewrite in place - without it, you get URLs like "index.php?r=site/index".

I’m satisfied with this solution for now.

At some point I’m going to switch to my own URL resolver anyway, as soon as I get around to finishing it… :slight_smile: