Create Laravel Routes/Routing

In this tutorial, you will learn to create laravel routes like basic routes, redirect routes, view routes & fallback routes. Even you will learn routes with required, optional, & validated parameters.

Routing is the most important part of the web application Because of using it, you can easily create links/URLs to perform different types of functionalities. So, you must learn routing in laravel.

Learn Also –

How to Create a Laravel Project

Routes in Laravel 8

Laravel routing provides a way to define routes in route file that remains in the routes folder.

Routes file are called automatically by the framework

By default, the routes folder contains four files –

  • api.php 
  • web.php 
  • console.php 
  • channels.php 

Here, we will define all routes in the web.php because it is frequently used to develop web applications.

There are four route methods.

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

Important Points –

All these methods are very important. But here we will discuss only the Route::get($URI, $Callback)  that are used o create a complete URL

Basic Routes

Basic Routes is a very simple route that can be created with the in-build object Route & a static method get()

Syntax –

Route::get($URI, $callback){
 // write custom code here
}

The get() method must be accessed with only two parameters URI & Callback/Closure

URI stands for Uniform Resource Identifier that is the name of the URL (Uniform Resource Locator).

For example – https://www.codingstatus.com/about-us is a complete URL and  about-us is a URI

Callback/Closure is a function that has no name and It will return any things that are defined by you.

Example –

Route::get("about-us", function(){
  return "CodingStatus is the best coding website";
});

Redirect Routes

The Redirect routes are used to redirect the current URI to another URI using Route::redirect method.

Syntax –

Route::redirect('/current_URI', '/another_URI', status_code);

Important Points –

  • here, current_URI & another_URI are required but status_code is optional
  • You need not define current_URI with Route::get() method. but you will have to define another_URI with Route::get().
  • by default, redirect routes return 302 status code but you can change it with 301
  • You can use Route::permanentRedirect(“current_URI, “another_URI”) method to return 301 status code

Example –

Route::redirect('/about', '/about-us');

You can use redirect routes If you need to return the same result by calling two or more URI.

In this example, /about will be redirected to about-us to return the same result. So, you will have to define only  about-us uri with Route::get() method.

Route::get("about-us", function(){
  return "CodingStatus is the best coding website";
});

View Routes

The View Routes are used to redirect return a view (front page) using Route::view() method.

If you need to return directly view file then you can easiliy use the view routes.

Syntax –

Route::view(URI, viewName, data);

View routes method can accept only three parameters first is URI, second is viewName & third is data

  • URL – Any name of the URL
  • viewName – View file name or path that remains inside views folder.
  • data – Any data in array formate. It is optional

Example –

Suppose we have created a view file with the name profile.blade.php. Now, we have to return this file directly with Route:view() method.

File Path – views/profile.blade.php

<h3>CodingStatus is the best coding Website</h3>

Now you can return the above view file like this –

Route::view('/company-profile', 'profile');

If you need to pass third parameter then you can define it like

Route::view('/company-profile', 'profile', ['author'=>'Md Nurullah']);

Note – view, data, status, & headers are resevered are reserved words by laravel. So, You can not use these words in your route otherwise you will face an error.

Fallback Route

The Fallback route is used to return an warning message if users request a route that is not defined in route file.

By default, laravel automatically return 404 page if there are no routes match. But if you need to display a custom warning message on unmatched route request then you can use Route::fallback method.

Syntax –

Route::fallback(function () {
    // write custom code here
});

Important Points –

  • Route::fallback() method can accept only one anonymous function as the first parameter
  • You should always define fallback route after all other routes. Means that It will be last route
  • Route with Parameters

Example –

Route::fallback(function () { 
 return "You requested route is not exist in our application";
});

Laravel Routes with Parameters

Routes with parameters are defined to get dynamic data from the URL.

If you pass any data to the route as parameters then you can say it Route paramters

Parameters are always passed to the Basic Routes.

Route paramerters always covered by curly brackets like {parameterName}

Also, Route paramerters name can be declared with underscore(_) like {parameter_name}

There are two main parameters that are frequently used in web application –

  • Required Parameters
  • Optional Parameters

Required Parameters

In case of Required Parameters,  You will have to pass values of all paramters otherwise you will get an error.

If you need always to pass values of parameters then you can define required parameters

Syntax –

Syntax for passing single parameters

Route::get(URI/{parameterName}, function($parameterName){
  // write custom code here
});

Syntax for passing multiple parameters

Route::get(URI/{parameterName1}/{parameterName2}/{parameterName3}/.../{parameterName(n)}, function($parameterName1, $parameterName2, $parameterName3,..., $parameterName3 ){ 
// write custom code here 
});

Example – 1

For single parameters

Route::get(web-developer/{id}, function($id){
   return "Developer id is: ".$id;
});

You can pass value of id parameter like –

http://127.0.0.1:8000/web-developer/24
http://127.0.0.1:8000/web-developer/55
http://127.0.0.1:8000/web-developer/12
http://127.0.0.1:8000/web-developer/6

Example – 2

For multiple parameters

Route::get(web-developer/{id}/{first_name}, function($id, $first_name){
  return "Web Develper name is: ".$id." and first name is: ".$first_name;
});

You can pass value of parameters id & first_name like –

http://127.0.0.1:8000/web-developer/24/noor
http://127.0.0.1:8000/web-developer/55/sunil
http://127.0.0.1:8000/web-developer/12/nurullah
http://127.0.0.1:8000/web-developer/6/rapsan

Example – 3

You can also define route with multiple parameters like –

Route::get(web-developer/id/{id}/name/{first_name}, function($id, $first_name){
  return "Web Develper name is: ".$id." and first name is: ".$first_name;
});

You can pass value of parameters id & first_name like –

http://127.0.0.1:8000/web-developer/id/24/name/noor
http://127.0.0.1:8000/web-developer/55/name/sunil
http://127.0.0.1:8000/web-developer/12/name/nurullah
http://127.0.0.1:8000/web-developer/6/name/rapsan

Optional Parameters

In case of Optional Parameters, You can make any paramters optional which values may be pass or not according to application requirement

If you need to pass values of parameters in some requests only not in all request then you can define optional parameters

Syntax –

Syntax for passing single parameters

Route::get(URI/{parameterName?}, function($parameterName = null){
  // write custom code here
});

Syntax for passing multiple parameters

Route::get(URI/{parameterName1}/{parameterName2?}/{parameterName3?}/.../{parameterName(n)}, function($parameterName1, $parameterName2=null, $parameterName3 = null,..., $parameterName3 ){ 
// write custom code here 
});

Important Points –

You have to add question mark (?) with marameter name and also assign null value to the variable of that parameter.

Example – 1

For single parameters

Route::get(web-developer/{id?}, function($id=null){
   return "Developer id is: ".$id;
});

You can pass the value of id parameter like –

http://127.0.0.1:8000/web-developer/24
http://127.0.0.1:8000/web-developer/55
http://127.0.0.1:8000/web-developer/
http://127.0.0.1:8000/web-developer/

Example – 2

For multiple parameters

Route::get(web-developer/{id}/{first_name?}, function($id, $first_name=null){
  return "Web Develper name is: ".$id." and first name is: ".$first_name;
});

You can pass the value of parameters id & first_name like –

http://127.0.0.1:8000/web-developer/24/noor
http://127.0.0.1:8000/web-developer/55/sunil
http://127.0.0.1:8000/web-developer/12/
http://127.0.0.1:8000/web-developer/6/

Example – 3

You can also define a route with multiple parameters like –

Route::get(web-developer/id/{id?}/name/{first_name}, function($id=null, $first_name){
  return "Web Develper name is: ".$id." and first name is: ".$first_name;
});

You can pass the value of parameters id & first_name like –

http://127.0.0.1:8000/web-developer/id/24/name/noor
http://127.0.0.1:8000/web-developer/55/name/sunil
http://127.0.0.1:8000/web-developer/name/nurullah
http://127.0.0.1:8000/web-developer/name/rapsan

Routes Parameters Validation

Laravel provides a build-in where() method to validate routes parameters using regular expression.

Example –

If the id parameter has to accept only number then you can validate it like

Route::get(web-developer/{id}, function($id){
   return "Developer id is: ".$id;
})->where('id', '[0-9]+');

If the firs_name parameter has to accept the first name in lower/upper case only then you can validate it like –

Route::get(web-developer/{id}, function($id){
   return "Developer id is: ".$id;
})->where('name', '[A-Za-z]+');

If both id accepts an only number and first_name accepts the first name only in lower/uppercase then you can validate them like –

Route::get(web-developer/{id}, function($id){
   return "Developer id is: ".$id;
})->where(['id' => '[0-9]+', 'first_name' => '[a-z]+']);

Official Docs –

You can know more about Laravel Routing from its official docs.