Framework: Class env does not exist when called from Kernel->__construct

Created on 20 Apr 2015  路  7Comments  路  Source: laravel/framework

I'm trying to add a bit of middleware only if the environment is set to 'local' so within the Kernel class I've overridden the constructor like so

public function __construct(Application $app, Router $router)
{
    if ($app->environment('local')) {
        $this->prependMiddleware('Clockwork\Support\Laravel\ClockworkMiddleware');
    }

    parent::__construct($app, $router);
}

However, that causes a ReflectionException to be thrown

( ! ) Fatal error: Uncaught exception 'ReflectionException' with message 'Class env does not exist' in /Volumes/Workspace/lapis/vendor/laravel/framework/src/Illuminate/Container/Container.php on line 776
( ! ) ReflectionException: Class env does not exist in /Volumes/Workspace/lapis/vendor/laravel/framework/src/Illuminate/Container/Container.php on line 776
Call Stack
#   Time    Memory  Function    Location
1   0.0011  234144  {main}( )   .../server.php:0
2   0.0012  238656  require_once( '/Volumes/Workspace/lapis/public/index.php' ) .../server.php:20
3   0.0181  1956936 Illuminate\Foundation\Application->make( )  .../index.php:49
4   0.0181  1957072 Illuminate\Container\Container->make( ) .../Application.php:642
5   0.0182  1957072 Illuminate\Container\Container->build( )    .../Container.php:656
6   0.0182  1957072 Illuminate\Container\Container->Illuminate\Container\{closure}( )   .../Container.php:773
7   0.0182  1957120 Illuminate\Foundation\Application->make( )  .../Container.php:229
8   0.0182  1957120 Illuminate\Container\Container->make( ) .../Application.php:642
9   0.0183  1957152 Illuminate\Container\Container->build( )    .../Container.php:656
10  0.0208  2317544 newInstanceArgs ( ) .../Container.php:817
11  0.0208  2317744 Lapis\Http\Kernel->__construct( )   .../Container.php:817
12  0.0208  2317872 Illuminate\Foundation\Application->environment( )   .../Kernel.php:48
13  0.0208  2318224 Illuminate\Container\Container->offsetGet( )    .../Kernel.php:403
14  0.0208  2318224 Illuminate\Foundation\Application->make( )  .../Container.php:1231
15  0.0208  2318360 Illuminate\Container\Container->make( ) .../Application.php:642
16  0.0209  2318360 Illuminate\Container\Container->build( )    .../Container.php:656
17  0.0209  2318576 __construct ( )

Most helpful comment

nope, doesnt work, and it doesnt make sense, the env variable should be loaded before the app bootstraps.

All 7 comments

Environment detection happen much later than Kernel::__construct(), so this wouldn't work https://github.com/laravel/framework/blob/5.0/src/Illuminate/Foundation/Http/Kernel.php#L33

Any idea how to go about detecting the environment another way?

Create a bootstrap class, or register it under one of the service provider. AppServiceProvider or ConfigServiceProvider should be a suitable place.

Please feel free to continue this on the forums if needed.

@crynobone The service providers run after the kernel has handled the request, so I wouldn't be able to add a global middleware there.

@GrahamCampbell Sure

For future reference if anyone else comes here, I found that you can check env('APP_ENV') within the Kernel even before DetectEnvironment has run, so doing the following works for me

    /**
     * Create a new HTTP kernel instance.
     *
     * @param  Application $app
     * @param  Router      $router
     */
    public function __construct(Application $app, Router $router)
    {
        if (env('APP_ENV', 'production') === 'local') {
            $this->prependMiddleware('Clockwork\Support\Laravel\ClockworkMiddleware');
        }

        parent::__construct($app, $router);
    }

nope, doesnt work, and it doesnt make sense, the env variable should be loaded before the app bootstraps.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

digirew picture digirew  路  3Comments

klimentLambevski picture klimentLambevski  路  3Comments

lzp819739483 picture lzp819739483  路  3Comments

felixsanz picture felixsanz  路  3Comments

kerbylav picture kerbylav  路  3Comments