Version 2016 Laravel series Introductory tutorial (ii) "Laravel course for the Chinese"

Source: Internet
Author: User
Tags php language response code laravel tutorial
The sample code for this tutorial is shown in: https://github.com/johnlui/Learn-Laravel-5

The quickest way to get stuck in any place is to look at the sample code.

In this article, I will learn the most important part of the Laravel framework-the routing system-with the kids.

If you have read the old version of this tutorial based on Laravel 5.0, you will find that the Particularize Auth system build in that article has been solved by Laravel. In the earlier Laravel 4 version of the tutorial, I actually improved the slope of the short-term learning curve by getting people to build their own hard-to-use Auth system, so that babies could feel the principle of Laravel running more quickly. But unfortunately, now the Auth system is too powerful, execute a few commands to activate the function, the novice is actually foggy. In order to compensate for this shortcoming, the baby decided to shirtless, hand-edge routing system, directly to show you how Laravel is the organization of the MVC architecture, to control the operation of the site.

Primary Knowledge Routing

Routing system is the core of all PHP framework, the route is hosted by the URL to the Code fragment mapping, the different framework of the accompanying routing system is the framework of the nature of the most authentic portrayal, naked, sweeping. Laravel Routing Chinese Document: http://laravel-china.org/docs/5.1/routing

After Laravel 5, we put the route to learnlaravel5/app/http/routes.php, so let's take a look at the only few lines of code that are currently in the route:

Route::get ('/', function () {    return view (' Welcome ');}); Route::auth (); Route::get ('/home ', ' homecontroller@index ');

The middle line of code Route::auth (); is the Auth system automatic injection of the routing configuration, we do not have to delve into, our attention mainly focused on the first three lines and the last line of code.

Name space

The baby has always believed that Laravel 5 in addition to a significant increase in performance compared to the 4 biggest progress is the new namespace planning: clearer, more reasonable, more conducive to the novice.

Laravel 4 Failure of simplicity

Laravel 4 era, a lot of code is running under the root namespace, routing, Controller, Model and so on. It seems that you can write less than a few lines of boring use XXXX; , it is a misuse of namespaces, and it is poisonous to learn namespaces for beginners.

Absolute class name

Laravel 5 introduces the Psr-4 namespace standard: The namespace is consistent with the folder hierarchy where the actual file is located, and the first capitalization of the folder is the contract namespace for this file. For a little chestnut: learnlaravel5/app/http/controllers/homecontroller.php's absolute class name is: \app\http\controllers\homecontroller, The absolute class name for learnlaravel5/app/user.php is: \app\user.

"Absolute class name" is the baby's own creation: in a system with a namespace enabled, the class under the sub-namespace has a name that can be directly accessed globally, which is the namespace full name of the class. Although the namespace looks strange in the "pragmatic" PHP language, he is still PHP, still following the principles and philosophy of PHP. In the same way, Laravel no matter how powerful, he is written in PHP code, so when you suffer Laravel do not provide a function you need, don't panic don't worry, just write the code in the PHP way.

Psr-4 's official English document is here: http://www.php-fig.org/psr/psr-4/

Useful information

Namespaces there is nothing particularly difficult, and I have written an article dedicated to the secret of the stripped-down namespace:

PHP Namespace FAQ

Base Route parsing

Closed packet Routing

The first three lines in the routing file are the closure routes:

Route::get ('/', function () {    return view (' Welcome ');});

Closure routing uses closures as the response code for this request, which is convenient and flexible, and many simple operations can be solved directly in the closure. For example, "Output Server Current Time":

Route::get (' Now ', function () {    return date ("y-m-d h:i:s");});

If you want to get Beijing time, please set timezone to Shanghai on the 55th line of learnlaravel5/config/app.php:

' TimeZone ' = ' Asia/shanghai ',

The following results can be obtained when accessing Http://fuck.io:1024/now:

Controller @ Method Routing

Although the closure routing is flexible and powerful, we still need to return to the MVC architecture in most scenarios:

Route::get ('/home ', ' homecontroller@index ');

This journey by the meaning of the code presumably everyone can guess one or two: when accessing Http://fuck.io:1024/home with the GET method, call the Index method (function) in the HomeController controller. Similarly, you can use the Route::p Ost ('/home ', ' homecontroller@indexpost '); The request to respond to the POST method.

Analysis on the principle of controller @ method invocation

The route of the Laravel is the simplest and most straightforward way to invoke a method in a controller, as is the case with all PHP frameworks: Use a string to initialize the class to get the object, call the object's specified method, and return the result. Below I briefly list a few steps to the Laravel route call process detection, interested in the words can be researched.

learnlaravel5/app/providers/routeserviceprovider.php

Global Search routes.php, we found this file. The last Mapwebroutes method of this file, which gives all routing consent to a routing group, defines a namespace and a middleware:

protected function Mapwebroutes (Router $router) {    $router->group ([        ' namespace ' = = $this->namespace, ' Middleware ' = ' web ',    ], function ($router) {        require app_path (' http/routes.php ');    });}

Looking up at this function, you'll find the namespace definition:

protected $namespace = ' app\http\controllers ';

How are namespaces, classes, methods passed after that?

learnlaravel5/vendor/laravel/framework/src/illuminate/routing/controllerdispatcher.php

After a simple follow-up, we found this file. Let's add a line of Var_dump ($controller) to the dispatch method; , refresh to see the following output on the page:

This is the "absolute class name" of the controller class that we want to invoke.

One final step

The Laravel uses a complete object-oriented program architecture that makes the call to the Controller ultra-multilayer, so the simplest way to probe is to manually throw an error, so you can see the full call stack:

Add a row to throw new \exception ("I deliberately", 1) before return in HomeController's index method; , refresh the page and you will see the following screen:

As we can see, the 80th line of Learnlaravel5/vendor/laravel/framework/src/illuminate/routing/controller.php finally drives the HomeController:

Public Function Callaction ($method, $parameters) {    return Call_user_func_array ([$this, $method], $parameters);}

Specific details are no longer detailed, if the baby is interested, take these methods one by one to see it again, I believe that you understand the Laravel operating principle is very helpful. In fact, the combination of PHP and the string of the tight level has been pressed JS and JSON. End share a little egg: How does this laravel route be written?

Next: 2016 edition Laravel series Introductory Tutorial (iii) "Best for Chinese Laravel tutorial"

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