Laravel

Laravel Routes with Example

January 30, 2018

author:

Laravel Routes with Example

# Laravel 5.6 Routing Overview

All application routes (or paths) are stored in the Routes directory amidst respective functional files. Routing is probably the first step of developing the web application. It serves as a quick road map of the application. Routes folder has various files like web.php, api.php, channels.php and console.php

• Web.php

This stores HTTP routes used on the www. It uses web middleware grouping. This enables CSRF protection, session states and cookie encryption.

• Api.php

It uses API middleware grouping. These are stateless URIs and uses a token authentication mechanism to validate the user requests as no session data is allowed.

• Console.php

This file stores console-based entry points to the application, restricting the HTTP requests.

• Channels.php

This file is used to register event broadcasting channels that application must support.

Since we will majorly focus on developing a web application with Laravel in this course, let us see the supported HTTP methods that are supported by the syntax:

    REQUEST TYPE   FUNCTION
    GET   Request data
    POST   Submit data
    PUT/PATCH   Update data
    DELETE   Delete data

As we have discussed earlier, Laravel is inspired by various frameworks and development practices. Thus, it really understands what it takes to create a successful product. And that is how we have got routing structure URI that is SEO friendly.

In Hello World application, we had created a simple GET route and displayed data in multiple ways. In this section, we will try and create multiple routes.

But firstly, create a fresh Laravel application:

laravel new learnLaravel

Try running it with:

php artisan serve

# Creating Routes in Laravel

Before creating our required routes, let us have a quick look at the Laravel routing syntax:

Route::HTTPReq($uri, $callback);

Now, we can change HTTPReq, $uri and $callback as per requirements here:

In routes/web.php

# routes/web.php

Route::get('about', function () {
    return 'about page goes here';
});

Route::post('course/laravel', function () {
    // logic here
});
Route::put('course/laravel', function () {
    // logic here
});
Route::delete('course/laravel', function () {
    // logic here
});

No magic involved. As seen above, we just created about page route and some dummy routes. You can test the routes yourself in the browser:

http://localhost:8000/about
Laravel Route Overview

Conclusion:

Since route is one of the integral part of Laravel framework, it is important to have a good understanding of it. We created a working example for about page in this post. In the coming segment, we will take routes a step further.

Leave a comment

Your email address will not be published. Required fields are marked *