URL Rewrites. Part II (Magento Certified Developer Exam)

December 13, 2012 Aleksander Tretjak Certification
URL Rewrites. Part II (Magento Certified Developer Exam)

My previous post was dedicated to URL rewriting with rules located in a database. This part is about rewriting via configuration files. Here we go.

Let’s return to our dispatch() method. The address was rewritten with the help of

The next type is rewriting via configuration files – it’s not as “big” as the previous one.

This rewriting type allows “replacing” controllers. “Replacing” – because, in fact, in this rewriting type Zend_Controller_Request_Http object’s PATH_INFO is rewritten.

Let’s create a simple Test module, which rewrites the Category controller of the Catalog module, and define the rewrite rule.

All the rewrite rules, defined in the configuration files, are first loaded:

If rules exist, the search will begin:

Data for a further processing is prepared and PATH_INFO is replaced:

Because of use of preg_replace, rewriting rules can be described with templates. In this case:

then:

Tip: if you use this rewriting method, compose rules carefully. In this case, ‘#^/catalog/category/#’ rule will not work.

As soon as $pathInfo variable is defined, PATH_INFO is set.

app/code/core/Mage/Core/Controller/Request/Http.php

And passed to dispatch() method for further proceeding.

As we can assume from the method, $this->_rewritedPathInfo is redefined the same way.

app/code/core/Mage/Core/Controller/Request/Http.php

This array takes part in the process of the current controller, path and action name definition. Methods:

  • getRequestedRouteName,
  • getRequestedControllerName,
  • getRequestedActionName

in the app/code/core/Mage/Core/Controller/Request/Http.php class return data based on this variable. It affects templates structure for the current request.

If complete variable is installed:

Rewrite takes place without $_rewritedPathInfo:

I.e. the original PATH_INFO will be used.

It is necessary to use the following rule to guarantee the stated module will be rewritten:

Processing of this template type is located in the _processRewriteUrl method:

app/code/core/Mage/Core/Controller/Varien/Front.php

I.e. if we redefine for the frontName module:

It will be possible to use the following template

Instead of this one:

One more way to change controller behavior is to use the before event:

After defining the path like this, it’s possible to use your own controller instead of a third-party one.



2 comments

  1. I think ‘/’ is missing

    /test/category/

    because without ‘/’ preg_replace function seems not to work

Post a new comment

top
BelVG Newsletter
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.
Email *