Laravel没有找到Alias的路由中间件

On my development setup, my Laravel project is working fine but after deploying it to my production server, I am getting an error. The error doesn't occur on the first request but does occur every request after that.

I suspect it's something to do with caching, but I can't work out what.

I have the following in my app/Http/Kernel.php (NB. there are other middlewares defined that also fail, this is an example to illustrate the error):

protected $routeMiddleware = [
        'json-response-headers' => \App\Http\Middleware\JsonResponseHeaders::class,   
    ];

And a route that uses it (again, the re are lots of routes using middleware, this is an example of a simple one that fails like all the others):

// system
Route::group( [
    'prefix' => 'system',
    'middleware' => [
        'json-response-headers'
    ]
], function()
{
    Route::get( 'status', 'SystemController@status' );
} );

When I request /system/status, it works the first time (as it does whenever I modifying the routes.php file) but after that, it gives the following error:

ReflectionException in Container.php line 736: Class json-response-headers does not exist

With the following call stack:

in Container.php line 736
at ReflectionClass->__construct('json-response-headers') in Container.php line 736
at Container->build('json-response-headers', array()) in Container.php line 626
at Container->make('json-response-headers', array()) in Application.php line 674
at Application->make('json-response-headers') in Pipeline.php line 123
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in Router.php line 706
at Router->runRouteWithinStack(object(Route), object(Request)) in Router.php line 671
at Router->dispatchToRoute(object(Request)) in Router.php line 631
at Router->dispatch(object(Request)) in Kernel.php line 236
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 139
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in ShareErrorsFromSession.php line 54
at ShareErrorsFromSession->handle(object(Request), object(Closure))
at call_user_func_array(array(object(ShareErrorsFromSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in StartSession.php line 62
at StartSession->handle(object(Request), object(Closure))
at call_user_func_array(array(object(StartSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in CheckForMaintenanceMode.php line 42
at CheckForMaintenanceMode->handle(object(Request), object(Closure))
at call_user_func_array(array(object(CheckForMaintenanceMode), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in Kernel.php line 122
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 87
at Kernel->handle(object(Request)) in index.php line 53

This is a project that I started in Laravel 5.0 but upgraded to 5.1 part-way through. I'm running it on IIS/Windows Server 2008 R2 on my development machine, and IIS/Windows Server 20112 R2 for production.

I have tried dumping the composer autoload and re-optimising.

composer dump-autoload
php artisan clear-compiled
php artisan optimize

If I change routes.php to use the full middleware path, it works fine, eg:

// system
Route::group( [
    'prefix' => 'system',
    'middleware' => [
        \App\Http\Middleware\JsonResponseHeaders::class // <-- this has changed
    ]
], function()
{
    Route::get( 'status', 'SystemController@status' );
} );

If composer dump-autoload not working then try this. Firstly remove composer.lock file from your root path and run composer update --no-scripts . Hope it will work.

I solved the issue. It turns out Wincache was breaking my code. I added the following and it works fine now:

if( ini_get( 'wincache.ocenabled' ) )
{
    ini_set( 'wincache.ocenabled', '0' );
}