Build your own PHP Framework-build the basic structure and framework basic structure-PHP Tutorial

Source: Internet
Author: User
Tags composer install
Build your own PHP Framework-set up the basic structure and framework basic structure. Build your own PHP Framework-set up the basic structure. First of all, let's take a look at why we should create our own framework? Why should we create our own framework? If you build your own PHP Framework-set up the basic structure and framework Basic structure

First, let's look at why we need to create our own framework?

Why should we create our own framework? If you talk to people around you, everyone will tell you that it is a bad thing to reinvent the wheel. you 'd better choose an existing framework and forget the idea of "creating your own framework. In most cases, they are correct, but I think of several advantages of creating my own framework:

  • Learn more about the underlying architecture of the framework
  • Create a framework that meets your special needs (but first you must determine that your needs are really special)
  • Try to write a framework for fun (for the purpose of "learning and then abandoning)
  • Use new development technologies and best practices to reconstruct existing projects
  • Prove to the world that you can write a framework (... but you only need to pay a little bit)

I will guide you step by step to create a framework. What you get in each step is a fully usable framework. We will start with a simple framework and then add functionality to it a little bit. Finally, you can get a complete web framework.

The above reason is that the Symfony2 component is used to create your own PHP Framework. if you think it is good, you can use it directly.

Create a directory first, and then enter the Directory

mkdir simple-frameworkcd simple-framework

Then create directories for the controller, model, and view respectively.

mkdir controllers models views public

Public is used to store unified portals and index. php is built in it. Most frameworks are now single portals.

Then we need to support the following composer. we hope that the third-party packages can be managed through composer. If you do not know what composer is, check composer.

Run composer init in the simple-framework folder, enter the corresponding content, and generate a composer. json file. the content is roughly as follows:

{    "name": "craryprimitiveman/simple-framework",    "description": "A simple php framework",    "license": "MIT",    "authors": [        {            "name": "harrysun",            "email": "sunguangjun@126.com"        }    ],    "require": {}}

Modify the following:

{    "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"},        {"packagist": false}    ]}

Autoload is used to support file loading of our own projects. the code of the framework is in sf, and the code of the normal business is in app, the repositories aims to solve the problem of downloading composer in China. if you are abroad or have a VPN as a proxy, you can directly remove it.

Then run composer install.

In this way, the basic directory structure is built.

Introduce the autoload file in the public/index. php file as follows:

 

Then we define our url as http: // localhost/simple-framework/public/index. php? R = site/test. then, the corresponding nginx or apache rewrite will be added. The url becomes concise and nice, but this is the first time.

This url accesses actionTest in SiteController. below we will simply implement this function.

  

No error handling is added to the code above, which is just a simple function implementation.

Add the SiteController. php file to the controllers folder. the content is as follows:

   

Then access the previous url http: // localhost/simple-framework/public/index. php? R = site/test, you will see the success printed!

Then we will write an actionView method to render a page. The code of actionView is as follows:

    public function actionView()    {        $body = 'Test body information   ';        require '../views/site/view.php';    }

We also need to add the site folder under views and create the view. php file under the site folder. its content is as follows:

            title                
        

Finally, access http: // localhost/simple-framework/public/index. php? R = site/view. the following page is displayed:

Now, we will be here today. The project content and blog content will also be put on Github. You are welcome to give suggestions.

Code: https://github.com/CraryPrimitiveMan/simple-framework/tree/0.1

Blog project: https://github.com/CraryPrimitiveMan/create-your-own-php-framework

First of all, let's look at why we need to create our own framework? Why should we create our own framework? If you and...

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.