CakePHP控制器别名 [英] CakePHP controller alias

查看:129
本文介绍了CakePHP控制器别名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我拥有的 >




  • example.com/log /

  • LogsController.php



我有 LogsController 而不是 LogController )因为CakePHP希望你有多个控制器。



但是你可能知道/注意, example.com/log / 永远不会使用 LogsController ,因为在网址中缺少's'。



/ log / * 重定向到 / logs / * 。使用以下代码可以很好地工作:

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

但是,当我尝试访问 example.com/log/actions/ foo / bar 它似乎不工作。所以在一些Googeling后,我发现这个:

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

效果非常好。但现在当我尝试再次访问example.com/log/时,表示


错误:找不到LogController。 / p>

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



我有几个控制器,我想更改这个,例如 flight => FlightsController profile => ProfilesController






请查看此问题。它是大约相同的主题,但略有不同。

解决方案

好的,在IRC上的其他人的帮助下,这样的东西。我发现了以下。



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

我试过这个,但在其他顺序,像这样:

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

无效。



所以前两行代码在这个帖子解决了我。另一个家伙告诉我,Arun Jain的解决方案不是一个合适的解决方案,因为它改变核心中的nameconventions以及。这将导致FormsHelper和类像这样的问题。所以我想我更喜欢这篇文章中的代码,因为这只是一个别名,而不是一个核心更改脚本。非常感谢您的帮助。


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

What I have

  • example.com/log/
  • LogsController.php

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

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

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

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

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

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

Error: LogController could not be found.

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.

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.

解决方案

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.

这篇关于CakePHP控制器别名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆