2012-07-17 50 views
1

我使用CakePHP 2.2.0,我需要創建一個路由得到這樣的頁面:如何路由與擴展到特定的控制器的動作的CakePHP請求2.2.0

http://www.example.com/users/mypage.php 

繼CakePHP的文件,我已經找到了這個網頁:http://book.cakephp.org/2.0/en/development/routing.html#file-extensions

在那裏我閱讀,我必須使用:

Router::parseExtensions('php'); 

我已經加入我的routes.php文件文件這一行(路線以上),比我添加了這條路線:

Router::connect('/users/mypage.php', array('controller' => 'users', 'action' => 'mypage')); 

所以,在UsersController裏面我加了這個動作。

遺憾的是,只有送到www.example.com/users/mypage工作良好(我的空間動作被調用)的要求,如果我嘗試www.example.com/users/mypage.php我得到404未找到錯誤

我真的不明白其中的道理,正如文件說:

這將告訴路由器,以消除任何匹配的文件擴展名,並 然後解析剩下。

所以,這正是我需要的,我必須解釋(僅此動作)的我的空間行動被稱爲當用戶數字/users/mypage.php(擴展)。

我沒有添加任何東西。 AppController是默認的,我的UsersController只有mypage()方法。

我不知道,如果NGINX的問題,我下面寫的域的配置:

server { 
     listen 80; 
     server_name www.example.com; 
     root /home/users/example.com/www/app/webroot/; 

     access_log /home/users/example.com/log/access.log; 
     error_log /home/users/example.com/log/error.log; 

     location/{ 
       index index.php index.html; 
       try_files $uri $uri/ /index.php?$uri&$args; 
     } 

     location ~* \.php$ { 
       fastcgi_pass 127.0.0.1:9000; 
       fastcgi_index index.php; 
       include   /etc/nginx/fastcgi_params; 
       fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
     } 
} 

我覺得這個問題是清楚的,如何將請求路由到如果請求特定的控制器的動作有擴展名嗎?

我需要:

www.example.com/users/mypage.php ==> to UsersController mypage() 

回答

2

首先,你要麼使用parseExtensions()或添加 「.PHP」 網址模板與連接()。你不能同時使用兩者。無論您選擇什麼,我都會提出一個實驗。嘗試使用任何其他擴展名,如「php5」,並看到它工作得很好。所以,很顯然你的問題是你的nginx的配置:

location ~* \.php$ { 
    ... 
} 

,這些行告訴nginx的在以.php爲您的文件系統中的硬盤文件結束URL解析什麼。這不是很容易克服。您可以在該指令中使用try_files,使其退回到另一個腳本,這很棘手,或者您可以簡單地爲您的url使用另一個擴展名:)

我希望這可以給您一個很好的暗示。

+0

謝謝你,但是,我嘗試添加.PHP的連接(和刪除parseExtensions()),並做反之亦然,我總是得到404沒有找到,所以你告訴我,NGINX應該是問題...你有什麼建議?我在那個盒子裏重複try_files? – Dail 2012-07-17 21:01:09

0

這是我的nginx.conf for cakephp 2.2。1:

server { 
    listen  80; 
    server_name localhost; 
    error_log /var/log/nginx/errordebug.log debug; 

    location/{ 
     index index.php; 
     try_files $uri $uri/ @cakephp; 
     expires max; 
     access_log off; 
    } 
    location @cakephp { 
     fastcgi_param SCRIPT_NAME /index.php; 
     include /etc/nginx/fastcgi.conf; 
     fastcgi_pass 127.0.0.1:9000; 
     fastcgi_param QUERY_STRING url=$request_uri; #&$args; 
     fastcgi_param SCRIPT_FILENAME $document_root/index.php; 
    } 

    location ~* \favicon.ico$ { 
     access_log off; 
     expires 1d; 
     add_header Cache-Control public; 
    } 
相關問題