Laravel Route Annotations 0.4.1 Released

Tom Ellis
2 min readNov 15, 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.

Version 0.4.1 has now been released. 😃

Whats New?

This is quite another significant release, getting even closer to version 1.0. The following has been updated:

  1. A RouteCollection class has been added for sorting Routes by priority
  2. Linking to point 2, you can now define a priority when declaring routes
  3. Code tidy up
  4. Unit test tidy up

Route Priorities

Anyone that has been working with Laravel will know that routes are registered in the order that you declare them, which works fine in routes files, but sometimes its useful to declare them in whatever order you want and define the priority of them.

Now this was a feature request, as Symfony has support for this, and has done for quite some time. You can read about that here.

Lets dive into an example to better explain.

Say we had the following controller, declared using the Route Annotation Package:

<?php

namespace App\Http\Controllers;

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

#[Route('/top-users', name: 'top_users.')]
class TopUsersController extends Controller
{
#[Route('/list', name: 'list')]
public function list()
{

}

#[Route('/{metric}', name: 'metric')]
public function metric()
{

}

}

The route for /list would have to be registered first, otherwise the route for /metric would get picked up.

You can define it as the following instead:

<?php

namespace App\Http\Controllers;

use Illuminate\Routing\Controller;
use SmashedEgg\LaravelRouteAnnotation\Route;
#[Route('/top-users', name: 'top_users.')]
class TopUsersController extends Controller
{
#[Route('/{metric}', name: 'metric')]
public function metric()
{
}

#[Route('/list', name: 'list', priority: 10)]
public function list()
{

}
}

The route for /list will be registered first, because it has a priority defined, meaning you can order them in your controller whichever way you want, and define the order they are registered in by setting a priority for each one.

Summary

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

Getting that ever bit closer to a version 1.0 release.

The next stages are going to be refactoring code to make it the best it can be.

Any feedback and suggestions is very much welcome and appreciated.

--

--

Tom Ellis
Tom Ellis

Written by Tom Ellis

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

No responses yet