Laravel Tutorial Index

Routing and Controllers

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, which 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 is 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/

<?php
Route:: get ('/', function () {
   return 'Welcome to index';
});

http://localhost/user/dashboard

<?php
Route:: post('user/dashboard', function () {
   return 'Welcome to dashboard';
});

http://localhost/user/add

<?php
Route:: put('user/add', function () {
//
});

http://localhost/post/example

<?php
Route:: delete('post/example', function () {
//
});

The Routing Mechanism in Laravel

The routing mechanism takes place in three different steps:

  1. First, 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. For example, you have to capture the customer's ID or employee from the generated URL.

Example:

<?php
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:

<?php
Route :: get ('emp/{desig?}', function ($desig = null) {
    echo $desig;
});
<?php
Route :: get ('emp/{name?}', function ($name = 'Guest') {
    echo $name;
});


Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram