Build your own PHP framework--Abstract framework content, PHP framework--Abstract _php Tutorial

Source: Internet
Author: User
Tags autoload php framework

Build your own PHP framework-the content of the abstract framework, the PHP framework-abstract


In the previous blog, we set up the simplest framework to enter from the public/index.php of a single entry, parse out the corresponding controller and action, execute, render the corresponding page or output the corresponding data.

But we can see in the public/index.php file, is a bunch of code, if after, I want to add permissions/events and so the mechanism is also here, it is not difficult to imagine, in the future, index.php will become a huge PHP file. We certainly don't want to see such a scenario, so we need to abstract this out and keep the entry file simple and straightforward.

So what are we going to do with this abstraction? There are smart classmates have thought of, that is, the location defined in Composer.json, we look at the Composer.json file:

{    "name": "Craryprimitiveman/simple-framework",    "description": "A simple PHP Framework" ,    " License ":" MIT ",    " authors ": [        {            " name ":" Harrysun ",            " email ":" Sunguangjun@126.com "        }    ],    " require ": {},    " AutoLoad ": {         "Psr-4": {            "sf\\": "src/",            "app\\": ""        }    },    " Repositories ": [        {" type ":" Composer "," url ":" Http://packagist.phpcomposer.com "},        {  false}    ]}

Can see autoload in the psr-4 there is a sf\\, his address is src/, this is what we want to put abstract content of the place.

One would ask, why not use app\\ directly as a namespace? Because of the thought of the content under the SRC to be made into a composer package, migrated to the vendor.

The following is the formal beginning of abstraction.

Create two folders under SRC, one is base and the other is web. Base class used to hold the base, and the Web to hold web-related classes. Because in the future the framework may also support the execution of PHP scripts, so to separate base and web, in the future to add PHP script, only need to set up a console folder.

Create a application.php file in two folders, respectively.

First look at the application.php in base.

 Phpnamespace sf\base; Use Exception;/** * application is the base class for all application classes. * @author Harry Sun 
    
 
    */Abstract classapplication{/** * @var string The namespace that controller classes is located in.     * This namespace is used to load controller classes by prepending it to the controller class name.     * The default namespace is ' App\controllers '. */     Public $controllerNamespace= ' App\\controllers '; /** * Runs the application.     * This is the main entrance of an application. */     Public functionrun () {Try {            return $this-HandleRequest (); } Catch(Exception $e) {            return $e; }    }    /** * Handles the specified request. */    Abstract  Public functionhandlerequest ();}

It is an abstract class that implements a simple run method, the Run method is to execute the following HandleRequest method.

It defines an abstract method HandleRequest, which waits to be inherited and implemented.

It defines a Controllernamespace property that records the NAMESAPCE the controller holds, and the default value is ' App\\controllers '.

And look at the application.php on the web.

 Phpnamespace Sf\web;/** * application is the base class for all application classes. * @author Harry Sun 
    
 
    */classApplicationextends\sf\base\application{/** * Handles the specified request. * @return Response the resulting Response*/     Public functionHandleRequest () {$router=$_get[' R ']; List($controllerName,$actionName) =Explode('/',$router); $ucController=Ucfirst($controllerName); $controllerName=$this->controllernamespace. '\\' .$ucController. ' Controller '; $controller=New $controllerName(); return Call_user_func([$controller, ' action '.Ucfirst($actionName)]); }}

is not feel very familiar, in fact is put in index.php in the content of the application HandleRequest method.

Then we need to call the code here from the portal file, which is very simple, and the contents of index.php are as follows:

 
  PHPrequire_once(__dir__. '/.. /vendor/autoload.php '); $application New sf\web\application (); $application->run ();

Go directly to the new instance of the Web application, execute the Run method, it is not very simple.

Visit: http://localhost/simple-framework/public/index.php?r=site/test, you can see the same result last time.

All right, let's get here today. Project content and blog content will also be put on GitHub, welcome suggestions.

code:https://github.com/craryprimitiveman/simple-framework/tree/0.2

Blog Project:https://github.com/craryprimitiveman/create-your-own-php-framework

http://www.bkjia.com/PHPjc/1051887.html www.bkjia.com true http://www.bkjia.com/PHPjc/1051887.html techarticle Build Your own PHP framework-the content of the abstract framework, PHP framework-Abstract The previous blog, we set up a simple framework, from the single entrance of the public/index.php into, parse out ...

  • 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.