Table of Contents
1. Create Routes in Laravel
2. The Routing Mechanism in Laravel
3. Route Parameters
4. Required Parameters
5. Optional Parameter
1. Create Routes in Laravel
All the routes in Laravel are defined within the route files that 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 request looks something like this:
Route:: get ('/', function () {
return 'Welcome to index';
});
Route:: post('user/dashboard', function () {
return 'Welcome to dashboard';
});
Route:: put('user/add', function () {
//
});
Route:: delete('post/example', function () {
//
});
2. The Routing Mechanism in Laravel
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
<html>
<head>
<title>Laravel5 Tutorial</title>
</head>
<body>
<h2>Laravel5 Tutorial</h2>
<p>Welcome to Laravel5 tutorial.</p>
</body>
</html>
3. Route Parameters
In many cases, within your application, a situation arises when you had to capture the parameters send ahead through the URL. For using these passed parameters effectively, in Laravel, you have to change the routes.php code.
Laravel provides two ways of capturing the passed parameter:
4. Required Parameters
At times you had to work with a segment(s) of the URL (Uniform Resource Locator) in your project. Route parameters are encapsulated within {} (curly-braces) with alphabets inside. Let us take an example where you have to capture the ID of the customer or employee from the generated URL.
Example:
Route :: get ('emp/{id}', function ($id) {
echo 'Emp '.$id;
});
5. Optional Parameter
There are many parameters which 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 name of the parameter.
Example:
Route :: get ('emp/{desig?}', function ($desig = null) {
echo $desig;
});
Route :: get ('emp/{name?}', function ($name = 'Guest') {
echo $name;
});
0 Comments
CAN FEEDBACK
Emoji