Professional Documents
Culture Documents
Routes, View, Controller
Routes, View, Controller
• Example:
• 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:
Usage:
• 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):
}
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