Create custom route file in laravel 10

Create custom route file in laravel 10

14-Sep-2024
| |
Image Carousel

Hello developers in this tutorial we will discuss about how to create custom routes in Laravel 10.

Table of Contents

S.no Contents-topics
1 What are routes
2 Register new route
3 Register route in RouteServiceProvider
4 Register route in Kernel
5 Output

1:What are Routes

Routes are define as where we register our application urls or we also define it as it is a path or bridge that connects our frontend with backend like whenever we sends data from frontend it goes to backend with the help of route as in route file we mention our all url's along with backend functions .

2:Register new route

For registering new route we simply create a file routes/admin.php , here for example we are using admin.php as our new route file you can use any appropriate route according to your requirment, as in our new route file we want all the same properties as in web.php file.
code for admin.php 
Copy

<?php
 
use Illuminate\Support\Facades\Route;
 
Route::get('dashboard', function () {
    echo"This is admin dashboard";
});

3:Register route in RouteServiceProvider

As we create our new route routes/admin.php and now we register our route in app/Providers/RouteServiceProvider.php to register our new route so that our laravel application give access to our new route.
code for RouteServiceProvider : Copy

<?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
    {
        $this->configureRateLimiting();
 
        $this->routes(function () {
            Route::middleware('api')
                ->prefix('api')
                ->group(base_path('routes/api.php'));
 
            Route::middleware('admin')
                ->prefix('admin')
                ->group(base_path('routes/admin.php'));
 
            Route::middleware('web')
                ->group(base_path('routes/web.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());
        });
    }
}

As shown we register our admin.php file with the prefix of admin that means our our url inside admin.php will be call by prefix admin/ like we have to call home url then we will call with admin/home Now after register our route in RouteServcieProvider as to provide similar properties with the web.php we will also register in app/Http/Kernel.php.

4: Register route in Kernel

Main reason for register route in app/Http/Kernel.php to provide the same middleware properties as of web.php as in kernel you can also provide the custom middleware as it will apply on all the routes in admin.php .
code for kernel.php :Copy

<?php
 
namespace App\Http;
 
use Illuminate\Foundation\Http\Kernel as HttpKernel;
 
class Kernel extends HttpKernel
{
    /**
     * The application's global HTTP middleware stack.
     *
     * These middleware are run during every request to your application.
     *
     * @var array<int, class-string|string>
     */
    protected $middleware = [
        // \App\Http\Middleware\TrustHosts::class,
        \App\Http\Middleware\TrustProxies::class,
        \Illuminate\Http\Middleware\HandleCors::class,
        \App\Http\Middleware\PreventRequestsDuringMaintenance::class,
        \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
        \App\Http\Middleware\TrimStrings::class,
        \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
    ];
 
    /**
     * The application's route middleware groups.
     *
     * @var array<string, array<int, class-string|string>>
     */
    protected $middlewareGroups = [
        'web' => [
            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
            \Illuminate\Session\Middleware\StartSession::class,
            \Illuminate\View\Middleware\ShareErrorsFromSession::class,
            \App\Http\Middleware\VerifyCsrfToken::class,
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
        ],
 
        'api' => [
            // \Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,
            \Illuminate\Routing\Middleware\ThrottleRequests::class.':api',
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
        ],
 
        'admin' => [
            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
            \Illuminate\Session\Middleware\StartSession::class,
            \Illuminate\View\Middleware\ShareErrorsFromSession::class,
            \App\Http\Middleware\VerifyCsrfToken::class,
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
        ],
    ];
 
    /**
     * The application's middleware aliases.
     *
     * Aliases may be used to conveniently assign middleware to routes and groups.
     *
     * @var array<string, class-string|string>
     */
    protected $middlewareAliases = [
        'auth' => \App\Http\Middleware\Authenticate::class,
        'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
        'auth.session' => \Illuminate\Session\Middleware\AuthenticateSession::class,
        'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
        'can' => \Illuminate\Auth\Middleware\Authorize::class,
        'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
        'password.confirm' => \Illuminate\Auth\Middleware\RequirePassword::class,
        'signed' => \App\Http\Middleware\ValidateSignature::class,
        'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
        'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
    ];
}

 5:Output

 

Tags: create a custom route file in laravel 10 , how to create a custom route file in laravel 10 , how to register custom route file in laravel 10 , make out route file in laravel 10 , laravel 10 features , advance laravel 10 features,laravel , php ,laravel-php , mvc laravel, advance laravel , bugs in laravel , laravel advance level,
0 Comments (Please let us know your query)
Leave Comment
Leave Comment
Articles from other Categories
Load More

Newsletter