<?php
namespace App\Providers;
use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\Facades\Route;
class RouteServiceProvider extends ServiceProvider {
/**
* The path to the "home" route for your application.
*
* This is used by Laravel authentication to redirect users after login.
*
* @var string
*/
public const HOME = '/home';
/**
* The controller namespace for the application.
*
* When present, controller route declarations will automatically be prefixed with this namespace.
*
* @var string|null
*/
// protected $namespace = 'App\\Http\\Controllers';
/**
* Define your route model bindings, pattern filters, etc.
*
* @return void
*/
public function boot() {
$this->configureRateLimiting();
$this->routes(function () {
Route::prefix('api')
->middleware('api')
->namespace($this->namespace)
->group(base_path('routes/api.php'));
Route::middleware('web')
->namespace($this->namespace)
->group(base_path('routes/web.php'));
});
}
/**
* Configure the rate limiters for the application.
*
* @return void
*/
protected function configureRateLimiting() {
RateLimiter::for ('api', function (Request $request) {
return Limit::perMinute(60)->by(optional($request->user())->id ?: $request->ip());
});
}
}
<?php
namespace App\Providers;
use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\Facades\Route;
class RouteServiceProvider extends ServiceProvider {
/**
* The path to the "home" route for your application.
*
* This is used by Laravel authentication to redirect users after login.
*
* @var string
*/
public const HOME = '/home';
/**
* The controller namespace for the application.
*
* When present, controller route declarations will automatically be prefixed with this namespace.
*
* @var string|null
*/
//
protected $namespace = 'App\\Http\\Controllers';
/**
* Define your route model bindings, pattern filters, etc.
*
* @return void
*/
public function boot() {
$this->configureRateLimiting();
$this->routes(function () {
Route::prefix('api')->namespace($this->namespace)->group(base_path('routes/api.php'));
Route::prefix('web')->namespace($this->namespace)->group(base_path('routes/web.php'));
Route::prefix('admin')->namespace($this->namespace)->group(base_path('routes/admin.php'));
Route::prefix('manager')->namespace($this->namespace)->group(base_path('routes/manager.php'));
});
}
/**
* Configure the rate limiters for the application.
*
* @return void
*/
protected function configureRateLimiting() {
RateLimiter::for ('api', function (Request $request) {
return Limit::perMinute(60)->by(optional($request->user())->id ?: $request->ip());
});
}
}
C:\xampp\htdocs\reset\routes\admin.php
<?php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
dd('Welcome to admin user routes.');
});
C:\xampp\htdocs\reset\routes\web.php
<?php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
dd('Welcome to simple user route file.');
});
C:\xampp\htdocs\reset\routes\manager.php
<?php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
dd('Welcome to manager user routes.');
});
<?phpnamespaceApp\Providers;useIlluminate\Cache\RateLimiting\Limit;useIlluminate\Foundation\Support\Providers\RouteServiceProvideras ServiceProvider;useIlluminate\Http\Request;useIlluminate\Support\Facades\RateLimiter;useIlluminate\Support\Facades\Route;classRouteServiceProviderextendsServiceProvider{/** * The path to the "home" route for your application. * * Typically, users are redirected here after authentication. * * @varstring */publicconst HOME ='/home';/** * Define your route model bindings, pattern filters, and other route configuration. */publicfunctionboot():void {parent::boot();$this->configureRateLimiting();// $this->routes(function () {// Route::middleware('api')// ->prefix('api')// ->group(base_path('routes/api.php'));// Route::middleware('web')// ->group(base_path('routes/web.php'));// }); }/** * Define the routes for the application. */publicfunctionmap():void {$this->mapApiRoutes();$this->mapAuthRoutes();$this->mapWebRoutes();$this->mapAdminRoutes(); }/** * Define the "api" routes for the application. * * These routes are typically stateless. */protectedfunctionmapApiRoutes():void {Route::prefix('api')->middleware('api')->group(base_path('routes/api.php')); }/** * Define the "auth" routes for the application. * * These routes are typically stateless. */protectedfunctionmapAuthRoutes():void {Route::middleware('web')->group(base_path('routes/auth.php')); }/** * Define the "web" routes for the application. * * These routes all receive session state, CSRF protection, etc. */protectedfunctionmapWebRoutes():void {Route::middleware('web')->group(base_path('routes/web.php')); }/** * Define the "admin" routes for the application. * * These routes are typically stateless. */protectedfunctionmapAdminRoutes():void {Route::prefix('admin')->middleware(['web','auth','verified'])->as('admin.')->group(base_path('routes/admin.php')); }/** * Configure the rate limiters for the application. */protectedfunctionconfigureRateLimiting():void {RateLimiter::for('api',function (Request $request) {returnLimit::perMinute(60)->by($request->user()?->id ?: $request->ip()); }); }}
<?phpnamespaceApp\Http\Controllers\Admin;useApp\Http\Controllers\Controller;useIlluminate\Http\Request;classMediaLibraryControllerextendsController{/** * Display a listing of the resource. */publicfunctionindex() {dd('aaa'); }/** * Show the form for creating a new resource. */publicfunctioncreate() {// }/** * Store a newly created resource in storage. */publicfunctionstore(Request $request) {// }/** * Display the specified resource. */publicfunctionshow(string $id) {// }/** * Show the form for editing the specified resource. */publicfunctionedit(string $id) {// }/** * Update the specified resource in storage. */publicfunctionupdate(Request $request,string $id) {// }/** * Remove the specified resource from storage. */publicfunctiondestroy(string $id) {// }}
How to Create Custom Route File in Laravel?
By Hardik Savani June 22, 2020 Category : LaravelPauseUnmuteLoaded: 2.45%FullscreenIn this tutorial, i will show you laravel create custom route file example. you can understand a concept of laravel add custom route file example. step by step explain how to add custom route file in laravel. i explained simply about how to create custom route file in laravel. You just need to some step to done define custom route file in laravel 6, laravel 7, laravel 8 and laravel 9.
If you are working with large application in laravel and you have different types of users then you have to create custom route file for user type wise in your application. for example if you have user, manager and admin three types of user then you have different prefix like "user/*", "manager/*" and "admin/*" url will be. so if you create different route file then you can easily make it better way your routes.
In this example, i will let you know how you to create custom route file in laravel application. you can easily use it with laravel 5, laravel 6 and laravel 7. So let's follow bellow steps:
In this tutorial, I am going to share with you how to define and use subdomain routes better way in Laravel 5 application.
Step 1 : Install Laravel Fresh Application
we are going from scratch, So we require to get fresh Laravel application using bellow command, So open your terminal OR command prompt and run bellow command:
composer create-project --prefer-dist laravel/laravel blog
Step 2: Create Custom Route File
Here, we will create following route file with bellow listed route. so let's create route file.
1) User Routes : Here you have to define routes in web.php in routes folder. in that file you have declare routes for user login.
2) Manager Routes : Here you have to create new file manager.php in routes folder. in that file you have declare routes for manager user.
3) Admin Routes : Here you have to create new file admin.php in routes folder. in that file you have declare routes for admin user.
So, let's proceed with routes define:
routes/web.php
<?php/*|--------------------------------------------------------------------------| Web Routes|--------------------------------------------------------------------------|| Here is where you can register web routes for your application. These| routes are loaded by the RouteServiceProvider within a group which| contains the "web" middleware group. Now create something great!|*/Route::get('/',function () { dd('Welcome to simple user route file.');});
routes/manager.php
<?php/*|--------------------------------------------------------------------------| User Routes|--------------------------------------------------------------------------|| Here is where you can register user routes for your application. These| routes are loaded by the RouteServiceProvider within a group which| contains the "user" middleware group. Now create something great!|*/Route::get('/',function () { dd('Welcome to manager user routes.');});
routes/admin.php
<?php/*|--------------------------------------------------------------------------| Admin Routes|--------------------------------------------------------------------------|| Here is where you can register admin routes for your application. These| routes are loaded by the RouteServiceProvider within a group which| contains the "admin" middleware group. Now create something great!|*/Route::get('/',function () { dd('Welcome to admin user routes.');});
In this step, we require to register our new two user file in RouteServiceProvider, that way we can create new file for each user wise like we create two user "admin" and "manager" then we create new file admin.php and manager.php in routes directory for define routing.
So, let's open RouteServiceProvider.php and put bellow code:
<?phpnamespaceApp\Providers;useIlluminate\Support\Facades\Route;useIlluminate\Foundation\Support\Providers\RouteServiceProvideras ServiceProvider;classRouteServiceProviderextendsServiceProvider{/** * This namespace is applied to your controller routes. * * In addition, it is set as the URL generator's root namespace. * * @varstring */protected $namespace ='App\Http\Controllers';/** * Define your route model bindings, pattern filters, etc. * * @returnvoid */publicfunctionboot() {parent::boot(); }/** * Define the routes for the application. * * @returnvoid */publicfunctionmap() {$this->mapApiRoutes();$this->mapWebRoutes();$this->mapAdminRoutes();$this->mapManagerRoutes(); }/** * Define the "web" routes for the application. * * These routes all receive session state, CSRF protection, etc. * * @returnvoid */protectedfunctionmapWebRoutes() {Route::middleware('web')->namespace($this->namespace)->group(base_path('routes/web.php')); }/** * Define the "api" routes for the application. * * These routes are typically stateless. * * @returnvoid */protectedfunctionmapApiRoutes() {Route::prefix('api')->middleware('api')->namespace($this->namespace)->group(base_path('routes/api.php')); }/** * Define the "admin" routes for the application. * * These routes are typically stateless. * * @returnvoid */protectedfunctionmapAdminRoutes() {Route::prefix('admin')->namespace($this->namespace)->group(base_path('routes/admin.php')); }/** * Define the "user" routes for the application. * * These routes are typically stateless. * * @returnvoid */protectedfunctionmapManagerRoutes() {Route::prefix('manager')->namespace($this->namespace)->group(base_path('routes/user.php')); }}
Ok, now we are ready to run simple example with our users. So, in this tutorial i explained i created following url as listed bellow: