Why would you want to differentiate between local and prod when coming to routes?
However, I would probably put that kind of check in the controller, that way I don't need to pollute the routes file with logic.
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
What is your approach to conditionally defining the routes, especially when differentiating between the local dev machine and a live production server? Would a simple app()->isLocal() in a route file suffice?
You could create a middleware and apply it to the route. Conditional clauses don't work when routes are cached, so it's best to keep the route files simple.
class LocalOnly {
public function handle(Request $request, Closure $next) {
if (!app()->environment('local'))
abort(404);
return $next($request);
}
}
...
Route::get('myroute')->middleware(LocalOnly::class);
Please or to participate in this conversation.