Download as pdf or txt
Download as pdf or txt
You are on page 1of 20

Web Technologies

Routing and Controllers


Today’s Lecture
• Routing and Controllers
– Routes
• HTTP Methods
– View
• Passing Data to Views
– Controllers
• Creating a Controller
Routes
• In Laravel, routes are the core component that defines how
incoming HTTP requests are handled by our application.
• They act as the traffic map, directing each request to the
appropriate controller action based on the request's URL and HTTP
method.
• Without routes, we have no ability to interact with the end user.
• All Laravel routes are defined in the routes files, which are in the
routes folder.
• Default installation of Laravel comes with two routes, one for the
web and other for API.
– They are created in web.php file for websites (web interface),
and api.php for APIs.
– These files are automatically loaded by our application's
App\Providers\RouteServiceProvider
Routes
Purpose
• Connect URLs to Controllers: Routes define the relationship
between a specific URL and the corresponding controller method
that should handle the request.
• Handle Different HTTP Methods: Different HTTP methods like
GET, POST, PUT, and DELETE are mapped to specific actions within
our controllers.
Basic Structure
• A typical route definition in Laravel consists of three parts:
– HTTP Methods: This specifies the type of HTTP request the
route handles (for example GET, POST, PUT, etc.).
– URI: This defines the URL pattern for the route.
– Action: This specifies the controller method or closure that
should be invoked when the route is matched.
Routes
Here is how the route for web in web.php looks like:
Routes
• Many simple websites could be defined within the web routes file.
– With a few simple GET routes combined with some templates,
we can serve a classic website easily:
Routes
• In Laravel, routes define the mapping between incoming HTTP
requests and the corresponding controller actions that handle
them.
• Laravel provides various route methods for different types of HTTP
requests and functionalities.

Basic Route Methods


• get: Handles GET requests.
• post: Handles POST requests.
• put: Handles PUT requests.
• patch: Handles PATCH requests.
• delete: Handles DELETE requests.
• options: Handles OPTIONS requests.
Routes
• GET
– It's used to read (or retrieve) a representation of a resource.
Route::get($uri, $callback);
• POST
– It’s most often utilized to create new resources.
Route::post($uri, $callback);
• PATCH
– It’s used to update (or modify) resources.
Route::patch($uri, $callback);
• DELETE
– It’s used to delete a resource identified by filters or ID.
Route::delete($uri, $callback);
Route Parameters
• Dynamic Routes:

• Capture parameters from the URL.


• Pass parameters to the controller or closure.

• Example:

Route::get('/user/{id?}/review/{com}', function ($id = null, $com) {


return “<h1>User ID : ”.$id.”<h1>”;
});

• Explanation: This route captures the id parameter from the URL and
returns it as part of the response. For example, accessing /user/1 would
return "User ID: 1".
user
Laravel Named Routes
Purpose:

• Assign a name to a route for easier URL generation.


• Use named routes in views and controllers.

Usage:

• Explanation: Named routes allow you to refer to routes using a name


instead of the URL, which makes your code more maintainable and
readable.
Laravel Routes Group
• Group routes that share common attributes like middleware or prefix.

• Explanation: Grouping routes under a common prefix (e.g., admin) makes it easier to manage and
apply shared attributes, such as middleware or URL segments.
Views
• In Laravel, views are the presentation layer of our application.
• They handle the output that is displayed to the user in response to a
request.
• Views are in the resources folder, and its path is resources/views
• In Laravel, there are two formats of views:
– PHP (welcome.php will be rendered with the PHP engine)
– Blade (welcome.blade.php will be rendered with the Blade
engine)
Views
Passing Data to Views
• Controllers pass data to views using the view helper function.
– This data can then be accessed within the view using Blade
directives or directly with PHP variables.
• Below code look for a view in resources/views/welcome.php or
resources/views/welcome.blade.php and loads its content.
– Once we return it, it’s passed on to the rest of the application and
eventually returned to the user.
Route::get('/', function () {
$name = ‘John Doe’
return view('welcome’, [‘user’ => $name ]);
});
In the document welcome.blade.php we can access this object as
<h1>{{$user}}<h1>
Can also use inside Route::
Controller
• In Laravel, controllers are the middlemen between routes and
views.
– They handle the application logic that responds to incoming
HTTP requests.
• Controllers are typically located in the app/Http/Controllers
directory.
• Each controller is a PHP class that extends the App\Http\Controller
base class.
• Instead of defining all our request handling logic as closures in our
route files, we may wish to organize this behavior by using the
"controller" classes.
• Controllers can group related request handling logic into a single
class.
• Example: a UserController class might handle all the incoming
requests related to users; including showing, creating, updating,
and deleting users.
Controller
Purpose
• Process Incoming Requests: Controllers receive requests from
routes and determine how to handle them.
• Execute Application Logic: They contain the business logic of our
application, interacting with models and other services to perform
tasks and retrieve data.
• Prepare Data for Views: They prepare and format data to be
displayed in the response view.
• Maintain Organization: Controllers group related functionalities
together, ensuring our code remains clean and organized.
Controller
Creating a Controller
• Open command prompt or terminal and type the command to create
controller using Artisan CLI (Command Line Interface):

• Example: php artisan make:controller UserController


– Here controller class UserController will create a controller file with
name UserController at the app/Http/Controller/UserController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class UserController extends Controller
{

}
Controller example
Summary of Today’s Lecture
• Routing and Controllers
– Routes
• HTTP Methods
– View
• Passing Data to Views
– Controllers
• Creating a Controller
References
• Ch-1, Ch-2, Ch-3; Laravel Up and Running, A Framework for Building
Modern PHP Apps, 2nd Edition, Matt. Stauffer, Oreilly.
• https://laravel.com/docs/10.x/routing
• https://laravel.com/docs/10.x/views
• https://laravel.com/docs/10.x/controllers

You might also like