Oh Dear is the all-in-one monitoring tool for your entire website. We monitor uptime, SSL certificates, broken links, scheduled tasks and more. You'll get a notifications for us when something's wrong. All that paired with a developer friendly API and kick-ass documentation. O, and you'll also be able to create a public status page under a minute. Start monitoring using our free trial now.

A better way to register routes in Laravel

Original – by Freek Van der Herten – 3 minute read

Let's take a look at how you can define routes to controllers in Laravel. By default you can do it like this:

Route::get('my-route', 'MyController@index');

This will look for the MyController class in the App\Http\Controllers namespace. This default namespace is set up in Laravel's RouteServiceProvider.

This was fine for a long time, but in modern Laravel apps (thanks for that PR, Jaanus Vapper) there's a cool new way to register routes. You can define them using a tuple. Let's see how that looks like:

Route::get('my-route', [\App\Http\Controllers\MyController::class, 'index']);

You can make it shorter by importing the namespace:

use App\Http\Controllers\MyController;

Route::get('my-route', [MyController::class, 'index']);

In a real-world route file You might end up with lots of use statements, but a good IDE will just hide all of them.

In order to make tuple notation work you'll need to remove this namespace call in the RouteServiceProvider so Laravel won't prefix \App\Http\Controllers to an already fully qualified controller class name.

Compared to using strings, defining a route using a tuple has two benefits:

  1. If you're using an IDE like PhpStorm you can now just click through the controller class name in your route files to go to the right controller.
  2. When refactoring the name or namespace of the controller the class names in your route file will change too.

You can use tuple notation when redirection using the action method too.

namespace App\Http\Controllers;

class MyController
{
    public function myMethod()
    {
        // do some work...

        return redirect->action([MyOtherController::class, 'index']);
    }
}

If you're using an invokable controller, you can define a route like this

use App\Http\Controllers\MyController;

Route::get('my-route', MyController::class);

At Spatie we're now using this tuple notation in all our current projects. I've also updated my blog to use them, here's the relevant commit.

In my opinion, tuple based routes should become the default in a future version of Laravel. There are only benefits and there's less magic involved (no need to prefix a string behind the scenes).

Do you like this notation? Let me know in the comments below.

Stay up to date with all things Laravel, PHP, and JavaScript.

You can follow me on these platforms:

On all these platforms, regularly share programming tips, and what I myself have learned in ongoing projects.

Every month I send out a newsletter containing lots of interesting stuff for the modern PHP developer.

Expect quick tips & tricks, interesting tutorials, opinions and packages. Because I work with Laravel every day there is an emphasis on that framework.

Rest assured that I will only use your email address to send you the newsletter and will not use it for any other purposes.

Comments

What are your thoughts on "A better way to register routes in Laravel"?

Comments powered by Laravel Comments
Want to join the conversation? Log in or create an account to post a comment.