Zend Framework Routing: .html extension

2019-03-26 01:54发布

问题:

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.

回答1:

A quick search on google yielded the following tutorials:

Extending Zend Framework Route and Router for custom routing
Routing and complex URLs in Zend Framework



回答2:

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);
    }
}


回答3:

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')
);


回答4:

The default route (without modules) is:

:controller/:action

Which you can remove by:

$router->removeDefaultRoutes();

Then add your version:

:controller/:action.html