Yii - Remove 'index' from URL

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

Answers


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

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

Need Your Help

Session is getting overwritten in php

php session

I am facing a problem that my session is getting overwritten by other logged-in user in php.

making a Javascript object inside another object

javascript angularjs

I want to make an object using angular.forEach() function and then I need to push another object and initialize all the values to false but the below way points errors.