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);
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 () {
return view('welcome');
});
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):
php artisan make:controller <ControllerName>
• Example: php artisan make:controller AdminController
– Here controller class AdminController will create a controller file
with name AdminController at the
app/Http/Controller/AdminController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class AdminController extends Controller
{
//
}
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