Laravel Routing – HTTP Routing in Laravel Tutorial

Routing is an important concepts in Laravel Framework. It permits us to route all our application request to its acceptable controller. In this tutorial, we are going to study routing concept in Laravel. The predominant and first routes in Laravel acknowledge and accept a URI (Uniform Resource Identifier) together with a closure, provided that it ought to should be a easy and expressive way of routing

Routing in Laravel

All the routes in Laravel are defined inside the web files that we will discover within the routes directory. These route files get loaded and generated automatically by the Laravel framework. The application’s web file will get defined within the routes/web.php file. The basic routing in Laravel for every of the possible request appears to be like something like this:

Route:: get ('/', function () {
   return 'Welcome to Tutorial';
Route:: post('user/dashboard', function () {
   return 'Welcome to dashboard';
Route:: put('user/add', function () {
Route:: delete('post/example', function () {

The code snippet above tells the browser to return the welcome view when the default URL is entered. The views are discovered within the resources/views directory. In this case, the request maps to resources/views/welcome.blade.php file. The commonest sample of defining routes is to attach the specified route to the related controller which handles the entire action taking place inside the route. For example:

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

So this associates the ‘/user’, with the ‘UserController@index’ controller. The URI (uniform resource identifier) on this case within the ‘/user’, whereas the corresponding callback function is the ‘UserController@index’ controller.

Router Method

The router allows you to register routes that respond to any HTTP verb:

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

Redirect Routes

There are completely different methods that laravel provides on the Route facade.One of them is the redirect methodology. As the method name implies,this methodology is helpful when you find yourself defining a URI that redirects to a different route. Here is how it’s used:

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

This means that in the event you attempt accessing the ‘/user’ route, you may be redirected to the ‘/admin’ route.

View Routes

Another method accessible to the Route facade is the view method. This method is simply helpful when your route only returns a view. The view method accepts two required arguments and one optionally available argument. The first is the URI you might be focusing on and the second is the name of the view you need to show.

Route::view('/user', 'user');

Routing Parameter

In many circumstances, inside your application, a situation arises once you needed to capture the parameters send forward by the URL. For using these handed parameters successfully, in Laravel, it’s important to change the routes.php code.

Laravel offers two ways of capturing the passed parameter:

  • Required Parameter
  • Optional Parameter

Required Parameter

At occasions you needed to work with a segment(s) of the URL (Uniform Resource Locator) in your project. Route parameters are encapsulated inside {} (curly-braces) with alphabets inside. Let us take an example the place it’s important to capture the ID of the customer or employee from the generated URL.

Route :: get ('emp/{id}', function ($id) {
    echo 'Emp '.$id;

Optional Parameter

There are many parameters which do not stay current inside the URL, however the developers had to make use of them. So such parameters get indicated by a “?” (question mark sign) following the name of the parameter.

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

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

Named Routes

Named routes permits a convenient way of creating routes. This is done by attaching the name method to the route definition.

Route::get('user/details', function () {

The following code shows an example for creating named routes with a controller

Route::get('user/details', ''UserController@showDetails')->name('details');

<— Previous Tutorial Next Tutorial —>


Please enter your comment!
Please enter your name here


Related Articles

Concept of Session in Laravel Tutorial

Sessions are used to store details about the user throughout the requests. Laravel supplies various drivers like file, cookie, apc, array, Memcached, Redis, and database to handle session data. By default, file driver is used as a result of it's light-weight....

Laravel Url Generation Tutorial

Our web application revolves around routes and URLs. After all, they're what direct our users to our pages. At the end of the day, serving pages is what any web application should do. Our users may...

Concept of Laravel Views Tutorial

In MVC framework, the letter "V" stands for Views. It separates the application logic and presentation logic. Views are saved in resources/views listing. Generally, the view contains the HTML which might be served by the application.
WP2Social Auto Publish Powered By :