smashed-egg/laravel-route-annotation

Adds support for Route Annotations in Laravel

0.5.0 2023-11-16 19:19 UTC

This package is auto-updated.

Last update: 2024-04-16 20:10:14 UTC


README

main.jpg

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