poshtive/router
Convention-based route discovery for Laravel controllers. Public methods become routes automatically, with PHP attributes for HTTP verb overrides, middleware, where constraints, prefixes, and model-bound parameters. Supports Laravel 12/13 and PHP 8.3.
Convention-based route discovery for Laravel controllers, with PHP attributes for overrides and metadata.
laravel-router is stable.
^8.3^12.0|^13.0src/ at 100% locallywhere constraints, and inheritance-aware discoveryInstall the package with Composer:
composer require poshtive/router
Optionally publish the configuration file:
php artisan vendor:publish --provider="Poshtive\Router\RouterServiceProvider" --tag="config"
Configure route discovery in config/router.php:
'groups' => [
'web' => [
'paths' => [app_path('Http/Controllers')],
'middleware' => ['web'],
],
],
Then add a controller method:
namespace App\Http\Controllers;
class UserController
{
public function index() {}
}
This registers GET /user automatically after the service provider boots.
You can still define Laravel routes manually as usual.
Keep web and API controllers in separate directories when they use different middleware or URL prefixes. For example, configure an API group like this:
// config/router.php
'groups' => [
'api' => [
'paths' => [app_path('Http/Controllers/Api')],
'prefix' => 'api',
'name' => 'api.',
'middleware' => ['api'],
],
],
Then add app/Http/Controllers/Api/UserController.php:
namespace App\Http\Controllers\Api;
use App\Models\User;
use Poshtive\Router\Attributes\Route;
class UserController
{
public function index() {}
public function show(User $user) {}
#[Route(method: 'POST')]
public function store() {}
#[Route(method: ['PUT', 'PATCH'])]
public function update(User $user) {}
}
With the default attribute_or_get convention, discovery registers:
| Method | URI | Name |
|---|---|---|
GET |
/api/user |
api.user.index |
GET |
/api/user/{user}/show |
api.user.show |
POST |
/api/user/store |
api.user.store |
PUT, PATCH |
/api/user/{user}/update |
api.user.update |
The User $user type hint supplies the {user} binding placeholder. Laravel
resolves the model after the route is registered. Method names default to GET;
use #[Route(method: ...)] or http_methods_map for other verbs.
Run the package test suite with:
composer test
Generate a Clover coverage report for src/ with:
composer test:coverage
Release notes are tracked in CHANGELOG.md.
Contributions are welcome. Please feel free to submit issues or pull requests.
This package is open-sourced software licensed under the MIT license.
How can I help you explore Laravel packages today?