Git Product home page Git Product logo

laravel-router's Introduction

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!

laravel-router's People

Contributors

lukaszlupa avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

laravel-router's Issues

Add a group function to the Loader class

Describe the solution you'd like
From this:

class RouteServiceProvider extends ServiceProvider
{
    public function boot(): void
    {
        $this->routes(function () {
            Route::middleware('web')
                ->group(function () {
                    Loader::loadFromDirectories(app_path('Http/Controllers/*Controller.php') );
                });
        });
    }
}

to this:

class RouteServiceProvider extends ServiceProvider
{
    public function boot(): void
    {
        $this->routes(function () {
            Loader::group('web')
                ->loadFromDirectories(app_path('Http/Controllers/*Controller.php'));
        });
    }
}

Wrong name of group attribute

To Reproduce
Steps to reproduce the behaviour:

  1. Create a controller and add router annotation.
  2. Set a middleware param.

Expected behaviour
A clear and concise description of what you expected to happen.

Versions (please complete the following information):

  • Package: 0.2.0
  • PHP: 8.2.10

Versioning

Describe the solution you'd like
Create a new option (Router and Method) in annotations to set a version.

The group cannot be named

Describe the bug
The group cannot be named.

Versions
Package version: 0.1.0

To Reproduce
Steps to reproduce the behavior:

  • Set the name value in the Route annotation.
  • Run a routes list command.

Expected behavior
If set a name value (e.g. 'auth.') in the Route annotation, then all methods must have a prefix before the name (e.g. if the method is called 'login', the entire route name must have a prefix and the method name - 'auth.login')

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.