Routing in laravel

Dikshant Rajput
3 min readSep 21, 2021

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.

Route::view('xyz','abc.index');
Route::get('xyz',function(){ return view('abc.index');})

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

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

METHODS AVAILABLE

  • GET
  • POST
  • PUT
  • PATCH
  • DELETE
  • OPTIONS

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:

Route::get('xyz/{id}',[Controller::class,index]);

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

Route::get('xyz/{id?}',[Controller::class,index]);

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

Route::get('xyz/{id}',function($id){    return view('abc.index',compact('id'));});

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

public function index($id){    return view('abc.index',compact('id'));}

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

Route::get('xyz/{id}/{name}',[Controller::class,index]);

Redirect route

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

Route::redirect('/abc','/xyz');

Middleware on route

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

Route::get('abc', function(){    // do something})->middleware('auth');

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:

Route::group(['prefix'=>'abc','middleware' => ['auth','web'], 'as' =>'abc.'], function(){     Route::get('xyz/{id}',[Controller::class,index])->name('index');
Route::get('xyz',[Controller::class,store])->name('store');
})

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.

--

--