Laravel5.1 Start-up explanation

Source: Internet
Author: User
Tags autoload

Reference: http://www.cnblogs.com/wish123/p/4756669.html

Laravel the entry file for all requests is:/public/index.php, the code is as follows

<?PHP/*|--------------------------------------------------------------------------| Register the Auto loader|--------------------------------------------------------------------------| | Composer provides a convenient, automatically generated class loader for| Our application. We just need to utilize it! We ' ll simply require it| Into the script, where so, we don ' t has to worry about manual| Loading any of our classes later on. It feels nice to relax.|*/
Automatically load file settingsrequire__dir__. ' /.. /bootstrap/autoload.php ';/*|--------------------------------------------------------------------------| Turn on the lights|--------------------------------------------------------------------------| | We need to illuminate PHP development, so let us turn on the lights.| This bootstraps the framework and gets it ready for use, then it| Would load up this application so we can run it and send| The responses browser and delight our users.|*/
Initializes the service container, which is the framework's IoC container, to generate a Laravel application instance$app=require_once__dir__. ' /.. /bootstrap/app.php ';/*|--------------------------------------------------------------------------| Run the application|--------------------------------------------------------------------------| | Once we have the application, we can handle the incoming request| Through the kernel, and send the associated response back to| The client ' s browser allowing them to enjoy the creative| and wonderful application we have prepared for them.|*/
Generating an instance of the Http/console kernel class from a service container$kernel=$app->make (Illuminate\contracts\http\kernel::class);
Kernel class instances run the handle method, receiving requests and processing results (primarily run middleware and URL-related controllers)$response=$kernel-Handle ($request= Illuminate\http\request::capture ());
Return processing Results$response-Send ();
Show the last action you want to provide for the entire request processing cycle, such as trying to log user behavior after each visit, sending messages to the front end$kernel->terminate ($request,$response);

(1) Automatic loading of files

The automatic load file function is implemented by composer, the loading path is extracted in Composer.json, and then processed according to the laravel corresponding loading mode, which is mainly four kinds of loading modes:

    1. PSR0 load mode-The corresponding file is autoload_namespaces.php
    2. PSR4 load mode-The corresponding file is autoload_psr4.php
    3. Other ways to load classes-the corresponding file is autoload_classmap.php
    4. Load common Method-the corresponding file is autoload_files.php

The specific definition forms the path within the Composer.json, as follows:

"AutoLoad": {    "classmap": [        "Database"    ],    "psr-4": {        "App \ \ ":" app/",        " app\\http\\models\\ ":" app/http/models/"    }}," Autoload-dev ": {     "Classmap": [        "tests/testcase.php"    ]},

(2) Initializing the service container

The Laravel service container is a powerful tool for managing class dependencies and performing dependency injection.

This sentence refers to the core of the Laravel service container is the dependency injection (also control inversion, a design pattern), it is to define instances in the class of this high-coupling mode is discarded, although the Factory mode is OK, but also less flexible, and then in a dependent way to register the relevant class in the container, When needed, the class instance is extracted from the container and injected into itself.

Essentially separation, the various functions are separated, to ensure the loose coupling, for example, the application to the Foo class, the Foo class needs the bar class, the bar class requires BIM class, then create the Bim class, then create the bar class and put Bim, then create the Foo class, and then inject the bar class, and then call the Foo method, Foo calls the Bar method and then does some other work.

Laravel This process is roughly the following steps:

1. Registerbasebindings: Initializes the basic container instance.

2, Registerbaseserviceproviders: Register the basic service provider, there are two: event, route. Event Service Provider sets the processing factory for the queue, and route service provider defines some routing behaviors, including requests, reactions, and redirects.

3, registercorecontaineraliases: Register the core of the container function class alias, these classes include authentication, caching, configuration, queue, session and so on.

(3) Generate kernel class

The HTTP kernel extends Illuminate\Foundation\Http\Kernel the class, which defines an array of that would be bootstrappers run before the request is executed. These bootstrappers configure error handling, configure logging, detect the application environment, and perform other TAS KS that need-be-done before the request is actually handled.

The HTTP kernel also defines a list of HTTP middleware that all requests must pass through before being handled by the app Lication. These middleware handle reading and writing the HTTP session, determine if the application is in maintenance mode, VERIFYI ng the CSRF token, and more.

(4) Kernel handle treatment

Consider kernel as a black box, receiving requests and processing requests.

Laravel5.1 Start-up explanation

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.