[AdSense-A]
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('Invoice', function () {
$invoice = Team::first()->findInvoice('in_1FEYTqF2myXP8kzfgFqu4ofw');
<span class="hljs-keyword">return</span> View::make(<span class="hljs-string">'cashier::receipt'</span>, array_merge([
<span class="hljs-string">'vendor'</span> => <span class="hljs-string">'Flare'</span>,
<span class="hljs-string">'localInvoice'</span> => Invoice::first(),
<span class="hljs-string">'product'</span> => <span class="hljs-string">'Pro Subscription'</span>,
], [
<span class="hljs-string">'invoice'</span> => $invoice,
<span class="hljs-string">'owner'</span> => $invoice->owner,
<span class="hljs-string">'user'</span> => $invoice->owner,
]));
});
Route::get(<span class="hljs-string">'Invoice'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-params">()</span> </span>{
$invoice = Team::first()->findInvoice(<span class="hljs-string">'in_1FEYTqF2myXP8kzfgFqu4ofw'</span>);
<span class="hljs-keyword">return</span> View::make(<span class="hljs-string">'cashier::receipt'</span>, array_merge([
<span class="hljs-string">'vendor'</span> => <span class="hljs-string">'Flare'</span>,
<span class="hljs-string">'localInvoice'</span> => Invoice::first(),
<span class="hljs-string">'product'</span> => <span class="hljs-string">'Pro Subscription'</span>,
], [
<span class="hljs-string">'invoice'</span> => $invoice,
<span class="hljs-string">'owner'</span> => $invoice->owner,
<span class="hljs-string">'user'</span> => $invoice->owner,
]));
});
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 views 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 () {
$errorOccurrence = ErrorOccurrence::factory()->create();
<span class="hljs-keyword">return</span> redirect($errorOccurrence->error->signedSnoozeUrl(<span class="hljs-string">'[email protected]'</span>));
});
});
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<span class="hljs-title">Providers;
class RouteServiceProvider extends ServiceProvider
{
<span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">map</span><span class="hljs-params">(Router $router)</span>
</span>{
<span class="hljs-comment">// mapping of other route files</span>
<span class="hljs-keyword">if</span> (app()->environment(<span class="hljs-string">'local'</span>)) {
<span class="hljs-keyword">$this</span>->mapDevRoutes($router);
}
}
}
<span class="hljs-keyword">protected</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">mapDevRoutes</span><span class="hljs-params">(Router $router)</span>
</span>{
$router
->middleware(<span class="hljs-string">'web'</span>)
->prefix(<span class="hljs-string">'dev'</span>)
->group(base_path(<span class="hljs-string">'routes/dev.php'</span>));
}
}
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.
(more…)