Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zend Framework Routing: .html extension

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.

like image 386
smack0007 Avatar asked Jan 23 '09 11:01

smack0007


People also ask

What is routing in Zend Framework?

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.

What is controller in Zend Framework?

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.

Is Zend Framework Good?

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.


2 Answers

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);
    }
}
like image 91
nevvermind Avatar answered Nov 07 '22 20:11

nevvermind


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

like image 27
Noah Goodrich Avatar answered Nov 07 '22 21:11

Noah Goodrich