Yii - How to use different model name from model directory?

687 Views Asked by At

I want to use a specific folder name for my module, but I don't really want it's name shown to the end user (ie: in the URL). I have tried to set the class name, but it tells me it doesn't exist when it is anything OTHER than what it's supposed to be. Please Note: The names have been changed in the example, but it gets the point across.

In my config:

'modules'=>array(
    'cart'
),

In /protected/modules I have a folder ezcart. This is a module created by Gii. I can confirm, when I create "cart" with Gii, it works as expected.

The problem is, source wise I want the name of the folder ezcart to stay the same. It is the name of my package, which I will put on GitHub and share, and use for myself in the future. So anyone in the files know what is what, just like we keep the same names in the "vendors" etc.

So if I create ezcart with Gii, in my config.php, how do I tell Yii to use a different directory name. For example, I want to do this:

'modules'=>array(
    'cart'=>array(
        'class'=>'application.modules.ezcart'
    )
),

However, it tells me it can't find ezcart. How can I get Yii to accept this naming difference?

2

There are 2 best solutions below

4
Manquer On

You should use Url routing for this.. in your config/main.php add like this rule like this

...
'components' => array(
    ...
 'urlManager'=>array(
     'rules'=>array(
       'mynewname/<controller>/<action>' =>'ezcart/<controller>/<action>'
      )
  )
)

see URL Management in the guide for some examples and what is all is possible using routing

0
Wade On

SOLVED!!!

It seems when you try to use a different name in the URL for your module, Yii loses it's brain. I tried to pass $defaultController and $defaultAction, and about 10 other things. I can not figure out how to override the mapping Yii set on my module "ezpaypal" so I can change it's URL to "billing" but yet still point to '/modules/ezpaypal".

First, create a module with Gii. I named mine "ezpaypal". Boom, everything is generated.

Add your module name to config.php:

'modules'=>array(
    'ezpaypal'
),

Problem is, I DO NOT want to see "ezpaypal" in the URL. I want it to be changeable, so it can be "/billing" or "/checkout".. So I need the module to stay named "ezpaypal". I tried and tried with the rules, and ran into tons of problems.

It seems Yii creates the root alias for "ezpaypal" since it is my loaded module. And indeed it works fine if you use it like normal. It knows the controller to use, everything.

When you use the urlManager rules to map "billing" to "ezpaypal" to change the name in the URL, Yii no longer knows which default controller to use. It also doesn't know what default actions to take, like /controller/3 it doesn't know to do /controller/view/3. Yii just went brain dead.

A better solution would be if Yii does have a "hidden" or not so known way to handle this? I am not sure where in the Yii framework it parses the config and loads the modules. Maybe there I can find a function, or variables to set, that will fix this without the lengthy url manager rules. It properly handles the modules url rules for "ezpaypal" without having to add anything to the urlmanager...

A simple fix though, is to write your URL manager rules to handle each and every possible combination. Maybe someone can clean this up into less lines?

'rules'=>array(
            // EZPayPal Friendly URL Support - DO NOT TOUCH!
            '<module:(billing)>'=>'ezpaypal',
            '<module:(billing)>/index'=>'ezpaypal/default/index',
            '<module:(billing)>/<controller:\w+>'=>'ezpaypal/<controller>',
            '<module:(billing)>/<controller:\w+>/<id:\d+>'=>'ezpaypal/<controller>/view',
            '<module:(billing)>/<controller:\w+>/<action:\w+>/<id:\d+>'=>'ezpaypal/<controller>/<action>',
            '<module:(billing)>/<controller:\w+>/<action:\w+>'=>'ezpaypal/<controller>/<action>',
),

WORKS :)

If anyone is doing the same thing, using a different URL path for your module, just change "billing" with the name you want in the URL, and "ezpaypal" with the actual name of your module's directory.

PS: The above rules could possible be simplified? I am not sure. It seems this is the perfect combination. If I remove one, that route breaks. Also, I am still on the hunt for an even easier way so we can override this, without the urlmanager.