Developer654079525's avatar

Conditionally defining routes

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?

0 likes
8 replies
Tray2's avatar

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.

Developer654079525's avatar

As a small shop with their small product, we want to, for example, keep potentially sensitive routes only on a local dev machine. No need to expose them on a production server.

JussiMannisto's avatar
Level 50

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);
1 like
Tray2's avatar

Sound like a strange thing to do to me, unless they are dev only routes, and if so they shouldn't be pushed to prod at all.

jlrdw's avatar

Make your app production ready or use guards.

If not guards use authorization or RBAC.

martinbean's avatar

As a small shop with their small product, we want to, for example, keep potentially sensitive routes only on a local dev machine. No need to expose them on a production server.

@developer654079525 There are far better ways to solve that problem.

Why would you have “potentially sensitive routes” without any protection?

Developer654079525's avatar

Thank you very much for this. Very useful. Wasn't aware of this behavior when the routes are cached.

Snapey's avatar

development environment should be just that, not a place to manage data via 'sensitive' routes

1 like

Please or to participate in this conversation.