Question

I know I've seen this done before but I can't find the information anywhere. I need to be able to route with .html extensions in the Zend Framework.

I.E. /controller/action.html should route to the appropriate controller / action.

We have an idea to throw away the .html extension with our .htaccess file but I think changing the route config would be the better solution.

Any advice is welcome.

Was it helpful?

Solution

OTHER TIPS

This is the plugin I've used in several applications:

/**
 * Removes .html extension from URI, if present.
 */
class Application_Plugin_RemoveHtmlExtension extends Zend_Controller_Plugin_Abstract
{
    public function routeStartup(Zend_Controller_Request_Abstract $request)
    {
        // remove ".html" from the end of the URI
        $url = preg_replace('#\.html$#i', '', $request->getRequestUri());

        $request->setRequestUri($url);
    }
}

I was trying to do the same for an old application. Here is what worked for me.

$front = Zend_Controller_Front::getInstance();
$router = $front->getRouter();
$router->addRoute('routeHTML', new Zend_Controller_Router_Route_Regex( '([a-z-]+)/([a-z-]+)/([a-z-]+)\.html', array(),
        array(1 => 'module', 2 => 'controller', '3' => 'action') ,
        '%s/%s/%s.html')
);

The default route (without modules) is:

:controller/:action

Which you can remove by:

$router->removeDefaultRoutes();

Then add your version:

:controller/:action.html
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top