I’m using CakePHP 2.2.0 and I need to create one route that get this kind of page:
http://www.example.com/users/mypage.php
Following the cakephp documentation, I have found this page: http://book.cakephp.org/2.0/en/development/routing.html#file-extensions
where I read that I must to use:
Router::parseExtensions('php');
I have added this line in my routes.php file (above the routes), and than I added this route:
Router::connect('/users/mypage.php', array('controller' => 'users', 'action' => 'mypage'));
So, inside UsersController I added this action.
Unfortunately, Only the requests sent to www.example.com/users/mypage work good(the mypage action is called), If I try www.example.com/users/mypage.php I get a 404 not found error.
I really do not understand the reason, As the documentation says:
This will tell the router to remove any matching file extensions, and
then parse what remains.
So, that’s exactly what I need, I have to interpret (only for this action) that the mypage action is called when the user digits /users/mypage.php (with extension).
I did not added anything else. AppController is as default, and my UsersController only has the mypage() method.
I do not know if NGINX is the problem, I write the configuration of the domain below:
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;
}
}
I think the question is clear, How to route the request to a specific Controller’s action if the request has the extension ?
I need:
www.example.com/users/mypage.php ==> to UsersController mypage()
First, you either use parseExtensions() or add “.php” to the url template with connect(). You cannot use both at the same time. Whatever you choose, I suggest an experiment. Try using any other extensions such as “php5” and see that it works just fine. So obviously your problem is your nginx configuration:
Those lines are telling nginx to parse anything in the url that ends in .php as a hard file in your file system. This is not very easy to overcome. You can use try_files in that directive having a fallback to another script, which is tricky, or you could simply just use another extension for your url 🙂
I hope this gives you a good hint of what you have to do.