CakePHP controller alias

落花浮王杯 提交于 2019-11-30 15:27:36

Ok, with the help of some other people on IRC and stuff like that. I found out the following.

A combination of

Router::connect('/flight/:action/*', array('controller'=>'flights'));
Router::connect('/flight/*', array('controller'=>'flights'));

does the trick. I tried this before, but in a other order, like so:

Router::connect('/flight/*', array('controller'=>'flights'));
Router::connect('/flight/:action/*', array('controller'=>'flights'));

which doesn't work.

So the first 2 lines of code in this post solved it for me. Another guy told me that the solution of Arun Jain isn't a proper solution, because it changes the nameconventions in the core as well. Which will cause problems with the FormsHelper and classes like that. So I think I will prefer the code in this post since this is just an alias instead of a core changing piece of script. Thanks for the help anyway.

To do this with routing the correct approach is as follows.

Router::connect('/flight', array('controller'=>'flights','action'=>'index'));
Router::connect('/flight/:action/*', array('controller'=>'flights'));

This tells the router that when an action is found in the URL to use it, but it no params are found then to default is to use the index action.

I have a slightly different take on all of this. The plural is more often the more accurate way to go with things but in occassions when the plural is just plain wrong, i add an exception into the Inflector class (/lib/Cake/Utility/Inflector).

In your example I would add log to this list of uninflected words. This means that system wide cake will not append the 's'. You'll have your LogController your views would sit in the Log view folder etc...

EDIT

I've come across a much neater way to do this from within app/config/bootstrap.php

Inflector::rules(
    'plural',
    array(
         'uninflected' => array('log')
    )
);

This would add log to the uninflected list without having to alter the files within the Core to allow easier version updating.

You can simply do it using following:

class LogsController extends AppController
{
public $name = 'Log';
..... YOUR REMAINING CODE ......
}

Your router connnect code will remain same. Kindly ask if it not worked for you.

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