URL rewriting principle, step URL rewrite

Source: Internet
Author: User

 


Http://www.magento-tutorial.net/how-does-magento-url-rewriting-work/how does magento URL rewriting work
January 2011 | posted by admin

 Create a category

 

We just input the category's name, and set "is active" to yes, and then click Save category

 

We can see that the URL key is automaticly generated. The value is "Nikon-camera ".

When a category is created, in Database Table core_url_rewriting
, There is a new record created at the same time

 

 

 

As we can see above, the main fields of table core_url_rewriting are:

  • Category_id (if this filed is null, it means this is a product URL rewriting record)
  • Product_id (if this field is null, it means this is a category URL rewriting record)
  • Id_path
  • Request_path (this is the URL suffix which we will use in category page or product page)
  • Target_path (this is the real target URL, it is the standard MVC style)

 

In front end of magento, when we put mouse on the category we created

 

We can see the target URL is looks like:

We are sure that the links of this menu is related
With the database table core_url_rewriting, it is generated by the Field
Request_path. Click on the category link, and then the category "Nikon"
Is opened.

How does magento find the category by this URL?

1st stop:/index. php (this is the entrance of most part of URL request in magento)

2nd
Stop:/APP/MAGE. php-> Mage: Run ()

3rd
Stop: mage_core_controller_varien_front-> dispatch ()

 

All of things are handled in Front Controller's dispatch () function, so let's go inside of this function.

 

Here is source code
Of dispatch method, in order to see the main operation; we removed
Line about varien_profiler, which is used to check programs performance.

 

 


Request is a global variable which contain all information about browser request, it is an object of Class "mage_core_controller_request_http
"Which is extend from class zend_controller_request_http
, Here is the hierarchy digoal:

 

1.



Rewriting

At the begining of dispatch (), browser request URL
"/Nikon-camera.html" must be stored in request object. We can proof this
By adding trace log to print the value $ request-> getpathinfo ().

It will print the value of request object's attribute _ pathinfo, here is the value at that moment:

Nikon-camera.html

And then, below program is executed:

Mage: GetModel ('Core/url_rewrite')-> rewrite ();

What this line did is modify the global request object, it will use function rewrite () of model class mage_core_model_url_rewrite
, This class is the entity object correspond with database table core_url_rewrite
. Here is the last lines of the rewrite () method

 

We added trace log to see what is set into $ request object, the printed result is:

Request URI =/index. php/CATALOG/category/View/ID/7

Path Info = CATALOG/category/View/ID/7

 

At this moment, the URL translation between browser request URL and target URL has been finished.

 

We guess that there must be a database operation before this, let's look at the beginning source line of function rewrite ().

 

 

$ Requestcases is an array which composed of $ requestpath and $ requestpath + $ querystring.

As we can see in first highlight line, $ requestpath
Is come from $ request-> getpathinfo (), as we have said before, this
Value is "/Nikon-camera.html", after trimming, the value will become
“Nikon-camera.html ".

 

Let's have a look what's inside the function loadbyrequestpath ($ PATH)
, Here is the function's source:

The meaning of this function can be explained in one SQL:

Select * From core_url_rewrite where request_path = "$ pathinfo"

As we talked before, the record has already created
In database table core_url_rewrite when a category is created, so if
The parameter $ pathinfo can be found in database, then the entity object
Of mage_core_model_url_rewrite will be initialized after above function
Executed. It means we can use below functions to get information
Database Table core_url_rewrite.

$ This-> getidpath ()

$ This-> getrequestpath ()

$ This-> gettargetpath ()

 

1.



Matching

As we can see in di1_1-6, there is a foreach operation

Foreach ($ this-> _ routers as $ router ){



If ($ router-> match ($ this-> getrequest ())){


Break;


}


}

From here, the responsibility transfers to
Front Controller and router. The Front Controller has an array
"Routers" that it uses to decide which module the URL shocould trigger.
This correlation between URL and module name is defined in
Config. XML files.

The available routers are:

• Standard (used in front end request)

• Admin (used in Back office request)

• Default (only used for 404 S)

 

The definition of a match between a router and a module looks like this for the catalog module:

 

Before dispatch () function, there is an Init ()
Function calling which will initial the attribute $ this-> _ routers.
Each router will collect all modules information from all config. xml
Files, standard router (mage_core_controller_varien_router_standard
) Will collect all frontend modules info and Admin router (mage_core_controller_varien_router_admin
) Will collect all backend modules info.

Let's have a look what has been done in match () function of router, just for the sample, we put the standard router's source her

By the pink comments, we can quick overview of the business logic of match () function.

  1. Analyzing target URL, purpose of this is:

A)


Get Module name

B)


Get controller name

C)


Get action name

 

  1. Instantiate controller class
  2. Set values to $ request (Module name, controller name, action name, other parameters)
  3. Dispatch action

 

We can add trace log to see each variable's value, here they are:

$ Module = 'catalog'

$ Realmodule = 'mage _ catalog'

$ Controller = 'category'

$ Controllerclassname = 'mage _ catalog_categorycontroller'

$ Action = 'view'

$ Request-> getparams () = Array

(



[ID] => 7

)

After the last line executed in match () function,
The responsibility will transfer to a specific controller to finish
Left business logic. After business logic is finished, there is a result
Page will be rendered and be set into response object. As we can see in
Distri1-6, the last line in dispatch ():

$ This-> getresponse ()-> sendresponse ();

After this, the result page will be sent to client browser. That's all.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.