简体   繁体   中英

Yii Controller Naming Convention

I have a controller named CategoryAdminController.php which can be accessed by {siteurl}/categoryadmin/index in my localhost which is on a Windows machine. But when I try to launch it in my remote server which is on a Linux platform, it is unable to load the page. But when I rename my controller as CategoryadminController.php (and the class name accordingly), it is working fine.

Is there any way to load the page while keeping the original name in the controller (ie: CategoryAdminController.php )

According to Yii guide, seems it is not possible.

http://www.yiiframework.com/doc/guide/1.1/en/basics.convention

Any ideas?

Thank you

To keep Controller names and Action names CamelCased and working on Linux, you need to add dash between each word in the URL:

"{siteurl}/category-admin/index"  -> CategoryAdminController->indexAction()

"{siteurl}/category-admin/my-way"  -> CategoryAdminControllern->myWayAction()

This will work both on Linux and Windows .

Windows reads files and folders case-insensitive . And often hides some of the mistakes.

The common issue comes when you develop on Windows ... and create a link categoryadmin the OS will find be happy to give you a file like CaTegoRYadmiNcontroller.php ignoring the capitalization.

But when you deploy the same code and files on Linux you will get File Not Found exceptions.
So, better stick to the convention.

It is case-sensitive for a file's name when on a Linux OS, but not on Windows. Did you try to access it: {siteurl}/categoryAdmin/index ?

You can rewrite the URLs by using the urlManager . In your case you can use the following code:

'urlManager' => array(
    'urlFormat' => 'path',
    'showScriptName' => FALSE,
    'rules' => array(
        'categoryadmin/<action:\w+>' => 'categoryAdmin/<action>'
    ),
),

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM