Build your own PHP framework with Composer step-by-step (ii)

Source: Internet
Author: User
Tags autoload php framework

In the previous article we have established an empty Composer project, this article will describe how to build a route.

The prestigious CodeIgniter framework is the PHP development Starter framework for many people, and it is also the framework for me to start learning how to build a website from scratch. I learned a lot in CI, in which the deep understanding of MVC and the understanding of the nature of the framework affected me the most. From the point of view of using the framework to improve development efficiency, the essence of the framework is routing.

Now we're going to build our own routes, and we'll go to GitHub first: Click here to view search results

Recommended Https://github.com/NoahBuscher/Macaw, the corresponding Composer package is NOAHBUSCHER/MACAW.

To start installing it, change the Composer.json:

{"  require": {    "Noahbuscher/macaw": "Dev-master"  }}

Run composer update and you will get the following directory after success:

At this point, Macaw package installation success!

Here is the moment to witness the miracle! We will give MFFC vitality, let it really run up!

Create a new Mffc/public folder, which will be the only visible part of the user. To create a new index.php file under a folder:

<?php//Autoload Auto-load require '. /vendor/autoload.php ';//routing configuration require '. /config/routes.php ';

The above line represents the introduction of the Composer auto-loading feature, and the following line indicates the load routing profile. Create a new Mffc/config folder and create a new routs.php file with the following contents:

<?phpuse Noahbuscher\macaw\macaw; Macaw::get (' Fuck ', function () {  echo "succeeded!) ";}); Macaw::get (' (: All) ', function ($FU) {  echo ' does not match to routing <br> '. $fu;}); Macaw::d ispatch ();

Macaw documents are located in Https://github.com/NoahBuscher/Macaw, please set your own pseudo-static according to your HTTP service software type, in fact, like most frameworks: "All non-static files point to index.php".

Then, a port with Apache or Nginx assigned to the Mffc/public directory, this step is recommended to use Apache or Nginx.

If you are using a PHP built-in HTTP server:

CD Public && php-s 127.0.0.1:3000

The macaw::get that will cause the route (' Fuck ' must be written as Macaw::get ('/fuck ') to respond.

There is no problem with the current code using Apache + mod_php and Nginx + PHP-FPM methods.

I have 81 ports locally bound, access Http://127.0.0.1:81/fuck can see:

If the page is garbled, please adjust the encoding to UTF-8. If you successfully see the above page, then congratulations, the routing configuration is successful!

Macaw only one file, the removal of empty lines is a little more than 100 lines, through the code we can directly see how it works. Let me briefly analyze:

1. Composer automatic loading in memory after each URL driver mffc/public/index.php maintains a full-scale namespace class name to the file name array, so that when we use a class in the code, the file containing the class will be loaded automatically.

2. We loaded the Macaw class in the routing file: "Use Noahbuscher\macaw\macaw;", followed by a two-time static method:: Get (), this method does not exist, will be by mffc/vendor/codingbean/macaw/ The __callstatic () takeover in the macaw.php.

3. This function accepts two parameters, $method and $params, the former is the specific function name, here is get, the latter is the argument passed this call, namely macaw::get (' Fuck ', function () {...}) Two parameters in the. The first parameter is the URL value we want to listen to, and the second parameter is a PHP closure, which, as a callback, represents what we want to do after the URL match succeeds.

4. __callstatic () is also simple to press the target URL (that is,/fuck), the HTTP method (that is, GET), and the callback code into the static member variables (arrays) of the $routes, $methods, and $callbacks three macaw classes.

5. Macaw of the last line of the routing file::d ispatch (); Method is where the current URL is actually handled. Can directly match to the call callback directly, not directly matched to the use of regular matching.

Original address: https://lvwenhan.com/php/406.html

Build your own PHP framework with Composer step-by-step (ii)

Related Article

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.