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.

Develop faster by adding dev routes file in a Laravel app

Original – by Freek Van der Herten – 3 minute read

Laravel's awesome closure based routing was probably one of the first features I fell in love with. I take it for granted now, but back in the days, such a simple way of adding a route felt like a glass of water in hell compared to the other frameworks.

Typically, you would only add routes that are necessary for the users of your app. Something that I have been doing for a long time is to create a routes file, called dev.php, with routes that can help with app development.

Here's some of the content of routes/dev.php from the Flare codebase.

Route::prefix('mails')->group(function (): void {
    Route::get('NewErrorOccurredNotification', function () {
        $notificationDestination = NotificationDestination::factory()->create();

        $atom = new NotificationDestinationAtom($notificationDestination, 'john@example.com', 0);

        $occurrence = ErrorOccurrenceFactory::createWithStackTrace();

        return (new NewErrorOccurredNotification($occurrence))->toMail($atom);
    });

    Route::get('Invoice', function () {
        $invoice = Team::first()->findInvoice('in_1FEYTqF2myXP8kzfgFqu4ofw');

        return View::make('cashier::receipt', array_merge([
            'vendor' => 'Flare',
            'localInvoice' => Invoice::first(),
            'product' => 'Pro Subscription',
        ], [
            'invoice' => $invoice,
            'owner' => $invoice->owner,
            'user' => $invoice->owner,
        ]));
    });

When you visit the first route above, you'll see the rendered mail.

screenshot

Using these routes, we can quickly render a mail in the browser and work on its content and layout. Of course, you can add routes for other things as well. Here's another example that we use to quickly render a view that users would see when they click a signed URL (for example, in Flare's mail notifications).

Route::prefix('signed')->group(function (): void {
    Route::get('snooze', function () {
        /** @var Error $error */
        $errorOccurrence = ErrorOccurrence::factory()->create();

        return redirect($errorOccurrence->error->signedSnoozeUrl('john@example.com'));
    });
});

Of course, you only don't want to have these routes in production, but only in the local environment. It's easy to set this up in the route service provider.


namespace App\Providers;

// other imports...

class RouteServiceProvider extends ServiceProvider
{
		// ...
		
	public function map(Router $router)
    {
		    // mapping of other route files
		    
		    if (app()->environment('local')) {
            	$this->mapDevRoutes($router);
			}  
        }
    }
    
    protected function mapDevRoutes(Router $router)
    {
        $router
            ->middleware('web')
            ->prefix('dev')
            ->group(base_path('routes/dev.php'));
    }
}

As you can see in the code above, I typically use a prefix dev to avoid conflicts with other routes in the app. Of course, you can name the routes file and prefix to whatever you like.

I really like using dev only routes, and hope that using a dev.php routes file can help you as well.

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

Follow me on Twitter. I regularly tweet out programming tips, and what I myself have learned in ongoing projects.

Every two weeks 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

Webmentions

Mr. P liked on 6th May 2021
MannikJ liked on 6th May 2021
Ross Ball liked on 6th May 2021
Ero-brunnin liked on 6th May 2021
imtiaz shakil liked on 6th May 2021
معاذ liked on 6th May 2021
Akash Shukla liked on 6th May 2021
Sebastian Rasch liked on 6th May 2021
Enzo Notario liked on 6th May 2021
Reno Philibert liked on 6th May 2021
Laravel Digest liked on 6th May 2021
Luke Abell liked on 6th May 2021
Tauseef shah liked on 6th May 2021
Salman Zafar liked on 6th May 2021
Jona Goldman liked on 6th May 2021
Sketch liked on 6th May 2021
Tahsin Abrar replied on 6th May 2021
Thanks for sharing. Liked the idea.
Anggit Ari Utomo 🐘 liked on 6th May 2021
Valo@Home liked on 6th May 2021
Tamás liked on 6th May 2021
José Cage liked on 6th May 2021
Tahsin Abrar liked on 6th May 2021
Dickens Odera liked on 6th May 2021
Adam Hardwick liked on 6th May 2021
Phillip Mwaniki N liked on 6th May 2021
Mikael Jorhult liked on 6th May 2021
Mark Debeljak liked on 6th May 2021
Thomas Venturini ⛵ liked on 5th May 2021
Thomas Venturini ⛵ replied on 5th May 2021
Cool Idea! 👌
Matthew Erwin liked on 5th May 2021
Daniel Bakan liked on 5th May 2021
Scorp974 liked on 5th May 2021
Matthew Erwin retweeted on 5th May 2021
Daniel retweeted on 5th May 2021
Daniel liked on 5th May 2021
Martin Medina liked on 5th May 2021
Jos Kolenberg liked on 5th May 2021
Abraham liked on 5th May 2021
ArielMejiaDev retweeted on 5th May 2021
bamo nadir liked on 5th May 2021
ArielMejiaDev liked on 5th May 2021
Abraham retweeted on 5th May 2021
Meet Thosar liked on 5th May 2021
Elvaris Gjinali liked on 5th May 2021
Hamed liked on 5th May 2021
ketsakda liked on 5th May 2021
Adinata retweeted on 5th May 2021
ali ali liked on 5th May 2021
Bill Richards liked on 5th May 2021
Abrar Ahmad liked on 5th May 2021
Michael Aguiar liked on 5th May 2021
Daniel liked on 5th May 2021
Peter Brinck 🤘 liked on 5th May 2021
Yoeri Boven replied on 5th May 2021
I currently deselect my dev routes in Github UI, so this is definitely a better way!
Grant Bennett liked on 5th May 2021
kronos_I retweeted on 5th May 2021
Brenier Arnaud liked on 5th May 2021
Brenier Arnaud retweeted on 5th May 2021
Jigal Sanders liked on 5th May 2021
chetan kharel liked on 5th May 2021
Mark liked on 5th May 2021
ダビッド トレス liked on 5th May 2021
Derek D liked on 5th May 2021
Richard Radermacher liked on 5th May 2021
Kruchy liked on 5th May 2021
Milan Urukalo liked on 5th May 2021
Alexis liked on 5th May 2021
Samuel Breuillet retweeted on 5th May 2021
Samuel Breuillet liked on 5th May 2021
Lennart Fischer liked on 5th May 2021
Stephen Shead liked on 5th May 2021
William Richard liked on 5th May 2021
j u a n liked on 5th May 2021
Willan Correia liked on 5th May 2021
Ravi Teja liked on 5th May 2021
bhaidar.dev ⚖️ retweeted on 5th May 2021
Nazmul Hasan Robin liked on 5th May 2021
Tim Joosten liked on 5th May 2021
bhaidar.dev ⚖️ liked on 5th May 2021
KlidiSp liked on 5th May 2021
Yasuke's Stan liked on 5th May 2021
Russ Clewett liked on 5th May 2021
Victor liked on 5th May 2021
Daniel Lucas liked on 5th May 2021
Alex liked on 5th May 2021
Sami Mazouz liked on 5th May 2021
Eliepse liked on 5th May 2021
RR liked on 5th May 2021
Peter Fox liked on 5th May 2021
Jens Twesmann liked on 5th May 2021
Blair Jersyer liked on 5th May 2021
Parthasarathi G K liked on 5th May 2021
Simon Blonér liked on 5th May 2021
Laravel tweets 🤖 retweeted on 5th May 2021
corbosman replied on 5th May 2021
Pretty cool tip. Thanks for sharing.
Jamie Shiers liked on 5th May 2021
Benjamin Crozat liked on 5th May 2021
Spatie retweeted on 5th May 2021
PHP Synopsis retweeted on 5th May 2021
Adrian Crisan liked on 5th May 2021
Ryan C. liked on 5th May 2021
فرودو liked on 5th May 2021
Padam Shankhadev liked on 5th May 2021
De Gracia Mathieu liked on 5th May 2021
Micha B liked on 5th May 2021
Murad Abaza liked on 5th May 2021
Mark Lewis liked on 5th May 2021
Dumitru Botezatu liked on 5th May 2021
Adam Wills liked on 5th May 2021
Huey liked on 5th May 2021
NAbeel Yousaf 🇵🇰 liked on 5th May 2021
Hayden liked on 5th May 2021
Apolistol 🔻 liked on 5th May 2021
Jesse Kanner liked on 5th May 2021
Laravel tweets 🤖 liked on 5th May 2021
Robin Dirksen liked on 5th May 2021
João Alves liked on 5th May 2021
Samuel Štancl liked on 5th May 2021
/dev/jovert liked on 5th May 2021
Lazy liked on 5th May 2021
Wyatt liked on 5th May 2021
👍👍🇵🇰