How do I define routes in Laravel that work only in the development environment?

2

I would like to know how I can define routes in Laravel that work only in the development environment.

I think this can be useful to make it easier to debugs .

I want to be able to separate some routes to use only when in development and, in production, these routes do not exist.

How can I do this?

    
asked by anonymous 02.09.2015 / 17:27

1 answer

2

In Laravel, it is possible to detect whether the environment in which the system is running in production or location.

The app / start / local.php file

In the case of local development, we have the file named app/start/local.php , which is only added to the application Laravel 4 if it is detected that environment (environment) is marked as "local" .

It would be possible to define some routes there, so they would work only when the application was in development.

An example:

#app/start/local.php

Route::group(['prefix' => 'local'], function ()
{

    Route::get('login/{id}', function ($id)
    {
        $u = Usuario::findOrFail($id);

        Auth::login($u);

        return Redirect::to('/');
    });
});

In this case, when we access the localhost/local/login/1 route, we could authenticate a user in a simple way, without the need for a form. And this is something very useful for development to become more agile.

Methods for detection of development environment

If there is a need to know in some code snippets that we are or are not in the development environment, we have three ways of detecting this, using methods present in Illuminate\Foundation\Application .

Example 1 :

var_dump(App::isLocal()); //bool(true);

var_dump(App::environment('local')); // bool(true)

var_dump(App::environment() === 'local')); // bool(true)

I do not know if it depends on the version of Laravel 4 , but I particularly like to use the first one.

With this, it is possible to do include of functions to debug, for example, when we are developing.

Example:

#app/start/global.php

if (App::isLocal()) 
    require app_path('local_functions.php');

Detecting the environment through the host used

It is also possible to define whether the application is in the local environment or not simply by checking the% of its% source.

The way I always use it is by adding a few lines of code to the url file.

So:

$env = $app->detectEnvironment(function() use($app) {

    $enviromentsHosts = [
        'localhost',
        'url_virtualhost',

    ];

    if (in_array($app['request']->getHost(), $enviromentsHosts)) {

        return 'local';

    } else {

        return 'production';
    }
});
    
02.09.2015 / 17:27