laravel-router

The new way of routing for the Laravel framework

MIT License

Downloads
32
Stars
12
Committers
1

Laravel Router

Attention! This package is not suitable for use in production.

The router is a new way of defining routes in the Laravel framework using annotations.

Requirements

  • Laravel 8 or above.
  • PHP 8.1 or above.

Installation

  1. Install the package via composer
composer require twirelab/laravel-router
  1. Done! It was simple.

Usage

Provider

In the place where you define routes (ex. RouteServiceProvider) you need to call a Loader class from the package.

The default class:

<?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 your application's "home" route.
     *
     * 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
    {
        RateLimiter::for('api', function (Request $request) {
            return Limit::perMinute(60)->by($request->user()?->id ?: $request->ip());
        });

        $this->routes(function () {
            Route::middleware('api')
                ->prefix('api')
                ->group(base_path('routes/api.php'));

            Route::middleware('web')
                ->group(base_path('routes/web.php'));
        });
    }
}

Change to this:

<?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 Twirelab\LaravelRouter\Facades\Loader;

class RouteServiceProvider extends ServiceProvider
{
    /**
     * The path to your application's "home" route.
     *
     * 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
    {
        RateLimiter::for('api', function (Request $request) {
            return Limit::perMinute(60)->by($request->user()?->id ?: $request->ip());
        });

        $this->routes(function () {
            Loader::group([
                'prefix' => 'api',
                'middleware' => 'api',
            ])->loadFromDirectories(
                app_path('Http/Controllers/API/**/*Controller.php'),
            );

            Loader::group([
                'middleware' => 'web',
            ])->loadFromDirectories(
                app_path('Http/Controllers/*Controller.php'),
            );
        });
    }
}

From now, the Loader automatically imports Controllers from selected directories.

If you prefer, select controllers manually all the time. You can use the loadControllers method.

use Twirelab/LaravelRouter/Loader;

Loader::group([
    'prefix' => 'api',
    'middleware' => 'api',
])->loadControllers(
    App\Http\Controllers\FirstController::class,
);

// or

Loader::group([
    'prefix' => 'api',
    'middleware' => 'api',
])->loadControllers(
    App\Http\Controllers\FirstController::class,
    App\Http\Controllers\SecondController::class,
);

If don't want to use a group function (for example: you don't need a "main" group like API or Web) you can use rest of functions directly.

use Twirelab/LaravelRouter/Facades/Loader;

Loader::loadFromDirectories(
    app_path('Http/Controllers/**/*Controllers.php')
);

// or

Loader::loadControllers(
    App\Http\Controllers\FirstController::class,
);

Controller

If you want routes to load properly, you need to add the annotate to the controller class.

<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Twirelab\LaravelRouter\Annotations\Router;

#[Router]
class FirstController extends Controller
{
    // ... methods
}

The "route" annotation works as a group function in Laravel.

Available options for Router annotation:

  • as - the name of a group,
  • prefix - the prefix of a group,
  • domain - the domain of a group,
  • middlewares - the list of middlewares of a group,

Now, we can define the first route for the method.

<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Twirelab\LaravelRouter\Annotations\Method;
use Twirelab\LaravelRouter\Annotations\Router;
use Twirelab\LaravelRouter\Enums\Methods;

#[Router]
class FirstController extends Controller
{
    #[Method(uri: '/', method: Methods::GET)]
    public function index()
    {
        // ... logic of the method
    }
}

Our route: GET - / - index > FirstController@index

Available options for Method annotation:

  • uri - the address URL for a route,
  • method - the method of a route,
  • name - the name of a route,
  • middlewares - the list of middlewares of a route,
  • where - the list of where's of a route,

Contributing

Feel free to add a new issue! Please describe in detail your problem or idea and I will check your issue and respond - Thank you!

Package Rankings
Top 41.86% on Packagist.org