2.5 Routes, Views and Controllers Class Notes
2.5 Routes, Views and Controllers Class Notes
1. Basic Routing
2. Routing Analogy
4. Routing(cont.)
5. Closures vs Controllers
6. The RouteServiceProvider
8. Routing Mechanism
9. Named Routes
•A Route in Laravel is the first point of interaction with your application from the web (or API) end.
•At the most basic level, the Route connects requests sent to your application to the relevant section of your application as determined by your
rules as the application developer
2. Routing Analogy
The basic form of a Route is a function in a class that takes two parameters
• If there is no route defined matching the incoming request, that request is denied and an appropriate error is thrown.
4. Routing(cont.)
Routing in Laravel allows you to route all your application requests to its appropriate controller.
All routes in Laravel acknowledge and accept a URI (Uniform Resource Identifier) along with a closure.
5. Closures vs Controllers
Route::get('/', function()
return View::make('home.index');
});
6. The RouteServiceProvider
•One of the most important service providers in your application is the App\Providers\RouteServiceProvider.
•This service provider loads the route files contained within your application's routes directory.
•Once the application has been bootstrapped and all service providers have been registered, the Request will be handed off to the router for
dispatching.
7. The web.php Directory
All the routes in Laravel are defined within the route files that you can find in the routes sub-directory.
Web.php is our root sub directory
These route files get loaded and generated automatically by the Laravel framework.
8. Routing Mechanism
i.First of all, you have to create and run the root URL of your project.
ii.The URL you run needs to be matched exactly with your method defined in the root.php file(the controller), and it will execute all related
functions.
iii.The function invokes the template files. It then calls the view() function with the file name located in resources/views.
9. Named Routes
•Named routes allow the convenient generation of URLs or redirects for specific routes.
•You may specify a name for a route by chaining the name method onto the route definition:
Route::get('/user/profile', function () {
//
})->name('profile');
•Instead of defining all of your request handling logic as closures in your route files, you may wish to organize this behavior using "controller"
classes.
• Controllers can group related request handling logic into a single class.
•
• or example, a UserController class might handle all incoming requests related to users, including showing, creating, updating, and deleting
users.
•
•By default, controllers are stored in the app/Http/Controllers directory.
11. Controller Definition & Registration in Route File
To create a Controller:
•Open the PHP artisan command terminal and type the following :
php artisan make:controller ControllerName
12. Normal and Resource Controllers
•Because of this common use case, Laravel resource routing assigns the typical create, read, update, and delete ("CRUD") routes to a controller
with a single line of code.
• A Resource controller may be created using the Artisan command's - -resource option to quickly create a controller to handle these actions:
•
•php artisan make:controller PhotosController - -resource
•This command will generate a controller at app/Http/Controllers/PhotosController.php.
•The controller will contain a method for each of the available resource operations.
• Next, you may register a resource route that points to the controller:
•
•use App\Http\Controllers\PhotosController;
•Route::resource('photos', PhotosController::class);
•To get a quick overview of your application's routes, run the :
• > php artisan route:list
•> php artisan optimise:clear
13. Actions Handled By Resource Controller