
mmuthigani
Published on 05/30/2024
Routing is one of the essential concepts in Laravel. Routing in Laravel allows you to route all your application requests to their appropriate controller. The main and primary routes in Laravel acknowledge and accept a URI (Uniform Resource Identifier) along with a closure, given that it should have to be a simple and expressive way of routing. In this tutorial, you will learn about the routing concept of Laravel.
All the routes in Laravel are defined within the route files you can find in the routes sub-directory. These route files get loaded and generated automatically by the Laravel framework. The application's route file gets defined in the app/Http/routes.php file. The general routing in Laravel for each of the possible requests looks something like this:
http://localhost/
Route:: get ('/', function () {
return 'Welcome to index';
});
http://localhost/user/dashboard
Route:: post('user/dashboard', function () {
return 'Welcome to dashboard';
});
http://localhost/user/add
Route:: put('user/add', function () {
//
});
http://localhost/post/example
Route:: delete('post/example', function () {
//
});
The routing mechanism takes place in three different steps:
Example:
app/Http/routes.php
<?php
Route:: get ('/', function () {
return view('laravel');
});
resources/view/laravel.blade.php
<!DOCTYPE html>
<html>
<head>
<title>Laravel5 Tutorial</title>
</head>
<body>
<h2>Laravel5 Tutorial</h2>
<p>Welcome to Laravel5 tutorial.</p>
</body>
</html>
In many cases, a situation arises in your application when you have to capture the parameters sent through the URL. To use these passed parameters effectively in Laravel, you must change the routes.php code.
You sometimes had to work with a segment(s) of your project's URL (Uniform Resource Locator). Route parameters are encapsulated within {} (curly-braces) with alphabets inside. Let us take an example where you have to capture the customer's ID or employee from the generated URL.
Example:
Route :: get ('emp/{id}', function ($id) {
echo 'Emp '.$id;
});
Many parameters do not remain present within the URL, but the developers had to use them. So such parameters get indicated by a "?" (question mark sign) following the parameter's name.
Example:
Route :: get ('emp/{desig?}', function ($desig = null) {
echo $desig;
});
Route :: get ('emp/{name?}', function ($name = 'Guest') {
echo $name;
});
mmuthigani
Published on 05/30/2024
mmuthigani
Published on 02/10/2024
mmuthigani
Published on 01/12/2024