filmov
tv
#10 Laravel 6 Basics | Controllers | Namespaces | Single Action Controllers | Controller Middleware

Показать описание
In Part-10 of Laravel 6 Basics, we will start learning about 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 about Basic Controllers and Controller Middleware.
Introduction
Controllers are meant to group associated request handling logic within a single class.
Controllers are required when there are some dynamic features in website that we can't handle with routes and blade files alone. We require to make some functions in Controllers files to handle its logics like we have done in our E-com website. Controllers are stored in the app/Http/Controllers directory.
Basic Controllers
Defining Controllers
We have created lot of controllers in E-com, Dating and Laravel 6 Basics series.
We will take below steps to define the controller :-
i) Run below artisan command to create ProductsController like below :-
php artisan make:controller ProductsController
(Above command will create ProductsController at app/Http/Controllers/ folder. In this controllers files, we will create functions or methods to handle website logics.)
ii) Create Get route to this controller action like below:
iii) Create product function in ProductsController
Controllers & Namespaces
Namespaces can be called as collection of classes.
We can add Namespace if we have two classes with same name or we need to store classes separately.
Like we have made Admin namespace for admin classes.
Single Action Controllers
Single Action Controllers are those who only have single function as name suggests and that needs to be defined with '__invoke' name.
When registering routes for single action controllers, we are not required to specify function __invoke as it will be automatically called with Controller.
We can create Single Action Controller with below artisan command with --invokable option:
php artisan make:controller PagesController --invokable
Route for the Single Action Controller will be like below with Controller name only like PagesController without any method/action to specify.
Route::get('page/{id}', 'PagesController');
Controller Middleware
Middleware can be assigned to the controller's routes in route files as shown in video.
There is another way to assign middleware to any controller by adding __construct function in controller file and assign middleware in it.
We will keep experimenting with lot of functionalities in new Laravel 6 website and in E-com series.
In this video, we will learn about Basic Controllers and Controller Middleware.
Introduction
Controllers are meant to group associated request handling logic within a single class.
Controllers are required when there are some dynamic features in website that we can't handle with routes and blade files alone. We require to make some functions in Controllers files to handle its logics like we have done in our E-com website. Controllers are stored in the app/Http/Controllers directory.
Basic Controllers
Defining Controllers
We have created lot of controllers in E-com, Dating and Laravel 6 Basics series.
We will take below steps to define the controller :-
i) Run below artisan command to create ProductsController like below :-
php artisan make:controller ProductsController
(Above command will create ProductsController at app/Http/Controllers/ folder. In this controllers files, we will create functions or methods to handle website logics.)
ii) Create Get route to this controller action like below:
iii) Create product function in ProductsController
Controllers & Namespaces
Namespaces can be called as collection of classes.
We can add Namespace if we have two classes with same name or we need to store classes separately.
Like we have made Admin namespace for admin classes.
Single Action Controllers
Single Action Controllers are those who only have single function as name suggests and that needs to be defined with '__invoke' name.
When registering routes for single action controllers, we are not required to specify function __invoke as it will be automatically called with Controller.
We can create Single Action Controller with below artisan command with --invokable option:
php artisan make:controller PagesController --invokable
Route for the Single Action Controller will be like below with Controller name only like PagesController without any method/action to specify.
Route::get('page/{id}', 'PagesController');
Controller Middleware
Middleware can be assigned to the controller's routes in route files as shown in video.
There is another way to assign middleware to any controller by adding __construct function in controller file and assign middleware in it.
Комментарии