Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/twirelab/laravel-router

The new way of routing for the Laravel framework
https://github.com/twirelab/laravel-router

annotation annotations laravel php81 php82 router routing twirelab

Last synced: 3 months ago
JSON representation

The new way of routing for the Laravel framework

Awesome Lists containing this project

README

        

# 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
```shell
composer require twirelab/laravel-router
```

2. 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
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
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.

```php
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.

```php
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
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
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!