Navaneetha Krishnan K
Navaneetha Krishnan K

Reputation: 237

Url Manager hide controller name and function name in YII

I'm new to the YII framework. I have to hide the default controller name and function name. For example:

(Existing URL : http://localhost/food/store/home )

(Required URL : http://localhost/food/ )

In the YII framework config page, they have declared the urlManager as:

URL Manage in config page :

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

Default Controller Declaration :

'defaultController'=>'store'

Upvotes: 0

Views: 622

Answers (2)

user4197709
user4197709

Reputation:

use alias will slove the problem
'urlManager' => array( 'urlFormat' => 'path', 'rules' => array( 'admin' => '/admin/default', 'mobile' => '/mobile/default', 'mobile/<alias:fees|aboutus|contactus|terms|policy|faq|aml|legal|news|testimonial>' => 'mobile/default/<alias>', '<alias:fees|about|contactus|terms|privacypolicy|faq|aml|legal>' => 'site/<alias>', '<module:\w+>/<controller:\w+>/<action:\w+>' => '<module>/<controller>/<action>', '<controller:\w+>/<id:\d+>' => '<controller>/view', '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>', '<controller:\w+>/<action:\w+>' => '<controller>/<action>', ), 'showScriptName' => false, ),

Upvotes: 0

marche
marche

Reputation: 1756

You can create specific rules without using placeholders like <controller> or <action>, you just need to make sure you add them before the rules for the general cases.

'urlManager' => [
    'urlFormat' => 'path',
    'showScriptName' => false,
    'rules' => [
        //Add the rules for the specific cases
        '' => 'store/home',

        // The general case rules go after the specific cases
        '<controller:\w+>/<id:\d+>' => '<controller>/view',
        '<controller:\w+>' => '<controller>/index',
        '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
        '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
    ],
],

Upvotes: 1

Related Questions