問題描述
可能的重復:
如何從 cakephp 中的 url 中刪除動作名稱?
我正在使用 cakephp 在 Cakephp 中開發我的網站,我想從應用程序 URL 中刪除操作(或視圖)名稱,我應該怎么做.我的要求是最近我想添加參數來代替視圖名稱,我的 URL 如下所示:
I am using cakephp to develop my website in Cakephp and I would like to remove action(or view )name from the application URL what should I do fro that. My requirement is I want to add the parameters in place of view name recently my URL is like:
域名/控制器名/視圖名/param1/param2"
"domainname/controllername/viewname/param1/param2"
但我需要
域名/控制器名/param1/param2"
"Domainname/controllername/param1/param2"
我的 .htaccess 文件如下
My .htaccess files are like below
根文件夾中的.htaccess
.htaccess in root folder
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ [L]
RewriteRule (.*) app/webroot/index.php/$1 [L]
</IfModule>
.htaccess 在應用程序文件夾中
.htaccess in app folder
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /liberty_new/app/
RewriteRule ^$ webroot/index.php/ [L]
RewriteRule (.*) webroot/index.php/$1 [L]
</IfModule>
.htaccess 在 webroot 文件夾中
.htaccess in webroot folder
<IfModule mod_rewrite.c>
RewriteEngine ON
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /$1 [QSA,L]
</IfModule>
推薦答案
您在 Config
outes.php
文件中執行此操作.設置 Router
參數以根據需要編寫 URL.不要編輯您的 htaccess 文件,否則它會/會破壞 CakePHP 路由.
You do this in your Config
outes.php
file. Set the Router
params to write the URL as you need. Do not edit your htaccess files or it can/will break CakePHP routing.
Router::connect('/:controller/:param1/:param2', array('action' => 'index'), array('param1' => '[a-zA-Z0-9]+', 'param2' => '[a-zA-Z0-9]+'));
這應該將所有請求發送到任何控制器到該控制器的索引函數并傳遞參數 1 和 2.當然,這可以高度定制.我強烈建議您閱讀文檔中有關路由的內容,除非必須,否則不要更改 htaccess.
This should send all requestes to any controller to the index function of that controller and pass params 1 and 2. Of course, this can be heavily customized. I would strongly suggest you read about routing in the documentation and never alter htaccess unless you have to.
http://book.cakephp.org/2.0/en/development/routing.html#routes-configuration
這篇關于從cakephp中的Url中刪除動作名稱?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!