Framework: [Proposal] Add ability to add Route::Group(['environment']..

Created on 3 Jan 2014  路  8Comments  路  Source: laravel/framework

I would like to add the ability to add the route group for one environment. I feel that something like this will be helpful for development to be able to have routes that you only want around locally or on a development box.

I have modified the createRoute functionality to allow for this.

Route::group(['environment'=>'development'], function() {
    Route::get('test', function() { return 'only available during development'; });
});

Most helpful comment

People end up here looking for the solution they think they need to a problem. They find details here for an alternative and perhaps unexpected solution to the same problem. The whole discussion becomes a part of the rich network of hyperlinks that make up the modern web, and ultimately the ideas here make their way to more formal documentation and links are added to get people there. It's how things often work, and it's great.

All 8 comments

Nothing wrong with an if-statement, is there?

if (App::environment('development'))
{

}

@JoostK There isn't... but for a cleaner routes file, I think this would be a great addition.... this way you can also do something like

Route::group(['environment'=>'development', 'prefix'=>'admin'], function() { ...

and then have one that is for all of your admin that does require before filter

Route::group(['environment'=>'production', 'prefix'=>'admin','before'=>'auth'], function() { ...

I personally see many uses of this and would love to see it in the core of Laravel. If it does not get approved, i guess I could always just write a package for it.

How is hiding a key value pair in with all the other crap you get with route groups "cleaner" for the routes file?

Is there a package that does this? I agree with @bretterer that you want a clean and clear route file.

Is there a package that does this? I agree with @bretterer that you want a clean and clear route file.

In your app\Http\Kernel.php, add to $middlewareGroups based on your needs:

'dev' => [
    \Illuminate\Session\Middleware\StartSession::class,
    \Illuminate\View\Middleware\ShareErrorsFromSession::class,
],

In your app\Providers\RouteServiceProvider.php, add to map():

if (app()->environment() == 'development' || app()->environment() == 'local')
    $this->mapDevelopmentRoutes();

Also add this to app\Providers\RouteServiceProvider.php:

protected function mapDevelopmentRoutes()
{
    Route::middleware('dev')
        ->namespace($this->namespace)
        ->group(base_path('routes/dev.php'));
}

Then create a dev.php file in your app\routes and add the routes you want.
Not sure if this is the right way to go about it, but I got the idea from:
https://hackernoon.com/hiding-feature-from-production-laravel-8815bcb64359

@bernardonigbinde That is a very good way to handle this problem (and a much more useful comment than "what's wrong with an if statement?"). Essentially, instead of setting an environment condition per route or per group in the production routes file, define additional route files for other environments and load those files conditionally.

The map function of the RouteServiceController has a // line already, which is used across Laravel to indicate "add your stuff here", which is a big hint to use this approach.

Why are people posting on a closed issue from 2014?

People end up here looking for the solution they think they need to a problem. They find details here for an alternative and perhaps unexpected solution to the same problem. The whole discussion becomes a part of the rich network of hyperlinks that make up the modern web, and ultimately the ideas here make their way to more formal documentation and links are added to get people there. It's how things often work, and it's great.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

progmars picture progmars  路  3Comments

SachinAgarwal1337 picture SachinAgarwal1337  路  3Comments

Anahkiasen picture Anahkiasen  路  3Comments

lzp819739483 picture lzp819739483  路  3Comments

shopblocks picture shopblocks  路  3Comments