Laravel Route Annotations 0.3.1 Released

Tom Ellis
2 min readSep 19, 2023

I’ve been working some recent changes 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. I wrote an update on version 0.2.0 here.

Version 0.3.1 has now been released. 😃

Whats New?

This is yet another significant release, getting slower to version 1.0, as it provides the ability to define:

  1. Singleton Routes
  2. Wrap calls in route groups (this was previously not working)
  3. Scoped bindings (this was previously not working)

Singleton Routes

Singleton Routes can now be defined in controllers by doing the

<?php

namespace App\Http\Controllers;

use Illuminate\Routing\Controller;
use SmashedEgg\LaravelRouteAnnotation\SingletonResourceRoute;

#[SingletonResourceRoute(name: 'profile')]
class ProfileController extends Controller
{
public function show()
{
}

public function edit()
{
}

public function update()
{
}
}

You routes.php file would look something like this:

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ProfileController;


Route::annotation(ProfileController::class);

Route Groups

Previously the following should have worked. Any option set at a group level was never applied to the routes inside it

<?php

Route::group(['middleware' => 'web'], function() {

Route::directory(dirname(__DIR__) . '/app/Http/Controllers');

});

Scoped Bindings

Against like with the Route Groups, this never worked when used at a Route Group level. There is no also an option to set it via annotation.

There are 2 ways it can be configured:

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\HomeController;

Route::middleware('web')->prefix('/app')->as('app.')->scopeBindings()->group(function() {
Route::directory(__DIR__ . '/Controllers');
// OR
Route::annotation(HomeController::class);
});

OR

<?php

namespace App\Http\Controllers;

use App\Models\Comment;
use App\Models\Post;
use Illuminate\Http\Request;
use Inertia\Inertia;
use SmashedEgg\LaravelRouteAnnotation\Route;

class PostController extends Controller
{
#[Route(uri: '/posts/{post}/comments/{comment}', name: 'post.comments.view', scopeBindings: true)]
public function viewComment(Post $post, Comment $comment)
{
return Inertia::render('Post/Comment', [
'post' => $post,
'comment' => $comment,
]);
}
}

Summary

Those are the newest changes in version 0.3.1. Thanks to everyone thats currently using the package and to those that have provided feedback.

Getting closer to a version 1.0 release.

Any feedback and suggestions is very much welcome and appreciated.

--

--

Tom Ellis

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