0

Zend Route issue.

Normally it works fine.

http://www.example.com/course-details/1/Physics-Newtons-Law

But if I type in an extra slash in the url, the noauthAction of my Error controller gets called.

Example of URL's that are not working.

http://www.example.com/course-details//1/Physics-Newtons-Law
http://www.example.com/course-details/1//Physics-Newtons-Law

Is there something I need to set in the route definition to allow extra slashes?

Routing in application.ini

resources.router.routes.viewcourse.route = "/course-details/:course_id/:title"
resources.router.routes.viewcourse.defaults.controller = course
resources.router.routes.viewcourse.defaults.action = view
resources.router.routes.viewcourse.defaults.title = 
resources.router.routes.viewcourse.reqs.course_id = "\d+"
r4.
  • 358
  • 1
  • 6
  • 22
Gublooo
  • 2,550
  • 8
  • 54
  • 91

1 Answers1

2

You could use a controller plugin to fix common URL typos.

/**
 * Fix common typos in URLs before the request
 * is evaluated against the defined routes.
 */
class YourNamespace_Controller_Plugin_UrlTypoFixer 
    extends Zend_Controller_Plugin_Abstract
{
    public function routeStartup($request)
    {
        // Correct consecutive slashes in the URL.
        $uri = $request->getRequestUri();
        $correctedUri = preg_replace('/\/{2,}/', '/', $uri);
        if ($uri != $correctedUri) {
            $request->setRequestUri($correctedUri);
        }
    }
}

And then register the plugin in your ini file.

resources.frontController.plugins.UrlTypoFixer = "YourNamespace_Controller_Plugin_UrlTypoFixer"
Zach
  • 423
  • 2
  • 4
  • 10
  • Thanks Zach - that fixed it for me. I was going down the wrong path - I was playing around with ReWrite rules of apache to remove double slashes - but this one was quick n easy. Thanks – Gublooo Jun 17 '12 at 18:50
  • Aslo be aware that if you give the router routes via a config file in your boostrapper with $router->addConfig($config, 'routes'); it's not actually adding routes too the already existing ones (defaults) but overriding them.. to really add them make sure you do $router->addDefaultRoutes(); before you do the addConfig – Ponsjuh Jun 18 '12 at 09:35