Build Your own PHP framework

Source: Internet
Author: User
Tags add autoload error handling mkdir php file php framework require composer install

First, let's say, why do we have to create our own framework?

Why create your own framework? If you talk to people around you, everyone will tell you it's a bad thing to reinvent the wheel, you'd better choose an existing frame and forget to "create your own frame". Most of the cases, they were right, but I thought about the benefits of creating my own framework:

    • Learn more about the underlying architecture of the framework

    • Create a framework that meets your specific needs (but first make sure your needs are really special)

    • Try to write a framework for fun (to "learn and discard")

    • Want to leverage new development techniques and best practices to refactor existing projects

    • Proving to the world that you can also write frames (...). But just a little bit of pay)

I'm going to walk you through step-by-step steps to create a framework. Each step you get is a fully available framework. We'll start with a simple frame and add a little bit of functionality to it. Finally, you will be able to get a complete web framework.

The reason above is to use the Symfony2 components to create their own PHP framework, feel good to say it directly to use.

Let's start with a directory and then go to the directory

 
  
  
  1. mkdir simple-framework
  2. CD Simple-framework

Then set up a directory to place Controller/model/view, etc.

 
  
  
  1. MKDIR Controllers Models views Public

Public is used to store a unified portal, where the index.php is built, and most of the frames are now a single entry.

Then we need to support the following composer, and we want third party packages to be managed through composer. If you still don't know what composer is, please check composer.

Execute composer Init under the Simple-framework folder and fill in the appropriate content to generate a Composer.json file that looks like this:

 
  
  
  1. {
  2. "Name": "Craryprimitiveman/simple-framework",
  3. "description": "A Simple PHP Framework",
  4. "License": "MIT",
  5. "Authors": [
  6. {
  7. "Name": "Harrysun",
  8. "Email": "sunguangjun@126.com"
  9. }
  10. ],
  11. "Require": {}
  12. }

Let's modify the following and the results are as follows:

 
 
  1. {
  2. "Name": "Craryprimitiveman/simple-framework",
  3. "description": "A Simple PHP Framework",
  4. "License": "MIT",
  5. "Authors": [
  6. {
  7. "Name": "Harrysun",
  8. "Email": "sunguangjun@126.com"
  9. }
  10. ],
  11. "Require": {},
  12. "AutoLoad": {
  13. "Psr-4": {
  14. "sf\\": "src/",
  15. "App\\": ""
  16. }
  17. },
  18. "Repositories": [
  19. {' type ': ' composer ', ' url ': ' Http://packagist.phpcomposer.com '},
  20. {' Packagist ': false}
  21. ]
  22. }

One of the autoload is to support our own project file loading, where SF is the framework of the Code, while the app is the normal business of the code, which repositories is to solve in the domestic use of composer download, Download does not come down the problem, if in foreign countries, or have a VPN agent, can be directly removed.

Then execute composer install.

So the basic directory structure is built.

In the entry file public/index.php, the AutoLoad file is introduced as follows:

 
  
  
  1. <?php
  2. Require_once __dir__. '/.. /vendor/autoload.php ';

Then we define our URL to be such a http://localhost/simple-framework/public/index.php?r=site/test and then add the corresponding nginx or Apache rewrite, is the URL to become concise good-looking, but for the time being first.

This URL accesses the actiontest in Sitecontroller, so let's simply implement this feature.

 
  
  
  1. <?php
  2. Require_once __dir__. '/.. /vendor/autoload.php ';
  3. $router = $_get[' R '];
  4. List ($controllerName, $actionName) = explode ('/', $router);
  5. $ucController = Ucfirst ($controllerName);
  6. $controllerName = ' app\\controllers\\ '. $ucController. ' Controller ';
  7. $controller = new $controllerName ();
  8. Return Call_user_func_array ([$controller, ' action '. Ucfirst ($actionName)];

The code above does not add any error handling, just a simple feature implementation.

Then add the sitecontroller.php file in the Controllers folder, which reads as follows:

 
  
  
  1. <?php
  2. namespace App\controllers;
  3. Class Sitecontroller
  4. {
  5. Public Function Actiontest ()
  6. {
  7. Echo ' success! ';
  8. }
  9. }

Then visit the URL before http://localhost/simple-framework/public/index.php?r=site/test and you'll see the print out of the success!

Then we're going to write a Actionview method to render a page. The code for Actionview is as follows:

 
  
  
  1. Public Function Actionview ()
  2. {
  3. $body = ' Test body information ';
  4. Require '.. /views/site/view.php ';
  5. }

We also need to add the site folder under the views and create the view.php file under the site folder, which reads as follows:

 
  
  
  1. <title>title</title>
  2. <body>
  3. <?php echo $body;? >
  4. </body>

The last visit to Http://localhost/simple-framework/public/index.php?r=site/view will get the following page:

All right, let's get here today. Project content and blog content will also be placed on the GitHub, you are welcome to make suggestions.

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

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



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.