Magento Development--Deep understanding Magento Chapter II

Source: Internet
Author: User
Tags lowercase naming convention blank page

Chapter II-Magento request distribution and controller

Model-view-controller (MVC), model-view-controller, originates from Smalltalk programming language and Xerox PARC. There are a number of systems that are based on the MVC architecture, and different systems MVC implementations are slightly different, but they all embody the essence of MVC, separating data, business logic, and display logic. The most common PHP MVC framework is a URL request that is blocked by a php file, usually called the front-end controller (Front Controller), which analyzes this URL to obtain an executive controller (Action Controller) and the name of an execution method (Action methods), which is often referred to as routing (Routing) instantiation of the execution controller called the execution controller to execute the execution method of executing the control to handle the business logic, such as fetching data The executive controller is responsible for passing data to the display logical display logic to generate HTML

This architecture is a huge leap relative to the traditional "every PHP is a page", but there are still complaints that "translator: CodeIgniter is such an MVC framework" the front-end controller still runs a configuration-based convention in a global manner that causes the system to be insufficiently modular URLs Routing not flexible controllers often and view bindings changing default settings often result in a lot of refactoring

Magento created a more abstract MVC solution to the problem.

URL request by a PHP intercept this PHP file instantiate a Magento object Magento object Instantiate the front-end controller the front-end controllers instantiate the routing object specified in the global configuration, which can be multiple routing objects that match the request URL, if a match is found, Then you can get an executive controller and an execution method of name instantiation #6 to obtain the execution controller, and invoke the corresponding execution method to execute the method to handle the business logic, the model data controller instantiates the layout object (Layout) Layout object According to the request parameter, The system configuration creates a list of block objects (blocks), and the materialized layout object invokes the output method of the Block object to generate HTML. This is a recursive procedure because a block object can nest block objects and each block object corresponds to a template file (Template files). The Block object contains the display logic, the template file contains the HTML and the PHP output code block object to obtain data directly from the model, in other words, in the Magento MVC architecture, the controller does not direct data to the view

It's complicated and we'll explain each part in detail later. Let's focus on the "front-end controller-> routing objects-> execution Controller" section.

Hello World Example

We have said too many theories, now let's practice and deepen our understanding through practice. Here's what we're going to do. Create a Hello World module Configure routing for this module to create an execution controller for this module

Create the Hello World module

First, we're going to create a directory structure of a module that we've already done before, and no longer boil down to
app/code/local/alanstormdotcom/helloworld/block  app/code/local/ alanstormdotcom/helloworld/controllers  app/code/local/alanstormdotcom/helloworld/etc  app/code/local/ alanstormdotcom/helloworld/helper  app/code/local/alanstormdotcom/helloworld/model  app/code/local/ Alanstormdotcom/helloworld/sql
Below is the content of Config.xml path:app/code/local/alanstormdotcom/helloworld/etc/ Config.xml <config> <modules> <Alanstormdotcom_Helloworld> <version>0.1.0</version> </Alanstormdotcom_Helloworld> </modules> </config>  Then we will create a system configuration file to activate this module  

Path:app/etc/modules/alanstormdotcom_helloworld.xml <config> <modules> <alanstormdotcom_helloworld > <active>true</active> <codePool>local</codePool> </Alanstormdotcom_Helloworld> </modules> </config> Finally, let's check to see if the module has been activated.
Empty Magento cache in admin background, enter system->configuration->advanced expand "Disable Modules Output" Confirmation alanstormdotcom_ HelloWorld showed up.

Configuring Routing

Next, we will configure a route. Routing is used to convert a URL request into an execution controller and method. Unlike traditional PHP mvc, you need to explicitly define your route in the Magento global configuration. Let's continue with the example above, in Config.xml, add the following code

<config> ... <frontend> <routers>

Here we have a lot of terminology to explain.

what is <frontend/>.

<frontend/> Label points to a Magento area, such as "frontend" refers to the front of the site, "admin" refers to the background of the site, "install" refers to the Magento installation program. "Translator Note: This is a bit like a disk partition, the area and the area is independent of each other, but all belong to the operating system can be managed, here Magento management." The default Magento installation does not have a "install" area, and the frontend zone takes over, and the following code in the global configuration can explain this

<frontend> ... <install> <use>standard</use> <args> <module>mage_install</ module> <frontName>install</frontName> </args> </install> ... </frontend>

what is <routers/>. Phil Karlton has a very famous phrase, "There are only two things that are difficult in the computer field: Caching and naming." Magento introduces a lot of new concepts, there is no doubt that there are many naming problems, here is an example. <routers> tags sometimes contain the definition of a routing object, sometimes including the definition of a path. A routing object is an entity that is routed, and the path is only one parameter of the routed object. "Translator Note: If you look at the global configuration XML carefully, you will find that there are two places where <routers&gt are present, one is" <web>-> <routers> "and the other is" <frontend> -> <routers> ". If you take a closer look, you will find that both <routers> contain different content. The first contains the definition of the routing object, and the second contains the definition of the path. 】

What is <module/>.
The contents of this label should be the full name of a module, Packagename_modulename, here is "Alanstormdotcom_helloworld". Magento uses this name to locate your module file.

what is <frontname/>. When a router parses a URL, it is done according to the following rules
http://example.com/frontName/actionControllerName/actionMethod/
So, when we define "HelloWorld" in the <frontName> tag, Magento will submit the following URL request to our module "Alanstormdotcom_helloworld" to process
http://example.com/helloworld/*
Some people tend to confuse <frontName> with front-end controllers (Front Controller). They are two different concepts,<frontname> only related to routing. "Translator Note: Based on the Magento MVC process we talked about earlier, the front-end controller is used to instantiate all the routes, and the" Frontname "here is just one parameter in the routing process.

What is
The name of this label should be the lowercase version of the module's name. Our module name is "Helloworld", so here we use "Helloworld". You should also have noticed that our definition of "frontname" is also compatible with our module. This is an unwritten rule, but not a mandatory one. In fact, a module can define multiple <routers&gt, that is, there can be multiple "frontname".

Create an execution controller for a route

Remember Magento's MVC process? Routing will give control to the execution controller. We defined the route above and now we define our execution controller. Create a file first
app/code/local/alanstormdotcom/helloworld/controllers/indexcontroller.php
The controller of the module should be placed inside the module's subdirectory "Controllers" (lowercase c). This is the rule that Magento will look for the module's controller file in this directory. Our first controller contains the following

Class Alanstormdotcom_helloworld_indexcontroller extends Mage_core_controller_front_action {public function Indexaction () {echo ' Hello world! ';}}

Empty the Magento cache and request the following URL
Http://exmaple.com/helloworld/index/index
If you see a blank page that says "Hello world", congratulations, you have successfully created your first Magento controller.
How to name the execution controller.
Remember Config.xml's <module> label?
<module>Alanstormdotcom_Helloworld</module>
The name of the execution control is constituted as follows
Start with the content of the <moudule> tag (Alanstormdotcom_helloworld) immediately followed by an underscore (alanstormdotcom_helloworld_) plus the name "Index" We gave the controller ( Alanstormdotcom_helloworld_index) finally add the keyword "Controller" (Alanstormdotcom_helloworld_indexcontroller) The execution controllers we define ourselves as part of the frontend area should inherit mage_core_controller_front_action.

What does the index/index in the URL mean.
As mentioned earlier, the rules for magento default routing are as follows
http://example.com/frontName/actionControllerName/actionMethod/
So at the URL we requested
Http://exmaple.com/helloworld/index/index
where "HelloWorld" is "Frontname", the first "index" is the name of the execution controller (Action Controller), and the second "index" is the name of the execution method. In contrast to the execution controller code we write, it is not difficult to find that the execution method is defined by executing the method name plus the "Action" keyword
Public Function indexaction () {...}
Magento finds the execution controller file based on the naming convention and instantiates it, and then invokes the specified execution method according to the naming convention. If the URL does not give the execution controller name or execution method, Magento will replace it with the default "index", so the following three URLs are equivalent
Http://exmaple.com/helloworld/index/index
http://exmaple.com/helloworld/index/
http://exmaple.com/helloworld/
Let's look at one more example. If the URL follows
Http://exmaple.com/checkout/cart/add
Magento The following steps are performed
Query the global configuration, find the module corresponding to Frontname "checkout", Mage_checkout find the "addaction" method of executing controller "Mage_checkout_cartcontroller" Calling execution controller
Further understanding of the Executive Controller

Let's add an execution method to our execution controller. Add the following code to indexcontroller.php

Public Function goodbyeaction () {echo ' Goodbye world! ';}

Request URL
Http://example.com/helloworld/index/goodbye
You should see "Goodbye world!" this time. Because we inherited "mage_core_controller_front_action", we can use some of the methods and variables that the parent class has already defined. For example, the parent class converts the arguments followed by the URL into an key/value array. Add the following code to our execution controller

Public Function paramsaction () {echo ' <dl> '; foreach ($this->getrequest ()->getparams () as $key => $value) {echo ' <dt><strong>param: </strong> '. $key. ' </dt> '; Echo ' <dl><strong>value: </strong> '. $value. ' </dl> '; Echo ' </dl> '; }

Request the following URL
Http://example.com/helloworld/index/params?foo=bar&baz=eof
You should see the following output Param: foo value: bar Param: baz value: EOF Finally, let's write an executive controller to handle the URL
Http://example.com/helloworld/messages/goodbye
The executive controller name here is "messages", so we want to create the following file
app/code/local/alanstormdotcom/helloworld/controllers/messagescontroller.php
The class name of the executing controller should be
Alanstormdotcom_helloworld_messagescontroller Add Execution method Public function goodbyeaction () {echo ' Another Goodbye ';} Well, that's probably it for Magento's MVC architecture. It's a bit more complicated than traditional PHP mvc, but Magento's highly flexible MVC architecture allows you to create almost any URL structure you can think of. Source text: Http://www.oschina.net/bbs/thread/7938?lp=1

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.