Laravel Introduction
Laravel is a framework of PHP. It is designed to make web development easier and more efficient. Laravel is a robust PHP web framework which speeds up development for developers learning PHP or working on it. The beauty of Laravel is its organized code and division of code into Model-View-Controller (MVC). MVC is a programming architectural paradigm useful for organisation of the large program.
You will be reading about the main and very important concepts of Laravel here. They are top 5 e.g. Request Lifecycle, Service Provider, Router, Service Container, and Facades. These are the main building blocks of Laravel that make it easy to learn. Anyone, looking for mastering web development must learn Laravel from experts. This blog is an ideal to learn it.
Important Features of Laravel
A summary of these top 5 components which make Laravel easy to learn is provided below.
1. Request Lifecycle
The request lifecycle is a series of phases through which every request enters in a Laravel application. It begins at the application’s entry point, which is often public/index.php. This entry point proceeds through a number of steps before responding to the user.
Laravel always create an instance of the application/server container whenever it initializes the application. This is where Laravel starts to handle the request.
// public/index.php
$app = require_once __DIR__.'/../bootstrap/app.php';
2. Routing
Router will receive the request for dispatch soon after:
- Following the bootstrapping of the application
- Registration of all service providers.
By running any middleware unique to a certain route, the router will forward the request to a route or controller.
A practical method for screening or filtering HTTP requests coming into your application is provided by middleware. We can use an auth middleware on a few routes to check if user is authenticated or not. If this user is not authenticated then this middleware will redirect user to any desired route.
Certain middleware like those specified in the $middleware attribute of an application, are allocated to every route inside the application.
3. Service Container
It is very effective technique for handling class dependencies in Laravel. You can execute dependency injection (DI) is the Service Container using it.
A method for achieving loose coupling between classes is called dependency injection. Dependency injection or DI provides necessary dependencies instead of hardcoding them.
Service Container feature in Laravel facilitates centralized dependency management. It offers easier to maintain and test the code.
How the Service Container Works?
When you map a service to an identifier, you don’t need to create a new instance manually. Switching out implementations or managing intricate dependencies is made simple by this.
Binding Services to the Container
Binding services to the container is a fundamental concept in Laravel’s DI system. After defining the relationships between components and related dependencies you can easily manage the creation and lifecycle of objects through the container.
Key concepts and approaches:
- Service Container: It is responsible for managing the lifecycle of objects.
- Binding: Registering a class or closure with the container.
- Resolving: Retrieving an instance of a bound class from the container is called resolving.
Binding Methods
There are multiple binding methods available in Laravel. I will explain one by one with simple examples.
bind() Method
Registers a class or closure with a specific name or interface.
Example:
App::bind('MyService', function () {
return new MyService();
});
singleton() Method
This method is very useful to ensuring not more than one instance is created.
Example:
App::singleton('MyService', MyService::class);
instance() Method
Directly binds an instance of a class to the container.
Example:
$service = new MyService();
App::instance('MyService', $service);
extend() Method
Extends an existing binding by modifying the closure used to resolve the instance.
Example:
App::extend('MyService', function ($service) {
// Modify the service instance
return $service;
});
Automatic Binding
Laravel automatically binds classes that are annotated with @Injectable to the container.
Example:
namespace App;
use Illuminate\Contracts\Container\Container;
class MyService
{
public function __construct(Container $container)
{
// Use the container to resolve dependencies
}
}
Resolving Services
Use the app() helper function or the container instance to resolve services.
Example:
$service = app('MyService');
4. Service Provider
Service providers are responsible for registering services, components, and configurations with the application’s service container. Because these are executed at the time of bootstrapping process, so customizing and extending its behavior is very easy.
Key Concepts and Functions:
- Bootstrapping: Service providers are registered with the service container and executed during the application’s bootstrapping process.
- Registration: Service providers register services, components, and configurations with the service container.
- Configuration: Service providers can modify the application’s configuration.
- Aliases: Service providers can define aliases for classes or interfaces.
- Facades: Service providers can register facades for classes, providing a convenient way to access them.
Making a Service Provider:
- Put a new class in app/Providers directory.
- Extend the Illuminate\Support\ServiceProvider class.
- Implement the register and boot methods.
Example:
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class MyServiceProvider extends ServiceProvider
{
/**
* Register
services.
*
* @return void
*/
public function register()
{
// Register
services with the container
$this->app->singleton('my_service', function () {
return new MyService();
});
}
/**
* Bootstrap services.
*
* @return void
*/
public function boot()
{
// Perform additional bootstrapping tasks
}
}
The Boot Method
The boot method in Laravel’s service providers is a crucial part of the application’s bootstrapping process. It’s executed after all services have been registered with the container. It allows you to perform tasks that rely on other services or configurations being in place.
5. Facades
A Laravel facade is a class that gives services inside the container an interface that looks static. The documentation claims that these facades act as a stand-in for the underlying container services implementation.
In my words – it is an interface which enable us to access all the complex classes putting in in-depth path.
How Facades are implemented in Laravel
Facades in Laravel provide a convenient way to access classes without directly depending on them. They act as static proxies, forwarding method calls to underlying instances of the actual classes.
Implementation Process:
- Class Definition: The facade class is defined as a static class, usually residing in the Illuminate\Support\Facades namespace. It extends the Illuminate\Support\Facades\Facade base class.
- Method Calls: When a method is called on the facade, it’s intercepted and forwarded to the underlying class instance.
- Underlying Instance: The facade obtains the underlying class instance using the getFacadeRoot() method. It is typically implemented within the concrete class.
- Method Invocation: The facade then calls the corresponding method on the underlying instance, passing along any arguments.
- Return Value: The return value of the method call is returned to the caller.
As was previously discussed, Laravel makes services more readable for developers by using facade classes. To do the identical task, we may write the following code by utilizing a facade class:
The basic class Illuminate\Support\Facades\Facade will be extended by the facades included with Laravel and any custom facades you design.
The _callStatic() magic-method is utilized by the Facade base class to forward calls from your facade to an object. That should has been resolved from the container. The Laravel cache system is called in the example below. Looking at this code, one could think that the Cache class’s static get function is being called:
A summary of Façade contribution to Laravel is listed below:
- Facades provide a clean and concise syntax for accessing classes.
- By abstracting away the dependency, making the code more maintainable and easier to understand.
- Facades are used using base class i.e. Illuminate\Support\Facades\Facade.
- The getFacadeAccessor() method of the facade confirms the identity of the underlying class.
- Facades can be used to access any class in the application, including service providers, controllers, and models.
The main building blocks of Laravel are Service Provider, Façade, Container, Request Lifecycle, Router, etc.
Are you new to Laravel and want to learn complete website development?
ADMEC’s certified courses can help you!
Author Introduction
Hi there! This is Shivani. I’m a web developer by profession and also a student at ADMEC Multimedia Institute. I’ve learned website designing under 12 month long diploma in web design and development under the guidance of Ravi Sir. I hope this blog clears the basics related to Laravel to you. Please let me know in comments section if you need any help.
For professional level of training, you can consider becoming a part of ADMEC’s web development courses. Thanks for reading.