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.
Routing is the act of matching a request to a given controller. Typically, routing will examine the request URI, and attempt to match the URI path segment against provided constraints. If the constraints match, a set of “matches” are returned, one of which should be the controller name to execute.
In Zend Framework 2, the controller is a class that is generally called {Controller name}Controller. Note that {Controller name} must start with a capital letter. This class lives in a file called {Controller name}Controller. php within the Controller directory for the module.
When it comes to PHP frameworks, Zend is counted among the best. Zend Framework offers lots of benefits for creating feature-rich and dynamic web solutions. MVC features and a strong component library have made Zend a popular PHP framework for creating a myriad of web solutions.
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);
}
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With