Nuthan Raghunath
Nuthan Raghunath

Reputation: 111

Yii2 -How to remove Module name from url with same module and controller name?

I am new to Yii2. I am getting used to it slowly. I am using a module to build customer data. I have a customer table. My module name is Customer. My controller name is Customer. Finally my url structure after enabling pretty url is as below.

http://localhost/basic/web/customers/customers
http://localhost/basic/web/customers/customers/create

etc

How do i remove the module name(customer) from the url? I want to make something like this.

 http://localhost/basic/web/customers/
 http://localhost/basic/web/customers/create

My folder structure

Modules

----Customers

------controllers

-----------DefaultController

-----------CustomersController

------Models

-----------Customers

------Views

-----------customers

--------------create

--------------update

...

----Module.php

I dont know how to insert rules in urlManager even after following this: Yii - Hiding module name in URL

'urlManager' => [
        'enablePrettyUrl' => true,
        'showScriptName' => false,           
        'rules' => array(

            '<controller:\w+>/<action:\w+>'=>'module/controller/action',

    ),

Upvotes: 1

Views: 1756

Answers (1)

Sageth
Sageth

Reputation: 1112

You have all you need is this and take care, you are looking a Yii1 question.

If your module name is called like your controller try that:

'urlManager' => [
    'enablePrettyUrl' => true,
    'showScriptName' => false,           
    'rules' =>[
        '<controller:\w+>/<action:\w+>'=>'controller/controller/action',
    ],

We want the format to be used in the Url is controller/action.

With <controller:\w+> we "declare" a "variable" called controller with cointains words, and the same with <action:\w+>.

The internally Yii will call <controller>/<controller>/<action>

Remember controller and action are "variables"

If you want call always the same module you will use:

'rules' =>[
        '<controller:\w+>/<action:\w+>'=>'customers/controller/action',
    ],

Upvotes: 0

Related Questions