views:

292

answers:

6

In Zend framework, using the MVC, if A user surf explicitly to http://base/url/index.php instead of just http://base/url, The system thinks the real base url is http://base/url/index.php/ and according to that calculates all the URLs in the system.

So, if I have a controller XXX and action YYY The link will be
http://base/url/index.php/XXX/YYY which is of course wrong.

I am currently solving this by adding a line at index.php:

$_SERVER["REQUEST_URI"]=str_replace('index.php','',$_SERVER["REQUEST_URI"]);

I am wondering if there is a built-in way in ZF to solve this.

+5  A: 

You can do it with ZF by using Zend_Controller_Router_Route_Static (phew!), example:

Read the manual page linked above, there are some pretty good examples to be found.

$route = new Zend_Controller_Router_Route_Static(
    'index.php',
    array('controller' => 'index', 'action' => 'index')
);
$router->addRoute('index', $route);

Can't say I totally disagree with your approach. That said, others may well point out 5000 or so disadvantages. Good luck with that.

karim79
+2  A: 

Well it really depends on how you want to solve this. As you know the Zend Frameworks build on the front controller pattern, where each request that does not explicitly reference a file in the /public directory is redirected to index.php. So you could basically solve this in a number of ways:

  1. Edit the .htaccess file (or server configuration directive) to rewrite the request to the desired request:
    • RewriteRule (.*index.php) /error/forbidden?req=$1 // Rewrite to the forbidden action of the error controller.
    • RewriteRule index.php /index // Rewrite the request to the main controller and action
  2. Add a static route in your bootstrapper as suggested by karim79.
PatrikAkerstrand
A: 

I don't think you should use a route to do this.

It's kind of a generic problem which shouldn't be solved by this way.

You better should have to do it in your .htaccess, which will offer you a better & easier way to redirect the user to where you want, like to an error page, or to the index.

Here is the documentation page for the mod_rewrite

Boris Guéry
A: 

I've never faced this problem using Zend Framework. just do not link to index.php file. that's it. and when your are giving your application's address to users, just tell them to go to http://base/url/

when the user enters http://base/url/ her request URI is base/url and your .htaccess file routs the request to index.php, but the request IS base/url. you do not need to remove 'index.php' from the request. because it is not there.

when you are trying to generate URLs for links and forms and ..., use the built-in url() view helper to generate your links. like this:

// in some view script
<a href="<?php
  echo $this->url( array('controller'=>'targetController','action'=>'targetAction') );
         ?>" >click</a>

do not worry about the link. Zend will generate a URL for you.

farzad
You are 1000% right, the problem is that users, I discovered, tend some times to add index.php to the url. This is how I learned about this issue.
Itay Moav
A: 

The way I look at this is that if I have a website powered by PHP and a user goes to http://site/index.aspx then I would send a 404.

Even though index.php does exist in theory, it's not a valid URL in my application so I would send a 404 in this case too.

noginn
Won't it be more friendly to "redirect" him to the real URL?
Itay Moav
+1  A: 

Use mod_rewrite. Something like this should do it:

RewriteRule ^index.php/(.*)$ /$1 [r=301,L]
Rob Allen