Yii - Remove 'index' from URL

2019-09-15 01:47发布

How do I remove index from http://localhost/dashboard/index/create

I have my urlManager setup like this:

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

And this gives me clean URLs like this http://localhost/dashboard where dashboard is a module with a default controller named indexController.

Now, the problem I am having is that I cannot access other actions of the module controller without first putting index e.g If I want to call actionCreate inside indexController, I always have to say http://localhost/dashboard/index/create.

Is there any way I can get rid of index and just call it like this: http://localhost/dashboard/create?

This is how my module is configured in main.php :

'modules' => array(
    ...
    'dashboard' => array(
        'defaultController' => 'index'
    ),
    ...
),

Thank you in advance :-)

标签: php url seo yii
2条回答
别忘想泡老子
2楼-- · 2019-09-15 02:24

Try arranging your array like this (put the line beginning with dashboard under your urlManager component):

array(
  ...
  'components' => array(
    ...
    'urlManager' => array(
      ...
        'rules' => array(
          'dashboard/<action:\w+>' => 'dashboard/index/<action>'
      ),
    ),
  ),
);
查看更多
欢心
3楼-- · 2019-09-15 02:26

You can create .htaccess file for that:

RewriteEngine on
RewriteBase /project_folder_NAME
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# otherwise forward it to index.php
RewriteRule . index.php
查看更多
登录 后发表回答