I’ve been working on a package called Laravel Route Annotations, which allows you to define routes directly on your Controller classes using PHP Attributes.

My original post about it can be found here.

Version 0.2.0 has now been released. 😃

Whats New?

This is quite a significant release as it provides the ability to define Resource and Api Resource routes.

Currently you can define these routes by adding the following to your routes file.

use App\Http\Controllers\PhotoController;
use App\Http\Controllers\PhotoApiController;

Route::resource('photos', PhotoController::class)

Route::apiResource('api.photos', PhotoApiController::class);

With this package you can now define them in your Controller classes.

The minimum you need is the following in your routes file.

# Load routes from a single controller
Route::annotation(UserController::class);

# Load routes for all controllers in a directory
Route::directory(__DIR__ . '/Controllers');

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\ResourceRoute;

#[ApiPhotoRoute(name: 'api.photos')]
class PhotoApiController extends Controller
{
public function index()
{
}

public function show()
{
}

public function store()
{
}

public function update()
{
}

public function destroy()
{
}
}

For more information on the package its best to read the docs on github, which will be updated to be in more detail soon.

Conclusion

So that’s the new features in version 0.2.0. I’ve got a little more work to do before its ready for version 1, but I would love any feedback and suggestions.

--

--

Tom Ellis

PHP and JavaScript hacker. Symfony and Laravel tinkerer. Open source developer.