PHP framework development: 2. Where should I start? URL

Source: Internet
Author: User

Note: Draft typeArticle, Will be constantly modified and improved, so please read with a skeptical attitude

Where should we start to develop a framework that is difficult to start?

Let's analyze it...

If you have done web development, you should know the more popular clear URL, such as: http://www.loveqipai.com/home/register represents the registration page url.

Let's analyze this URL. Except for the Protocol and host, the rest is/home/register. If you have used any MVC framework, you must know that home represents the Controller name, and register represents the action name. If you don't understand what MVC is, it doesn't matter if you don't understand it. A special chapter will be provided later.

Now the problem is coming. We know that PHP websites are built by default. PHP/phtml/PHP * is post-signed, while/home/register is not post-signed. How can this URL be implemented? The answer is: URL rewrite (URL rewriting)

URL rewrite

In this tutorial, we use the mod_rewrite component of Apache 2.2 +. for details about how to configure the htaccess file, refer to the [Apache mod_rewrite] Article. Here we assume that you have configured Apache, which is next. the rewrite rules in htaccess are described.

About ide: The IDE used in this tutorial isNetbeans 6.9We recommend that you use the same IDE for this tutorial. Of course, you can use any IDE or text processing software you like.

Please create a PHP project in your ide and create three folders: application, library, and public (the directory structure in ZF is similar here)

Among them: the application stores controller, model, view, layout, and so on (the subsequent sections will detail each part), the library stores the framework we want to develop, and public is the home directory of the website, point your Apache DocumentRoot here.

Open the public folder and create an index. php file and A. htaccess file .. The content of the htaccess file is very simple, just two linesCode:

Rewriteengine on
Rewriterule! ^ .*? \. (JPG | JPEG | GIF | PNG | CSS) $ index. php [Nc]

The second line of code is to rewrite the rule, which means that if the accessed URL does not end with JPG, JPEG, GIF, PNG, or CSS, it will be forwarded to the index. PHP page.

After the above processing, index. php becomes the entry point for all non-resource files (for more information about rewrite rules, see its documentation ).

Next we will add content for index. php by using the steps for making a common PHP page.

Note: In this tutorial, we tryImplementation ProcessI will tell you why the code is constantly restructured. What you see now does not mean that it is in the final framework, but it will deepen your understanding and help you understand why you need to modify the code in the future.

 

A PHP applicationProgramGenerally, a website has one or more configuration files. We also need them here.

    1. Create a config folder under public and create a config. php file. You do not need to write anything first;
    2. Include the config. php file in index. php:

Because index. php is our entry file and the URLs accessed by users are transmitted to index. php, we can analyze the URL in index. php according to the parameters provided by the URL.Reasonable callOther code, and return the content that the user actually wants to get to the user. HereReasonable callThat is what our framework is going to do, So we write its code to the folder to which the framework belongs.

Note: In this tutorial, I createWebsiteThe framework name and namespace are also abbreviated as lqp (loveqipai.com). You can change it to the name you want to use.

    1. Create a folder in the Library: lqp, used to store all framework code
    2. create the first file in our framework named rewrite. php. The content is as follows:
    3.   
           php /*** description of rewrite ** @ author z */ class lqp_rewrite {}

      isn't it easy? Haha, let's talk about this lqp_rewrite class. We use lqp as the front iterator of each framework class and it indicates that it belongs to the lqp framework. Now we will add several attributes and constructors to this class:

      <?PHP/*** Description of rewrite *** @ author z */Class lqp_rewrite {protected $ _ urlpath, $ _ controllername, $ _ actionname;
      Private function _ construct () {$ this-> _ urlpath = $ _ server ['request _ URI ']; $ Params = explode ('/', $ this-> _ urlpath); if ($ Params) $ this-> _ controllername = empty ($ Params [1])? 'Home': $ Params [1]; If (count ($ Params)> 1) $ this-> _ actionname = empty ($ Params [2])? 'Index': $ Params [2];}

      $ _ Urlpath indicates the address starting with/, $ _ controllername indicates the Controller name, and $ _ actionname indicates the action name. The code in the constructor gives the appropriate values for these attributes, the Controller and action names are parsed by URLs. If the value does not exist or is null, the default values are home and index.

      The above Code involves the concepts of controllers and actions. Let's go through the MVC-related knowledge in the next section and continue our framework journey.
      This section source code download: http://cid-8248e4adbf2b92f3.office.live.com/self.aspx/.Public/Lesson%202.rar

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.