Laravel 5.6中间件似乎不起作用

时间:2018-07-28 16:10:40

标签: php laravel-5.6

有人可以检查我的代码吗,我不知道为什么它不起作用。 网站上有带有密码字段的正在建设中的页面。只允许受邀用户访问其他页面,他们必须输入发送给他们的密码。 如果密码不正确或丢失,它们将被重定向回构建页面。这部分没问题。但是,当用户输入正确的密码时,他将获得“找不到页面”。 我的中间件:

    namespace App\Http\Middleware;

    use Closure; 
    use Illuminate\Support\Facades\Cookie;

    class CheckInvitation
    {
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        \Log::debug($_COOKIE['invitation']);
        if ($request->hasCookie('invitation')) {
            \Log::info('Found cookie');
            return $next($request);
        } else {
            return redirect('/under-construction');
        }

    }
}

控制器

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cookie;

class UnderConstructionController extends Controller
{
public function create() {
    return view('construction');
}

public function check(Request $request) {

    if ($request->invitation_pass === 'somepass') {
        //dd('pass works');
        \Log::info($_COOKIE);
        Cookie::queue(Cookie::make('invitation', 'You are invited', 
180));
        return redirect('/home');
    } else {
        return redirect('/under-construction');
    }
}
}

Kernel.php

 namespace App\Http;

    use Illuminate\Foundation\Http\Kernel as HttpKernel;

    class Kernel extends HttpKernel
    {
    /**
     * The application's global HTTP middleware stack.
     *
     * These middleware are run during every request to your application.
     *
     * @var array
     */
    protected $middleware = [

    \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
        \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
        \App\Http\Middleware\TrimStrings::class,

  \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
        \App\Http\Middleware\TrustProxies::class,
    ];

    /**
     * The application's route middleware groups.
     *
     * @var array
     */
    protected $middlewareGroups = [
        'web' => [
            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
            \Illuminate\Session\Middleware\StartSession::class,
            // \Illuminate\Session\Middleware\AuthenticateSession::class,
            \Illuminate\View\Middleware\ShareErrorsFromSession::class,
            \App\Http\Middleware\VerifyCsrfToken::class,
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
        ],

        'api' => [
            'throttle:60,1',
            'bindings',
        ],
    ];

    /**
     * The application's route middleware.
     *
     * These middleware may be assigned to groups or used individually.
     *
     * @var array
     */
    protected $routeMiddleware = [
        'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
        'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
        'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
        'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
        'can' => \Illuminate\Auth\Middleware\Authorize::class,
        'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
        'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
        'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
        'invitation' => \App\Http\Middleware\CheckInvitation::class,
    ];
}

web.php

    Route::get('/under-construction', 
    'UnderConstructionController@create');
    Route::post('/check-invitation', 'UnderConstructionController@check');
     Route::get('/catalog', 'ProductsController@index')->name('catalog');
    Route::get('/catalog/{id}', 'CatalogController@showProduct')->name('show- 
    product');

    Route::middleware(['invitation'], function() {
    Route::get('/', function() {
      return redirect('/home');
    });
    Route::get('/home', 'HomeController@setHome')->name('home');
   // Route::get('/catalog', 'ProductsController@index')->name('catalog');
    //Route::get('/catalog/{id}', 'CatalogController@showProduct')- 
    >name('show-product');
    Route::get('/sale', 'SaleController@setSale')->name('sale');
    Route::get('/services', 'ServicesController@setServices')- 
    >name('services');
    Route::get('/register', 'AuthController@showRegisterForm');
    Route::post('/register', 'AuthController@register');
    Route::get('/login', 'AuthController@showLoginForm')->name('login');
    Route::post('/login', 'AuthController@login');
    Route::get('/logout', 'AuthController@logout');
});

0 个答案:

没有答案