問題描述
我知道關(guān)于這個(gè)主題還有其他幾個(gè)主題,但似乎沒有一個(gè)適合我的需要.
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
(復(fù)數(shù)),因?yàn)?CakePHP 希望你有復(fù)數(shù)的控制器.
I have LogsController
instead of LogController
(plural) because CakePHP wants you to have controllers in plural.
但您可能知道/注意到,example.com/log/
永遠(yuǎn)不會(huì)使用 LogsController
,因?yàn)榫W(wǎng)址中缺少s".
But as you might know/notice, example.com/log/
will never use LogsController
because of the missing 's' in the url.
現(xiàn)在,我希望將 /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'));
但是,當(dāng)我嘗試訪問 example.com/log/actions/foo/bar
時(shí),它似乎不起作用.所以經(jīng)過一些谷歌搜索后,我發(fā)現(xiàn)了這個(gè):
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'));
效果很好.但是現(xiàn)在當(dāng)我再次嘗試訪問 example.com/log/時(shí),它說
Works great. But now when I'm trying to access example.com/log/ again, it says
錯(cuò)誤:找不到 LogController.
Error: LogController could not be found.
問題所以我的問題是,如何為我的 url 設(shè)置別名,以便 /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
.
我還有幾個(gè)控制器想要改變它,比如 flight =>FlightsController
, profile =>ProfilesController
.
I have a few more Controllers where I'd like to change this, like flight => FlightsController
, profile => ProfilesController
.
看看這個(gè)問題.這是關(guān)于相同的主題,但略有不同.它可能會(huì)以某種方式幫助您.
Have a look at this question. It is about the same subject, but slightly different. It might help you in some way.
推薦答案
好的,在 IRC 上其他一些人的幫助下,諸如此類.我發(fā)現(xiàn)了以下內(nèi)容.
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'));
可以解決問題.我以前試過這個(gè),但按其他順序,像這樣:
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'));
這不起作用.
所以這篇文章的前兩行代碼為我解決了這個(gè)問題.另一個(gè)人告訴我 Arun Jain 的解決方案不是一個(gè)合適的解決方案,因?yàn)樗哺淖兞撕诵闹械拿Q約定.這會(huì)導(dǎo)致 FormsHelper 和類似的類出現(xiàn)問題.所以我想我會(huì)更喜歡這篇文章中的代碼,因?yàn)檫@只是一個(gè)別名,而不是一個(gè)核心更改腳本.無(wú)論如何,感謝您的幫助.
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.
這篇關(guān)于CakePHP 控制器別名的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!