smashed-egg/laravel-route-annotation

Adds support for Route Annotations in Laravel
6,178 18
Install
composer require smashed-egg/laravel-route-annotation
Latest Version:0.8.0
PHP:^8.0.2
License:MIT
Last Updated:Mar 6, 2026
Links: GitHub  ·  Packagist
Maintainer: tomgrohl

Laravel Route Annotation

Latest Stable Version Downloads this Month

This package allows you to load routes using PHP Attributes to define routes in your controller classes.

More details to follow.

Requirements

  • PHP 8.0.2+
  • Laravel 9.0+

Installation

To install this package please run:

composer require smashed-egg/laravel-route-annotation

Support Me

Do you like this package? Does it improve you're development. Consider sponsoring to help with future development.

Buy me a coffee!

Thank you!

Usage

Registering Routes

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');
    }
}

Resource Routes

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()
    {
    }
}

Api Resource Routes

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()
    {
    }
}

Loading Routes

Loading routes from a single controller

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);

Loading routes from a directory

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');
});

Related Packages

doctrine/dbal

Powerful PHP database abstraction layer (DBAL) with many features for database s...

631,580,251 9,707
laravel/framework

The Laravel Framework.

580,311,802 34,925
laravel/tinker

Powerful REPL for the Laravel framework.

489,754,436 7,444
laravel/serializable-closure

Laravel Serializable Closure provides an easy and secure way to serialize closur...

404,034,618 608
nunomaduro/collision

Cli error handling for console/command-line PHP applications.

384,821,166 4,658