Laravel Routing

Morris Muriuki Muthigani
73
0
laravel

05/02/2023 (1 year ago)


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.

Create Routes in 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 in Laravel

The routing mechanism takes place in three different steps:

  1. First of all, you have to create and run the root URL of your project.
  2. The URL you run needs to be matched exactly with your method defined in the root.php file, and it will execute all related functions.
  3. The function invokes the template files. It then calls the view() function with the file name located in resources/views/, and eliminates the file extension blade.php at the time of calling.

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>

Route Parameters

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.

Laravel provides two ways of capturing the passed parameter:
  • Required parameter
  • Optional Parameter

Required Parameters

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;
});

Optional Parameter

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;
});

Discussion (0)

Recent Blogs

Our Recent Blogs