Controllers
Controllers can group related request handling logic into a single class. Instead of defining all of your request handling logic as Closures in route files, you may wish to organize this behavior using Controller classes.
Controllers are stored in the app/Http/Controllers directory.
Controller extends the base Controller class included with Laravel.
Defining Controller Class
Run Command:- php artisan make:controller
Example:- php artisan make:controller AboutController
Path:- app/Http/Controllers/AboutController.php
Creating Route for Controller Class
Path of Controller :- app/Http/Controllers/AboutController.php
Path of Route:- routes/web.php
Syntax:- Route::get(‘uri’, [ControllerName::class, ‘method_name’]);
Passing Data from Controller to View
Path of Controller :- app/Http/Controllers/AboutController.php
Path of Route:- routes/web.php
Path of View:- resources/views/aboutme.blade.php
Multiple Methods inside Controller
Path of Controller :- app/Http/ControllersAboutController.php
Path of Route:- routes/web.php
Path of aboutme blade file:- resources/views/aboutme.blade.php
Path of aboutyou blade file:- resources/views/aboutyou.blade.php
Single Action Controller
If you would like to define a controller that only handles a single action, you may place a single __invoke method on the controller.
Run Command:- php artisan make:controller ShowAbout –invokable
Path of Controller :- app/Http/Controllers/ShowAboutController.php
Path of Route:- routes/web.php
Path of aboutme blade file:- resources/views/aboutme.blade.php
Tagged : laravel / Php