Laravel Routing with Controller only — Using PHP 8 Attributes on Laravel Routing

Samir Mammadhasanov
3 min readJun 25, 2022

--

Laravel routing on the new way with PHP8 Attributes

Attributes are a new feature in PHP 8.0 and are only available on > PHP 8.0. The goal of these attributes, also known as annotations in many other languages, is to add metadata to classes, methods, variables, and whatnot in a structured way.

Using Attributes on Routing will avoid additional routing file creation and improve the code's readability. Also, the only requirement is PHP to do this feature and you don’t need to install any additional vendor package.

Let’s make a short comparison:

Before:

Laravel Routing on the old way

After:

Laravel routing on the new way with PHP8 Attributes

So let’s start to dive deep. How does this shortcut routing work?

First of all, I suggest you get familiar with the official attributes documentation of PHP.

If you are enough familiar with the main logic of the Attributes, we can continue to the next steps.

Let’s define our new attribute: Route

Create a new directory on the app folder with the name Attributes. Then you can create our new attribute file named Route.php:

Our custom Route attribute on Laravel

Then we need to update our route loading logic defined on RouteServiceProvider.php as below:

Final code:

RouteServiceProvider.php

This routing configuration will loop and read all your defined controllers. If you add the Route attribute to any of your methods, defined arguments will be used to make our new route.

So you don’t need to add any Route to the routes folder.

Note: This functionality is only for one level Controllers file structure. If you are using nested folder structure you need to update boot method to load all files recursively.

If you want to use Single Action Controllers you need to add Route attribute to the class (not to method):

You can use path definition like default Routing definition. (adding required or optional parameters etc.)

Conclusion:

PHP is improving itself day by day and using this Attributes feature we can super boost our productivity on the code.

--

--