😄Create Custom Route File, route frontend, route backend? (ok)

https://www.itsolutionstuff.com/post/how-to-create-custom-route-file-in-laravelexample.html

1. Ví dụ đã hoàn thành 👍

C:\xampp\htdocs\reset\app\Providers\RouteServiceProvider.php (Bản gốc 8.x)

<?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());
    });
  }
}

C:\xampp\htdocs\reset\app\Providers\RouteServiceProvider.php

<?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.');
});

2. Ví dụ đã hoàn thành 👍

C:\xampp82\htdocs\lva6\app\Providers\RouteServiceProvider.php

<?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.
   *
   * Typically, users are redirected here after authentication.
   *
   * @var string
   */
  public const HOME = '/home';
  /**
   * Define your route model bindings, pattern filters, and other route configuration.
   */
  public function boot(): 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.
   */
  public function map(): void
  {
    $this->mapApiRoutes();
    $this->mapAuthRoutes();
    $this->mapWebRoutes();
    $this->mapAdminRoutes();
  }
  /**
   * Define the "api" routes for the application.
   *
   * These routes are typically stateless.
   */
  protected function mapApiRoutes(): void
  {
    Route::prefix('api')
      ->middleware('api')
      ->group(base_path('routes/api.php'));
  }
  /**
   * Define the "auth" routes for the application.
   *
   * These routes are typically stateless.
   */
  protected function mapAuthRoutes(): 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.
   */
  protected function mapWebRoutes(): void
  {
    Route::middleware('web')
      ->group(base_path('routes/web.php'));
  }
  /**
   * Define the "admin" routes for the application.
   *
   * These routes are typically stateless.
   */
  protected function mapAdminRoutes(): void
  {
    Route::prefix('admin')
      ->middleware(['web', 'auth', 'verified'])
      ->as('admin.')
      ->group(base_path('routes/admin.php'));
  }
  /**
   * Configure the rate limiters for the application.
   */
  protected function configureRateLimiting(): void
  {
    RateLimiter::for('api', function (Request $request) {
      return Limit::perMinute(60)->by($request->user()?->id ?: $request->ip());
    });
  }
}

C:\xampp82\htdocs\lva6\routes\admin.php

<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Admin\MediaLibraryController;
Route::resource( 'media', MediaLibraryController::class )->only( [ 'index', 'show', 'create', 'store', 'destroy' ] );

C:\xampp82\htdocs\lva6\app\Http\Controllers\Admin\MediaLibraryController.php

<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class MediaLibraryController extends Controller
{
  /**
   * Display a listing of the resource.
   */
  public function index()
  {
    dd('aaa');
  }
  /**
   * Show the form for creating a new resource.
   */
  public function create()
  {
    //
  }
  /**
   * Store a newly created resource in storage.
   */
  public function store(Request $request)
  {
    //
  }
  /**
   * Display the specified resource.
   */
  public function show(string $id)
  {
    //
  }
  /**
   * Show the form for editing the specified resource.
   */
  public function edit(string $id)
  {
    //
  }
  /**
   * Update the specified resource in storage.
   */
  public function update(Request $request, string $id)
  {
    //
  }
  /**
   * Remove the specified resource from storage.
   */
  public function destroy(string $id)
  {
    //
  }
}

How to Create Custom Route File in Laravel?

By Hardik Savani June 22, 2020 Category : LaravelPauseUnmuteLoaded: 2.45%FullscreenVDO.AIIn 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.');
});

Read Also: Laravel clear cache from route, view, config and all cache data from application

Step 3: Add Files to ServiceProvider

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:

app/Providers/RouteServiceProvider.php

Read Also: Laravel Disable Registration Route Example

<?php
namespace App\Providers;
use Illuminate\Support\Facades\Route;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
class RouteServiceProvider extends ServiceProvider
{
  /**
   * This namespace is applied to your controller routes.
   *
   * In addition, it is set as the URL generator's root namespace.
   *
   * @var string
   */
  protected $namespace = 'App\Http\Controllers';
  /**
   * Define your route model bindings, pattern filters, etc.
   *
   * @return void
   */
  public function boot()
  {
    parent::boot();
  }
  /**
   * Define the routes for the application.
   *
   * @return void
   */
  public function map()
  {
    $this->mapApiRoutes();
    $this->mapWebRoutes();
    $this->mapAdminRoutes();
    $this->mapManagerRoutes();
  }
  /**
   * Define the "web" routes for the application.
   *
   * These routes all receive session state, CSRF protection, etc.
   *
   * @return void
   */
  protected function mapWebRoutes()
  {
    Route::middleware('web')
      ->namespace($this->namespace)
      ->group(base_path('routes/web.php'));
  }
  /**
   * Define the "api" routes for the application.
   *
   * These routes are typically stateless.
   *
   * @return void
   */
  protected function mapApiRoutes()
  {
    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.
   *
   * @return void
   */
  protected function mapAdminRoutes()
  {
    Route::prefix('admin')
      ->namespace($this->namespace)
      ->group(base_path('routes/admin.php'));
  }
  /**
   * Define the "user" routes for the application.
   *
   * These routes are typically stateless.
   *
   * @return void
   */
  protected function mapManagerRoutes()
  {
    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:

1) http://localhost:8000/

2) http://localhost:8000/manager/*

3) http://localhost:8000/admin/*

I hope it can help you.

Last updated