filmov
tv
#13 Laravel 6 Basics | Controllers (IV) | Dependency Injection & Controllers | Route Caching
Показать описание
In Part-13 of Laravel 6 Basics, we will keep learning Controllers in detail.
We will keep experimenting with lot of functionalities in new Laravel 6 website and in E-com series.
In this video, we will learn Dependency Injection & Controllers and Route Caching.
Dependency Injection & Controllers
Dependency Injection, as the name suggests, is a technique where one object is depended on another or one class depended on another to complete its execution. One inject its properties to another and this process is called Dependency Injection.
Laravel service container is a powerful tool for managing this class dependencies and performing dependency injection. Class dependencies are "injected" into the class via the constructor or, in some cases, "setter" methods.
Like we want to retrieve the users from users table in few of our methods in controller. So, we can inject users into the constructor so that we can call them in any of the method in the controller.
1) We will create users table and Users model if not created yet.
2) We will create __construct function in UsersController in which we will get the users from User model
3) Add statement at top of UsersController to include User model.
use App\User;
4) We will create index function to find particular user details from list of users retrived at constructor. So we are injecting users into constructor to use in any method of UsersController.
Route Caching
Route Caching is useful when application is using controller based routes. Route cache will drastically decrease the amount of time to register all application's routes. In some cases, route registration may even be up to 100x faster. To generate a route cache, just execute below artisan command:
php artisan route:cache
If we add any new routes, sometimes we require to clear the route cache that is possible with below artisan command :-
php artisan route:clear
We will keep experimenting with lot of functionalities in new Laravel 6 website and in E-com series.
In this video, we will learn Dependency Injection & Controllers and Route Caching.
Dependency Injection & Controllers
Dependency Injection, as the name suggests, is a technique where one object is depended on another or one class depended on another to complete its execution. One inject its properties to another and this process is called Dependency Injection.
Laravel service container is a powerful tool for managing this class dependencies and performing dependency injection. Class dependencies are "injected" into the class via the constructor or, in some cases, "setter" methods.
Like we want to retrieve the users from users table in few of our methods in controller. So, we can inject users into the constructor so that we can call them in any of the method in the controller.
1) We will create users table and Users model if not created yet.
2) We will create __construct function in UsersController in which we will get the users from User model
3) Add statement at top of UsersController to include User model.
use App\User;
4) We will create index function to find particular user details from list of users retrived at constructor. So we are injecting users into constructor to use in any method of UsersController.
Route Caching
Route Caching is useful when application is using controller based routes. Route cache will drastically decrease the amount of time to register all application's routes. In some cases, route registration may even be up to 100x faster. To generate a route cache, just execute below artisan command:
php artisan route:cache
If we add any new routes, sometimes we require to clear the route cache that is possible with below artisan command :-
php artisan route:clear
Комментарии