What Are the 4 Main Route Types Used In Laravel and How to use them?

In this tutorial, we will discuss about the different Route Types Used In Laravel.

1. Basic Routing

Basic routing in Laravel accept a URI and a Closure, providing a very expressive and simple way of defining routes.

Route::get('foo', function () {
    return 'Hello World';

Every Laravel route is defined in route files, these are located in the routes directory. Therefore, the files will automatically load by the Laravel framework. The routes/web.php file defines routes that are for your web interface. These routes are assigned with the web middleware group, this provides features like session state and CSRF protection. The routes defined in routes/api.php file are stateless and are assigned the api middleware group.

Mostly, you can begin by defining routes in your routes/web.php file. You can define routes in this file, and access by entering the defined route’s URL in your browser. For example, you may access the following route by navigating to http://your-app.test/abc in your browser:

Route::get('/abc', 'UserController@index');

2. Router Methods

You can define a basic route using the methods shown below.

Route::get($uri, $callback);
Route::post($uri, $callback);
Route::put($uri, $callback);
Route::patch($uri, $callback);
Route::delete($uri, $callback);
Route::options($uri, $callback);

3. Redirect Routes

A route that redirects to another URI, is called a Redirect Route. You can use the Route::redirect method for this. The method provides a convenient shortcut so that you may not have to define the entire route or controller for performing a simple redirect. We can use it as shown below:

Route::redirect('/user', '/there');

By default, Route::redirect returns a 302 status code. However, you can customize the status code using the optional third parameter:

You can also customize the status code using an optional third parameter.

Route::redirect('/here', '/there', 301);

By default, the Route::redirect method gives back a 302 as the status code. The above code returns a 301 status code.

4. View Routes

If your route only needs to return a view, you may use the Route::view method. Likewise the redirect method, this method provides a simple shortcut so that you do not have to define a full route or controller. The view method accepts a URI as its first argument and a view name as its second argument. In addition, you may provide an array of data to pass to the view as an optional third argument:

A route that just requires to return a view can be defined using the Route::view method. Similar to the redirect method, it gives a simple shortcut that won’t require defining a full controller or route.

The Route::view method takes in a URI as the first argument and name of the view to return as the second. Moreover, you can provide an array of data that can be passed to the view that will be returned. This can be the third argument.

Route::view('/home', 'Welcome to Scratch Coding');
Route::view('/home', 'Welcome to Scratch Coding', ['name' => 'Laravel']);

5. Named Routes

Named routes is also one of the route types used in Laravel. It enables redirects for specific routes or convenient generation of URLs. You can specify a name for a route by chaining the name method onto the route definition as shown below:

Route::get('home/profile', function () {

In addition, you may also specify route names for controller actions:

Route names can also be specified as a controller action.

Route::get('home/profile', 'UserProfileController@show')->name('profile');

Leave a Comment