Lumen-framework: ErrorException in Application.php line 1263: Undefined variable: closure

Created on 22 Jul 2015  Â·  12Comments  Â·  Source: laravel/lumen-framework

I'm trying to use some middleware in my Lumen app and i'm encountering an error with the middleware (even when it's stripped back to a basic example).

I've added the middleware to my bootstrap/app.php file:

$app->routeMiddleware([
    'authentication' => 'App\Http\Middleware\AuthenticationMiddleware::class'
]);

This is the code for my middleware (example).

<?php

namespace App\Http\Middleware;

use Closure;

class AuthenticationMiddleware
{
    /**
     * Filter the incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {

        if ($request->input('age') >= 200) {
            return redirect('home');
        }

        return $next($request);
    }

}

Each request my app receives I get the error:

ErrorException in Application.php line 1263: Undefined variable: closure

If I set $request -> input('age') to 201 then I just get redirected to home with no error - as expected.

I've got the latest version of Lumen v5.1.3 at the time of writing.

Most helpful comment

Try using:

$app -> get('/newsfeed/{user_id}/{contract_id}/{limit}/{cache_override}', ['middleware' => 'authentication', 'uses'=> 'NewsfeedController@fetch']);

All 12 comments

For some reason, the above middleware works fine if you take it out of $app -> routeMiddleware and put it in $app -> middleware

$app->middleware([
    'App\Http\Middleware\AuthenticationMiddleware::class'
]);

$app->routeMiddleware([
    //'authentication' => 'App\Http\Middleware\AuthenticationMiddleware::class'
]);

it

'App\Http\Middleware\AuthenticationMiddleware::class'

should be

App\Http\Middleware\AuthenticationMiddleware::class

without quotes.

@vluzrmos Thanks for the suggestion. I've tried that and I get the same outcome. Undefined variable: closure

I think there is something wrong in your routes.php.

https://github.com/laravel/lumen-framework/blob/5.1/src/Application.php#L1263

@vluzrmos

Hmmm. Seems to work fine my routes.php. I've included it below including the middleware through the $app->routeMiddleware([ option

<?php

/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
*/

/**
 * Status Routes
 */
$app -> get('/', 'StatusController@index');
$app -> get('/status', 'StatusController@index');

/**
 * Newsfeed Routes
 */
$app -> get('/newsfeed/{user_id}/{contract_id}/{limit}/{cache_override}', ['middleware' => 'authentication', 'NewsfeedController@fetch']);

Try using:

$app -> get('/newsfeed/{user_id}/{contract_id}/{limit}/{cache_override}', ['middleware' => 'authentication', 'uses'=> 'NewsfeedController@fetch']);

Yes!! I just figured it out now! Didn't see that in the documentation.

Thanks for your help @vluzrmos :+1:

Without "uses" the Application class will try to find a closure... And that is not what you are using...

Yes. Had this same problem. Thanks :D

Thanks!Thanks!Thanks!Thanks!

Hi, thanks for solution)) Best wishes from Ukraine)

had same problem, thank you!

Was this page helpful?
0 / 5 - 0 ratings