0% found this document useful (0 votes)
3 views

2.5 Routes, Views and Controllers Class Notes

The document provides class notes on routing, views, and controllers in the Laravel PHP framework, detailing the routing mechanism and the role of controllers in handling requests. It explains the basic routing concept, the use of closures versus controllers, and how to define and register routes in the application. Additionally, it covers resource controllers and the actions they handle, along with the importance of the RouteServiceProvider and the web.php directory for route management.

Uploaded by

shoibbasil62
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

2.5 Routes, Views and Controllers Class Notes

The document provides class notes on routing, views, and controllers in the Laravel PHP framework, detailing the routing mechanism and the role of controllers in handling requests. It explains the basic routing concept, the use of closures versus controllers, and how to define and register routes in the application. Additionally, it covers resource controllers and the actions they handle, along with the importance of the RouteServiceProvider and the web.php directory for route management.

Uploaded by

shoibbasil62
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

2.

5 Routes, Views and Controllers Class notes

Site: TUM E-Learning Portal Printed by: WAMBUA NZIOKI ONESMUS


Course: Laravel PHP Framework (TG) Date: Tuesday, 12 December 2023, 2:55 PM
Book: 2.5 Routes, Views and Controllers Class notes
Table of contents

1. Basic Routing

2. Routing Analogy

3. What is Routing in Laravel?

4. Routing(cont.)

5. Closures vs Controllers

6. The RouteServiceProvider

7. The web.php Directory

8. Routing Mechanism

9. Named Routes

10. Laravel Controllers

11. Controller Definition & Registration in Route File

12. Normal and Resource Controllers

13. Actions Handled By Resource Controller


1. Basic Routing

•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

•You can think of it as the “doorman” that also doubles up as an usher.


•However, our doorman is much more powerful than just opening doors and showing the way.
3. What is Routing in Laravel?

The basic form of a Route is a function in a class that takes two parameters

•an intended destination


•and a closure/callback or an array containing a class and a function name that resides within the class.
•The route effectively checks the request, confirm its method (POST, GET, DELETE, PATCH, etc.) and decides if the request can be sent to its
intended destination or not.

• 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

•Routes can either use closures or Controllers.


•Route with Closure Example
<?php

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

The routing mechanism takes place in three different steps:

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');

•You may also specify route names for controller actions:



•Route::get(
• '/user/profile',
• [UserProfileController::class, 'show'])->name('profile');
10. Laravel Controllers

•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

•Registration IN Route File:


•use App\Http\Controllers\UsersController;

•Definition in Route File


•Route::get('/user', [UsersController::class, ‘index']);

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

Verb URI Action Route Name

GET /photos index photos.index

GET /photos/create create photos.create

POST /photos store photos.store

GET /photos/{photo} show photos.show

GET /photos/{photo}/edit edit photos.edit

PUT/PATCH /photos/{photo} update photos.update

DELETE /photos/{photo} destroy photos.destroy

You might also like