This package allows you to load routes using PHP Attributes to define routes in your controller classes.
More details to follow.
- PHP 8.0.2+
- Laravel 9.0+
To install this package please run:
composer require smashed-egg/laravel-route-annotation
Do you like this package? Does it improve you're development. Consider sponsoring to help with future development.
Thank you!
To register routes in your controller, first you have to import the Route annotation class:
<?php
use SmashedEgg\LaravelRouteAnnotation\Route;
The Route annotation class takes the following arguments:
- string|null $uri
- string|null $name
- string|null $domain
- array $schemes
- array $defaults
- array $methods
- array $middleware
- array $wheres
- int $priority (Set order of priority for routes, defaults to 0)
Here is an example controller using Route annotations:
<?php
namespace App\Http\Controllers;
use Illuminate\Routing\Controller;
use SmashedEgg\LaravelRouteAnnotation\Route;
#[Route('/users', name: 'users.')]
class UserController extends Controller
{
#[Route('/', name: 'home', methods: ['GET', 'POST'])]
public function home()
{
return response()->make('users.home');
}
#[Route('/create', name: 'create', methods: ['GET', 'POST'])]
public function create()
{
return response()->make('users.create');
}
#[Route('/edit/{id}', name: 'edit', methods: ['GET', 'POST'], wheres: ['id' => '[0-9]+'])]
public function edit($id)
{
return response()->make('users.edit');
}
}
You can configure resource routes by doing the following:
<?php
namespace App\Http\Controllers;
use Illuminate\Routing\Controller;
use SmashedEgg\LaravelRouteAnnotation\ResourceRoute;
#[ResourceRoute(name: 'photos')]
class PhotoController extends Controller
{
public function index()
{
}
public function create()
{
}
public function store()
{
}
public function edit($id)
{
}
public function update()
{
}
public function destroy()
{
}
}
You can configure api resource routes by doing the following:
<?php
namespace App\Http\Controllers;
use Illuminate\Routing\Controller;
use SmashedEgg\LaravelRouteAnnotation\ApiResourceRoute;
#[ApiResourceRoute(name: 'api.photos')]
class PhotoApiController extends Controller
{
public function index()
{
}
public function show()
{
}
public function store()
{
}
public function update()
{
}
public function destroy()
{
}
}
In your routes file or service provider you can add the following to load routes for a given controller class.
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\UserController;
Route::annotation(UserController::class);
In your routes file or service provider you can add the following to load routes for a given directory.
<?php
use Illuminate\Support\Facades\Route;
Route::directory(__DIR__ . '/Controllers');
You can also wrap them in route groups:
<?php
use Illuminate\Support\Facades\Route;
Route::middleware('web')->prefix('/app')->as('app.')->scopeBindings()->group(function() {
Route::directory(__DIR__ . '/Controllers');
});