我在app/Http/Middleware/上创建了一个名为CorsMiddleware.php的中间件。这很简单,它已经在我的离线宅基地盒上运行。问题发生在我的远程计算机上:
ReflectionException in Container.php line 738:
Class App'Http'MiddleWare'CorsMiddleware does not exist
我的文件结构:
Middleware:
total 20
-rwxr-xr-x 1 CasatoroWeb CasatoroWeb 676 Mar 16 12:53 Authenticate.php
-rwxr-xr-x 1 CasatoroWeb CasatoroWeb 1570 Mar 16 12:53 CorsMiddleware.php
-rwxr-xr-x 1 CasatoroWeb CasatoroWeb 300 Mar 16 12:53 EncryptCookies.php
-rwxr-xr-x 1 CasatoroWeb CasatoroWeb 519 Mar 16 12:53 RedirectIfAuthenticated.php
-rwxr-xr-x 1 CasatoroWeb CasatoroWeb 311 Mar 16 12:53 VerifyCsrfToken.php
我的班级:
<?php namespace App'Http'Middleware;
use Closure;
class CorsMiddleware {
/**
* Run the request filter.
*
* @param 'Illuminate'Http'Request $request
* @param 'Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
/*
* Get the response like normal.
* When laravel cannot find the exact route it will try to find the same route for different methods
* If the method is OPTION and there are other methods for the uri,
* it will then return a 200 response with an Allow header
*
* Else it will throw an exception in which case the user is trying to do something it should not do.
*/
$response = $next($request);
// We only want the headers set for the api requests
if ($request->segment(1) == 'api') {
// Set the default headers for cors If you only want this for OPTION method put this in the if below
$response->headers->set('Access-Control-Allow-Origin','*');
$response->headers->set('Access-Control-Allow-Headers','Content-Type, X-Auth-Token, Origin, Authorization');
// Set the allowed methods for the specific uri if the request method is OPTION
if ($request->isMethod('options')) {
$response->headers->set(
'Access-Control-Allow-Methods',
$response->headers->get('Allow')
);
}
}
return $response;
}
}
我的内核.php
protected $middleware = [
'Illuminate'Foundation'Http'Middleware'CheckForMaintenanceMode::class,
'App'Http'MiddleWare'CorsMiddleware::class,
];
我尝试通过此处发布的解决方案来解决它 - https://laracasts.com/discuss/channels/general-discussion/l5-cant-get-custom-middleware-to-work-on-routes:
composer dump-autoload
php artisan clear-compiled
php artisan optimize
并且还通过运行
php artisan cache:clear
我不知道还能去哪里找。谁能指出我正确的方向?
Homestead 让我在中间件声明中侥幸逃脱了一个糟糕的情况:
protected $middleware = [
'Illuminate'Foundation'Http'Middleware'CheckForMaintenanceMode::class,
'App'Http'MiddleWare'CorsMiddleware::class,
];
我不得不将案例从中间件更改为中间ware。