How to create routes in Laravel

In previous blog, we see how we can make environment ready to run laravel project locally.

Before learning how to create a route in Laravel, let’s see laravel project folder structure.

In the above picture, you can see the structure of Laravel project. In the root directory, there is a folder named “routes” and inside that folder you can see web.php file.

We will create a route in this file.

Let’s see how this file looks in default.

In the picture above, you can see there is a index route added default in a Laravel project which has a callback function and that function returns view file named welcome.

Creating A New Basic Route

Route::get('/test-route',function (){
   // This will return the text below
   return "This is a test route"; 
});

To create a route you need to call Route Facade and you have to set route method. In this case this is GET method route. And there is a function which will be called when this route is triggered and the function code will execute.

Let me introduce a new php class named Controller. Let’s see how do we create a route which will trigger the function inside of a controller.

Route::get('/another-route',[\Http\Controllers\RouteController::class,'index']);

Here, I have created a new route which will now trigger a function named index which is inside Controller named RouteController. And the code inside the index function will be executed when this route is called. Don’t worry, I will cover about Controller in another blog.

Let’s see what can we do more in routes.

Name Route

We can give name to routes which can be used later in blade file or controllers. Just keep this in mind that route name must be unique.

//Using Controller
Route::get('/another-route',[\App\Http\Controllers\RouteController::class,'index'])->name('anotherRoute');

//Using Callback function
Route::get('/', function () {
    return view('welcome');
})->name('welcome');

Grouping Route

You can group the route for various purposes. Let’s see a basic example for grouping routes.

Route::group(['prefix' => 'admin','as'=>'admin.','middleware' => 'auth'],function (){
   Route::get('/dashboard',function (){
      return "This is the admin dashboard";
   })->name('dashboard');
});

Here, I have grouped a route and you can see group function that has parameter which is array and inside that array you can see prefix, as and middleware with respective value.

Now the route url will be “admin/dashboard” because we have used “admin” in prefix, and the name of this route will be “admin.dashboard” because we have used “admin.” in as and this route will be checked by auth middleware because we have used “auth” in middleware. We will learn about middleware in upcoming blog. In this way, you can create route in laravel.

Group by Controller

Similarly, we can group routes by controller.

use App\Http\Controllers\RouteController;

Route::controller(RouteController::class)->prefix('admin')->as('controller.')->middleware('auth')->group(function () {
    Route::get('/controller-group', 'index')->name('controllerGroup');
});

You can group route by controller in this way and you can use prefix, middleware and as methods as well. Now, the route url will be admin/controller-group and name will be controller.controllerGroup. You have to make sure that you have imported the RouteController in this web.php file.

Route Methods

There are six methods which you can use to create routes in Laravel.

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

Dynamic Routing / Route Parameter

You can create dynamic routes in laravel. Let’s take an example of a blog and you have show blog route which will fetch data from database on the basis of parameter taken from route. And you also can make this parameter optional.

//Required Parameter.
Route::get('/blog/{slug}', function ($slug) {
    $blog = Blog::where('slug',$slug)->first();
    return view('blog',compact('blog'));
})->name('blog');


//Optional Parameter
Route::get('/blog/{slug?}', function ($slug) {
    $blog = Blog::where('slug',$slug)->first();
    return view('blog',compact('blog'));
})->name('blog');

What’s Next

Now you have understood how you can create route in laravel. You can checkout laravel documentation for more details.

Conclusion

You can register/create route in your laravel project in this way.

Let us know your thought on this in the comment section. If you want to hire me for your project then contact me here.

4 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *