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 can define routes to use for your web interface. These routes are assigned with the web middleware group, this provides features like session state and CSRF protection. The routes you define in routes/api.php are stateless and assigned the api middleware group.

We can start 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, to access the route given below, you can navigate 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. Consider the code 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 a route just returns a view, you can use the Route::view method. Like the redirect method, it provides a simple shortcut so there is no need to define a full route or controller. The view method will accept a URI as its first argument and a view name as the second. Moreover, you can 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 () {
    //
})->name('profile');

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