2016 Laravel series Getting Started Tutorial (2) [the most suitable for Chinese Laravel tutorial] this tutorial sample code see: https://github.com/johnlui/Learn-Laravel-5
The fastest way to get stuck anywhere is to look at the sample code.
In this article, I will learn the most important part of the Laravel framework-the routing system.
If you have read the old Laravel 5.0-based tutorial, you will find that the Auth system construction of the book has been solved by Laravel hacker. In earlier Laravel 4 tutorials, I actually improved the slope of the short-term learning curve by letting everyone build a difficult Auth system on their own, so that the babies can quickly feel how Laravel works. However, unfortunately, the current Auth system is too powerful. after a few commands are executed, this function is activated. the new user is still confused. In order to make up for this defect, the baby decided to go into battle in the red dragonfly, and the hand-edge routing system directly showed you how Laravel organized the MVC architecture to control website operations.
First-recognized route
The routing system is the core of all PHP frameworks, and routes are mapped from URLs to code snippets. the routing systems attached to different frameworks are the most authentic portrayal of the framework, all at a glance. Laravel routing documents: http://laravel-china.org/docs/5.1/routing
After Laravel 5, the route is placed in the location of learnlaravel5/app/Http/routes. php. let's take a look at the remaining lines of code in the current route:
Route::get('/', function () { return view('welcome');});Route::auth();Route::get('/home', 'HomeController@index');
The middle line of code Route: auth (); is the routing configuration automatically injected by the Auth system. we don't need to go into it. our focus is on the first three lines and the last line of code.
Namespace
This baby has always believed that Laravel 5, in addition to its significant performance improvement, has made the greatest improvement over 4 because of the new namespace plan: clearer, more reasonable, and more conducive to new users.
The conciseness of Laravel 4 failure
In the Laravel 4 era, a large number of codes run in the root namespace, such as routing, Controller, and Model. It seems that you can write a few lines of boring use xxxx; in fact, it is a false use of the namespace, and it is toxic for beginners to learn the namespace.
Absolute class name
Laravel 5 fully introduces the psr-4 namespace standard: the namespace is the same as the folder level where the actual file is located, and the first letter of the folder is the agreed namespace for this file. Example: learnlaravel5/app/Http/Controllers/HomeController. php's absolute class name: \ App \ Http \ Controllers \ HomeController, learnlaravel5/app/User. the absolute class name of php is \ App \ User.
The "absolute class name" is created by the baby: in a namespace-enabled system, the sub-namespace class has a globally accessible name, this name is the full name of the namespace of the class. Although the namespace seems odd in the "pragmatic" PHP language, it still follows the operating principles and philosophy of PHP. Similarly, Laravel is written in PHP code no matter how powerful it is. so when Laravel does not provide a function you need, don't worry, just write the code in the PHP way.
Psr-4 official English documentation here: http://www.php-fig.org/psr/psr-4/
Useful materials
In fact, there is nothing special about namespaces. I have written an article dedicated to the secrets of namespaces:
PHP namespaces
"
Basic route resolution closure routing
The first three lines in the routing file are closure routes:
Route::get('/', function () { return view('welcome');});
Closure routing uses closure as the response code for this request, which is convenient and flexible. many simple operations can be solved directly in the closure. For example, "current time of the output server ":
Route::get('now', function () { return date("Y-m-d H:i:s");});
If you want to get the Beijing time, set the timezone to Shanghai in line around 55th of learnlaravel5/config/app. php:
'timezone' => 'Asia/Shanghai',
At this time access http://fuck.io: 1024/now can get the following results:
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');
The meaning of this line of routing code can be guessed one or two: when the GET method to access the http://fuck.io: 1024/home, call the index method (function) in the HomeController controller ). Similarly, you can use Route: post ('/home', 'homecontroller @ indexpost') to respond to the POST method request.
Controller @ method call principle analysis
Like all PHP framework routes, Laravel uses the simplest and most direct PHP method to call the method in the controller: get the object using the string initialization class and call the specified method of the object, returned results. Below I will list several steps to test the Laravel routing call process. if you are interested, you can study it yourself.
Learnlaravel5/app/Providers/RouteServiceProvider. php
Search for routes. php globally. we found this file. The mapWebRoutes method at the end of this file adds a routing group to all the routes agreed to define 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'); });}
Following this function, you will find the namespace definition:
protected $namespace = 'App\Http\Controllers';
After that, how are namespaces, classes, and methods passed?
Learnlaravel5/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher. php
After simple tracing, we found this file. Let's add a line of var_dump ($ controller); in the dispatch method, refresh to see the following output on the page:
This is the "absolute class name" of the controller class to be called ".
Last step
Laravel uses a complete object-oriented program architecture to encapsulate controller calls in multiple layers. Therefore, the simplest method of detection is to manually throw an error, in this way, you can see the complete call stack:
Add a row throw new \ Exception ("I intentionally", 1) before return in the index method of HomeController. refresh the page and you will see the following picture:
We can see that row 4 of learnlaravel5/vendor/laravel/framework/src/Illuminate/Routing/Controller. php eventually drives HomeController:
public function callAction($method, $parameters){ return call_user_func_array([$this, $method], $parameters);}
The specific details will not be explained in detail. if you are interested, read these methods one by one. I believe it will be helpful for you to understand the operating principle of Laravel. In fact, the degree of closeness between PHP and strings has already forced JavaScript and JSON. Share a little egg at the end: How to write this laravel route?
Next step: Getting started with Laravel 2016 series (3) [Laravel tutorial most suitable for Chinese users]