Routing in laravel

Routing is one of the first step you would have to take care of while building any project. Routing is basically making the connection of controller with uri or view with uri. URI stands for uniform resource identifier and it is the path which you see on the top of your browser or the url of your website.

If you don’t know the basics of laravel, I have written a blog on that also, do have a look at it here:

Routing File

All the routings of your website have to be registred in one of the file in directory of “routes”. By defaults, all the routings are mapped with the “web.php” file. I will be telling you how to make separate routes files for different sections of your project later in the blog.

If you want to directly show a view file without creating any controller file, you can do it like this or if you want to pass some data to the view file pass in the callback function. But it is not advisable to do this as it breaks the “mvc” pattern rules.

There are several ways of binding a controller method to the route.

  • (Latest) Route::get(‘xyz’, [Controller::class, “index”]);
  • Route::get(‘/xyz’, ‘Controller@index’);


  • GET
  • POST
  • PUT

Route Parameters

There are situations where you want to pass id or any slug to the url with which you can identify the item selected. In that case, you will have to pass the parameter so for passing that you have to register the route with parameter.

Register the route like this:

For optional parameter where you are not sure whether you will pass or not use this:

To get the value of parameter in the route callback function, you can do like this:

To get the value of parameter in the controller method, you can do like this:

You can pass as many parameters as you want, those parameters will be injected in the order they are :

Redirect route

If you want to redirect from one route to another route, you can use this:

Middleware on route

If you want to apply middleware on a route, you can do it like this:

Route Grouping

If you want to apply middlewares on multiple routes or you want to apply some common settings to the route like naming of route or route common uri, you can do it using route groups like this:

Handling routing of different sections

Suppose you have three different parts of your project/website i.e

  • Student
  • Teacher
  • Admin

You can create separate routing files in route directory and can register them in the “RouteServiceProvider” file found in the App\Providers directory.

I’ll be telling you how to separate “admin” routes and other file which is automatically present is the“ web.php” file.

Create a file named “admin.php” in the routes directory.

Now go to the “RouteServiceProvider” file and you will find a boot method and you have to register your route within $this->routes() method like this:

You can find all the code here:

If you like the blog, do share it and hit the clap icon and if you want to learn more about laravel basics follow me.




WEB LEARNER | WEB3 Enthusiast

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Ecosystem Expansion: Cronastic on Cronos

Writing better Elm code with simple List transformations

Setting up SSH connection to Ubuntu from Windows using Putty Public Key Based Authentication

IGO(Initial Grill Offerings) with BarbecueSwap Finance 🍖

The Difference Between a Forecast and a Commitment

How to Generate PDF Documents in Python

Fun Commands in linux —

5 Key Points to become a Internet Of Things Expert

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Dikshant Rajput

Dikshant Rajput

WEB LEARNER | WEB3 Enthusiast

More from Medium

Password and Confirm Password Validation in Laravel

Laravel Advance | Laravel 7/6 Cron Job Task Scheduling Tutorial

筆記 - Laravel Request Lifecycle

How to generate custom error page in Laravel