Home >>Laravel Tutorial >Laravel Routing Parameter

Laravel Routing Parameter

What is Laravel routing and How Larvel Routing Works ?

Routing is one of the essential and can consider as the core components of the Laravel framework, in In Laravel, all requests are outlined with the guidance of routes. All Laravel routes are determined in the file located as the app/Http/routes.php file, which is automatically stored and loaded by the framework. Primary routing named as basic routing routes the call may be referred as a request to the associated controllers. This laravel routing tutorial addresses routing in Laravel.

Routing in Laravel comprises the following categories.

  • Basic Routing
  • Route parameters
  • Named Routes

Basic Routing

All the application routes registered itself within the app/routes.php file. This file shows Laravel for the URIs it should counter to and the associated controller will dispense it a correct call.A very basic routes are expressed as follows-

Route: :get('/', function () 
{
return 'You are welcome to the Laravel Site.';
});

  It accepts two parameters URL and Closure. These are the most straightforward routes determined as they don't need you to pass to anything other than the corresponding view. You don't require to log in or give any laravel routing parameters in their routes description or definition.

  Example Perceive the subsequent case or instance to learn more about Routing − app/Http/routes.php

<?php
Route::get('/', function () {
return view('welcome');
});

Welcome blade

resources/view/You are welcome to the Laravel Site.blade.php
<!DOCTYPE html>
<html>
<head>
<title>Laravel</title>
<link href = "https://fonts.googleapis.com/css?family=Lato:100" rel = "stylesheet"
type = "text/css">

<style>
html, body {
height: 100%;
}
body {
margin: 0;
padding: 0;
width: 100%;
display: table;
font-weight: 100;
font-family: 'Lato';
}
.container {
text-align: center;
display: table-cell;
vertical-align: middle;
}
.content {
text-align: center;
display: inline-block;
}
.title {
font-size: 96px;
}
</style>
</head>

<body>
<div class = "container">

<div class = "content">
<div class = "title">Laravel 5.1</div>
</div>

</div>
</body>
</html>

 Let us now know and understand how laravel routing works and the steps included in the routing mechanism in detail −

Step 1 − originally, we should perform the root URL of the application.

Step 2 − Forthwith, the executed URL should meet with the relevant system or method in the route.php file. In the existing or present case, it should equal the method and the root (‘/’) URL, will perform the related function.

Step 3 − the function denominates or call the template file located and named as resources/views/You are welcome to the Laravel Site.blade.php. Next, the function calls the view() function with argument ‘welcome’ without utilizing the blade.php, will generate the HTML output.  

Route Parameters

Usually, in the application, we aim to apprehend the laravel routing parameter enacted or pass with the URL. To achieve such functionality, we need to transform the code in routes.php file respectively.

There are possibly two processes by which we can catch the parameters transferred with the URL.   You can catch the parameters in routes.php file in two ways as considered below.  

Required Parameters

These parameters should be mandatorily apprehended for routing the web application.

For instance, it is necessary to catch or capture the user’s credentials or identification number from the URL.

it can be feasible through determining route parameters as shown below.  

Route::get('ID/{id}',function($id){

  echo 'ID: '.$id;

});

 

Optional Parameters

It may be possible that developers need to produce parameters as optional and it is probable with the embodiment, Following the parameter name in URL.

Retaining the default value specified as a parameter name is essential. Mark at the next example that explains how to determine an optional parameter −   Route::get('user/{name?}', function ($name = 'laravel routing parameter explanation') { return $name;});

The example shown above determines or check if the value matches to laravel routing parameter explanation and accordingly routes to the defined URL.  

Regular Expression Constraints

You can or may restrain the setup of your route parameter applying the where method on a route instance.

The where method takes the given name of the parameter including a regular expression describing how the parameter should be compelled or constraints:  

Route::get('user/{name}', function ($name) {
    //
})
->where('name', '[A-Za-z]+');

Route::get('user/{id}', function ($id) {
    //
})
->where('id', '[0-9]+');

Route::get('user/{id}/{name}', function ($id, $name) {
    //
})
->where(['id' => '[0-9]+', 'name' => '[a-z]+']);

 

Global Constraints

laravel routing tutorial will lead to understanding the necessary Global Constraints thus If you would like a route parameter always to be constrained by a proffered regular expression, you may use the pattern method.

You should describe these patterns in the boot method of your RouteServiceProvider:  

/**

 * Define your route model bindings, pattern filters, etc.

 *

 * @param  \Illuminate\Routing\Router  $router

 * @return void

 */

public function boot(Router $router)

{

    $router->pattern('id', '[0-9]+');

 

    parent::boot($router);
}

Once the pattern has been determined, it is automatically applied to all routes utilizing that parameter name:

Route::get('user/{id}', function ($id) {

    // Only called if {id} is numeric.

});

Named Routes

Named routes provide a suitable method of generating paths of routing. The chaining of routes can be designated applying name method onto the route description.

The resulting code confers an example for producing named routes with the controller.

The above explanation will lead you to understand how laravel routing works also the core Laravel Php framework. Here is the 6th Session of Laravel Tutorial, now click on next page


No Sidebar ads