How to add Custom Route File in Laravel?
Teams applying “How to add Custom Route File in Laravel?” in a production project can use resource forecasting to record implementation, review and debugging time, then compare that effort with tests and shipped functionality instead of treating activity as performance by itself.
Before copying the example into a live application, confirm version-specific behaviour with the official Laravel website and test it against the project’s actual database and runtime.
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...
- How to Notifications With database Driver in Laravel 11?
- How to Send Email Via Notification in Laravel 11?
- How to Install and Configure Laravel 11 Debugbar?
- How to Like Dislike System in Laravel 11?
- How to Paginate a Model with Relationship in Laravel 11?
- Laravel 11 Upload Files to Amazon S3 Example
- Laravel 11 Send WhatsApp Messages Example
- Laravel 11 Rest API Authentication Using JWT Tutorial