Yii - Remove 'index' from URL

与世无争的帅哥 提交于 2019-12-12 06:24:15

问题


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 :-)


回答1:


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>'
      ),
    ),
  ),
);



回答2:


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


来源:https://stackoverflow.com/questions/11530174/yii-remove-index-from-url

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!