Lumen: Initialization of lumen (1)--app initialization

Source: Internet
Author: User

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Some of the comments of the original Baidu translation, can be some difficult to understand or strange, I will be based on their understanding of the adjustment of HA!!! Do not like to spray, Layer Master English not pass ...

Let's take a look at the entry file public/index.php

Request Header Header (' Content-type:application/json; Charset=utf-8 ');
/*
|--------------------------------------------------------------------------



| This creates an instance of an application/container and the application is ready to receive HTTP/console from the environment requirements.
*/
$app = require __dir__. ' /.. /bootstrap/app.php ';

/*
|--------------------------------------------------------------------------
| Run the application (running the application )
| Once we have the application,
| We can process incoming requests through the kernel and send related responses back to the client's browser.
| Let them enjoy the creative and wonderful applications we have prepared for them.
*/
/*
|--------------------------------------------------------------------------


| Once we have the application, we can process the incoming request through the kernel and send the relevant response back to the client's browser.
| Let them enjoy the creative and wonderful applications we have prepared for them.
*/
$app->run ();

So the most important thing now is bootstrap/app.php.

Require_once __dir__. ‘/.. /vendor/autoload.php '; try {    (new dotenv\dotenv (__dir__). ‘/.. /'))->load ();} catch (Dotenv\exception\invalidpathexception $e) {    //}

Here is the introduction of the composer package, previously said, here is unknown to talk about. Dotenv is the use of env configuration.

Look down.

/*|--------------------------------------------------------------------------| Create the application (app creation) |--------------------------------------------------------------------------| | Here, we will load the environment and create an instance of the application that is the central part of the framework.
| We will use this application as the "IOC" container and router for this framework. |*/$app = new Laravel\lumen\application ( Realpath (__dir__. ‘/.. /'); $app->withfacades (); $app->witheloquent ();
Laravel\lumen\application

    

Class Application extends Container
{

Use Concerns\routesrequests,
Concerns\registersexceptionhandlers;

     ...
/** * Create A new Lumen application instance. (Create a new instance of the lumen application.) ) */public function __construct ($basePath = null) { if (! Empty (env (' App_timezone '))) { Date_ Default_timezone_set (env (' App_timezone ', ' UTC ')); } $this->basepath = $basePath; $this->bootstrapcontainer (); $this->registererrorhandling (); }
This can be said to be the most core class of the entire lumen application, inherited the Core container class (Container), the left and right reference concerns\routesrequests (routing), concerns\ Registersexceptionhandlers (Exception handling), the next instance of the App object, it is important Oh!!!

$this->bootstrapcontainer ();
    /**     * Bootstrap the application container. (Boot application container)     *     * @return void */    protected function Bootstrapcontainer ()    {        static::setinstance ($this);        $this->instance (' app ', $this);        $this->instance (' laravel\lumen\application ', $this);        $this->instance (' path ', $this->path ());        $this->registercontaineraliases ();    }

Lumen's dependency injection services are mostly carried out in containers (Container), so it's important to say this step

look first . setinstance () function

    /**     * Set the shared instance of the container. (Sets the sharing instance of the container. )     *     * @param  \illuminate\contracts\container\container|null  $container     * @return Static     * /public    static function setinstance (containercontract $container = null)    {        return static:: $instance = $ container;    }

A simple word, the current inheritance container, implementation of the Containercontract interface application app instance, registered to the $instance object of container, feel this realizes the cross-cutting between each other, there will be a big use!

And then there's the $this->instance () function,

/**     * Register an existing instance as GKFX in the container. (Registers a container that is shared by an existing instance.) )     */public    function instance ($abstract, $instance)    {        $this->removeabstractalias ($abstract); Removes an alias from the context-bound cache.        $isBound = $this->bound ($abstract);//determines that the given type has been bound.        unset ($this->aliases[$abstract]);//delete the corresponding registration type alias.        //todo Translation Correction
Check to determine if this type has been bound, //If there is a callback and reflection that we will trigger with the container registration can consume the class has resolved the update here. $this->instances[$abstract] = $instance; if ($isBound) { $this->rebound ($abstract);//Trigger callback given abstract type }

And then to the $this->registercontaineraliases () function, which is used to annotate the alias of the core container

protected function registercontaineraliases () {$this->aliases = [' Illuminate\contracts\foundatio N\application ' + ' app ', ' illuminate\contracts\auth\factory ' = ' Auth ', ' illuminate\contracts\ Auth\guard ' = ' auth.driver ', ' illuminate\contracts\cache\factory ' = ' Cache ', ' Illuminate\con            Tracts\cache\repository ' = ' cache.store ', ' illuminate\contracts\config\repository ' = ' Config ',            ' Illuminate\container\container ' + ' app ', ' Illuminate\contracts\container\container ' + ' app ', ' Illuminate\database\connectionresolverinterface ' = ' db ', ' illuminate\database\databasemanager ' = ' DB ', ' illuminate\contracts\encryption\encrypter ' = ' encrypter ', ' Illuminate\contracts\events\di Spatcher ' = ' events ', ' illuminate\contracts\hashing\hasher ' = ' hash ', ' log ' = ' psr\log\l '        Oggerinterface ',    ' Illuminate\contracts\queue\factory ' = ' Queue ', ' illuminate\contracts\queue\queue ' = ' queue.connect '            Ion ', ' request ' = ' illuminate\http\request ', ' laravel\lumen\routing\urlgenerator ' = ' url ', ' Illuminate\contracts\validation\factory ' = ' validator ', ' illuminate\contracts\view\factory ' =&gt ;    ' View ',]; }

There is a feeling of wood that is familiar to these names!

At this point $this->bootstrapcontainer () finished, and then look at $this->registererrorhandling (), since it is exception handling, it is certainly in the concerns\ Registersexceptionhandlers (Exception handling).

    /**     * Set the error handling for the application. (Sets the application's handling of errors. )     *     * @return void */    protected function registererrorhandling ()    {        error_reporting ( -1);        Set_error_handler (function ($level, $message, $file = ", $line = 0) {            if (error_reporting () & $level) {                thro W New Errorexception ($message, 0, $level, $file, $line);            }        });        Set_exception_handler (function ($e) {            $this->handleuncaughtexception ($e);        });        Register_shutdown_function (function () {            $this->handleshutdown ();        });    }

This part of the following in writing a text to add, http://...

Here, $app application is ready, and the next one goes on.

$app->withfacades ();//Register the façade for the application. $app->witheloquent ();//load a powerful library for your application.

And behind the content!!!

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Lumen: Initialization of lumen (1)--app initialization

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.