Laravel Routing / Routes.php Tutorial with Examples

All MVC Frameworks provide some mechanism to define rules that are used to map different requests to corresponding controllers. In Laravel MVC Framework, we define these rules in routes.php (located in http\Http folder) file to map different URL patterns to controllers.

I assume you have created your first laravel project. Here I explain how to define mapping rules in routes.php file.

Simplest Routing Rule

Route::get('/hello-world', function () {
    return "Hello World";
});

Laravel provide a class with different static methods e.g. get, post etc. The method name tells about the type of HTTP request (get, post, ...) for which the rule is defined. The first parameter is URL Pattern, means what a user enter in the browser address bar after the context path or domain name. Here "/hello-world" pattern shows this rule would be used to process or delegate requests coming from domain-name/hello-world pattern.


The 2nd parameter can be a string or function. In above code, its function that would be executed when the request comes on /hello-world URL pattern. Function can do many useful things, but to keep the example simple, I have just returned a string. This string would be returned to browser and would be displayed as-is.

Receive URL Path Variables

HTTP request can contain parameters e.g. /show-product?id=5, in this URL the id is parameter with value 5. Such URL are not SEO friendly as also difficult for users to remember and type. So we usually perform URL rewriting so that URL like /show-product/5 can work. In Laravel, using URL Path Variables is very simple. As defined in below rule.

Route::get('/show-product/{product_id}', function($product_id){    
    return "Server Received URL Path Variable : " . $product_id;
});

You notice, its very simple, we just put a place-holder in first parameter with curly braces i.e. {} and add a variable inside function arguments. Framework extract the value placed at {product-id} location and pass that value to the request handler function. Note, the argument variable name and place holder name can be different. what matters is the order. The first place holder value would be passed to first argument. In same way you can also extract multiple URL path variables as shown in below sample code:

Route::get('/print-invoice//{customer_id/{invoice_id}', function($customer_id, $invoice_id){
    return $customer_id  . " : " . $invoice_id;
});


You can add additional parts into URL to make them more SEO friendly. e.g.
Route::get('/print-invoice//{customer_id/invoice/{invoice_id}', function($customer_id, $invoice_id){
    return $customer_id  . " : " . $invoice_id;
});


Dispatch Request to Controller

Above example are basic and not widely used in real projects. Controller are the component that process the requests. To map a request URL to some controller, we can define route like this:

Route::get('/users', 'UsersController@index');

A basic controller code is defined below, along with output:

namespace App\Http\Controllers;

use App\Http\Requests;
use Illuminate\Http\Request;

class UsersController extends Controller
{
    function index(){
        return "Index method inside the UsersController called. ";
    }
}


We instead of writing function as 2nd parameter, we use another overloaded method that received a string which contains controller class name and method name we want to call when the request is received. I would explain controllers in some separate post, as its not scope of this post.

Comments