How to add Custom Route File in Laravel?

10-Apr-2023

.

Admin

How to add Custom Route File in Laravel?

Hi Guys,

In this blog, You can create Custom laravel route file in your project. we will create you how to Custom route file in laravel. i will show step by step create custom route file in laravel.This is a very easy way to manage laravel custom route files in your projects.i will create different route files like user_route, customer_route.

I will give full example of laravel custom route file, the example.

Step:1


First of all, we need to realize the existing code. See there is a method named map(). Add new two method named mapUserRoutes() for & mapCustomerRoutes().

app/providers/RouteServiceProvider.php

/**

* Define the routes for the application.

*

* @return void

*/

public function map()

{

$this->mapApiRoutes();

$this->mapWebRoutes();

$this->mapCustomerRoutes();

$this->mapUserRoutes();

}

Step:2

In this method, two another method is called named mapCustomerRoutes() & mapUserRoutes(). If we want to see those two methods then we will that this method is mapping two individual route files named user.php & customer.php in the route folder.

app/providers/RouteServiceProvider.php

/**

* Define the "user" routes for the application.

*

* These routes are typically stateless.

*

* @return void

*/

protected function mapUserRoutes()

{

Route::middleware('web')

->namespace($this->namespace)

->group(base_path('routes/user.php'));

}

/**

* Define the "customer" routes for the application.

*

* These routes are typically stateless.

*

* @return void

*/

protected function mapCustomerRoutes()

{

Route::middleware('web')

->namespace($this->namespace)

->group(base_path('routes/customer.php'));

}

after, i will create the two file in routes folder user.php and customer.php.

Step:3

in this step, i will create custom route file get route. I will create route in user.php and customer.php

routes/user.php

/**

* These routes are typically stateless.

*

* @return void

*/

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

return view('userWelcome');

});

routes/customer.php

/**

* These routes are typically stateless.

*

* @return void

*/

Route::get('customer', function () {

return view('customerWelcome');

});

Step:4

In this step,i will create blade file in userWelcome.php and customerWelcome.php. we will create new blade fille in project.

resources/view/userWelcome.blade.php

<!DOCTYPE html>

<html>

<head>

<title>User Welcome - nicesnippets.com</title>

</head>

<style type="text/css">

h1{

font-size: 45px;

text-align: center;

margin:5% 25%;

border:2px solid black;

width: 50%;

border-radius: 10px;

}

</style>

<body>

<h1>User Welcome</h1>

</body>

</html>

resources/view/customerWelcome.blade.php

<!DOCTYPE html>

<html>

<head>

<title>Customer Welcome - nicesnippets.com</title>

</head>

<style type="text/css">

h1{

font-size: 45px;

text-align: center;

margin:5% 25%;

border:2px solid black;

width: 50%;

border-radius: 10px;

}

</style>

<body>

<h1>Customer Welcome</h1>

</body>

</html>

Now we are ready to run our example so run bellow command for quick run:

php artisan serve

Now you can open bellow URL on your browser:

http://localhost:8000/users

http://localhost:8000/customer

Output :

It will help you...

#Laravel 7

#Laravel

#Laravel 6