Search code examples
phpurlyiiurl-routing

Yii UrlManager not working


I am having Yii UrlManager issue.

I want to have following url: localhost/mylist/this-is-demo-content

It is working fine by:

'urlManager'=>array(
            'urlFormat'=>'path',
            'showScriptName' => false,
            'rules'=>array(
                '<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
                '<controller:\w+>/<title:\w-]+>' => '<controller>/view',
                '<controller:\w+>/<id1:\w+>'=>'<controller>/view',
                '<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
            ),

But when I try localhost/mylist/this, it says

no action found "this"

Here mylist is controller and I have one action

public function actionView($title)
{
    echo $title;
}

I have also tried this url pattern:

'<controller:\w+>/<title:[A-Z a-z 0-9 _ -]+>' => '<controller>/view',

but could not work


Solution

  • Continue from these simplified rules:

        'rules'=>array(
            // Place custom rules here
            'mylist/<title>' => 'mylist/view',
    
            // Nothing should go below these default rules
            '<controller:\w+>/<id:\d+>' => '<controller>/view',
            '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
            '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
        ),