Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
On Service Providers and Laravel's life cycle

On Service Providers and Laravel's life cycle

In this blog post I talk about Laravel's Service Providers and about its life cycles. In brief, a Laravel application is bootstrapped (started) whenever a webrequest hits index.php in the public folder, or when an artisan command is executed. Tough both the index.php and artisan file work very similarly, I will only talk about index.php. When the index.php file is called it registers the autoloader. After that Laravel's Application is initiated and stored in the $app variable which is used to construct a kernel that manages the input and output of our app. After handling the input by our $kernel it returns a $response object which is send back to our visitor.

Let's rewind a bit, there are four1 important things that are done in the index.php:

  1. The autoloader is registered
  2. Laravel's Application class is initiated and stored in $app
  3. A kernel is constructed using the $app's Service Container.
  4. The kernel handles our input and returns a response.

The goal of this blog post is to explain the inner workings of Laravel's life cycle. The next section describes how the Applicaiton class works

Laravel's Application object

When a Laravel application is bootstrapped a Applicaiton object is made in bootstrap/app.php. This object can be found in vendor/laravel/framework/src/Illuminate/Foundation/Application. There are two important bits about this class: it extends the Illuminate\Container\Container and implements a ApplicationContract.

Registration phase

Bootstrap phase

So how do Service Providers work?

Footnotes:

  1. Actually there is a fifth step where we terminate the kernel which will call the terminate method on any active TerminableMiddleware and terminates the $app instance.
@HawkNostalrius

This comment has been minimized.

Copy link

commented Mar 9, 2016

Could you continue your blog please ?

@WrongWay

This comment has been minimized.

Copy link

commented Apr 13, 2016

Any updates on this ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.