CakePHP 控制器别名

2021-12-21 00:00:00 php cakephp cakephp-2.2

我知道关于这个主题还有其他几个主题,但似乎没有一个适合我的需要.

I know there are a couple of other topics about this subject, but non of them seems to fit my needs.

我有什么

  • example.com/log/
  • LogsController.php

我有 LogsController 而不是 LogController(复数),因为 CakePHP 希望你有复数的控制器.

I have LogsController instead of LogController (plural) because CakePHP wants you to have controllers in plural.

但您可能知道/注意到,example.com/log/ 永远不会使用 LogsController,因为网址中缺少s".

But as you might know/notice, example.com/log/ will never use LogsController because of the missing 's' in the url.

现在,我希望将 /log/* 重定向到 /logs/*.使用以下代码可以正常工作:

Now, I want to have /log/* being redirected to /logs/*. Works perfectly fine with the following code:

Router::connect ('/log/*', array('controller'=>'logs'));

但是,当我尝试访问 example.com/log/actions/foo/bar 时,它似乎不起作用.所以经过一些谷歌搜索后,我发现了这个:

But, when I try to access example.com/log/actions/foo/bar it doesn't seem to work. So after some Googeling I found this:

Router::connect ('/log/:action/*', array('controller'=>'logs'));

效果很好.但是现在当我再次尝试访问 example.com/log/时,它说

Works great. But now when I'm trying to access example.com/log/ again, it says

错误:找不到 LogController.

Error: LogController could not be found.

问题所以我的问题是,如何为我的 url 设置别名,以便 /log/ 将使用 LogsController 而不是尝试使用 LogController.

Question So my question is, how do I set up an alias for my url so that /log/ will use LogsController instead of trying to use LogController.

我还有几个控制器想要改变它,比如 flight =>FlightsController, profile =>ProfilesController.

I have a few more Controllers where I'd like to change this, like flight => FlightsController, profile => ProfilesController.

看看这个问题.这是关于相同的主题,但略有不同.它可能会以某种方式帮助您.

Have a look at this question. It is about the same subject, but slightly different. It might help you in some way.

推荐答案

好的,在 IRC 上其他一些人的帮助下,诸如此类.我发现了以下内容.

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

组合

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

这不起作用.

所以这篇文章的前两行代码为我解决了这个问题.另一个人告诉我 Arun Jain 的解决方案不是一个合适的解决方案,因为它也改变了核心中的名称约定.这会导致 FormsHelper 和类似的类出现问题.所以我想我会更喜欢这篇文章中的代码,因为这只是一个别名,而不是一个核心更改脚本.无论如何,感谢您的帮助.

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.

相关文章