Laravel5 OldMiddleware该页面没有正确重定向

时间:2015-05-21 12:59:09

标签: laravel-5 laravel-routing

我只是第一次看laravel5 因此,在新的安装中,我开始玩游戏 (像往常一样:))

php artisan make:中间件OldMiddleware

<?php namespace App\Http\Middleware;

use Closure;

class OldMiddleware {

    /**
     * Run the request filter.
     *
     * @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);
    }

}

<?php namespace App\Http;

use Illuminate\Foundation\Http\Kernel as HttpKernel;

class Kernel extends HttpKernel {

    /**
     * The application's global HTTP middleware stack.
     *
     * @var array
     */
    protected $middleware = [
        'Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode',
        'Illuminate\Cookie\Middleware\EncryptCookies',
        'Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse',
        'Illuminate\Session\Middleware\StartSession',
        'Illuminate\View\Middleware\ShareErrorsFromSession',
        'App\Http\Middleware\VerifyCsrfToken',
        'App\Http\Middleware\OldMiddleware',
    ];

    /**
     * The application's route middleware.
     *
     * @var array
     */
    protected $routeMiddleware = [
        'auth' => 'App\Http\Middleware\Authenticate',
        'auth.basic' => 'Illuminate\Auth\Middleware\AuthenticateWithBasicAuth',
        'guest' => 'App\Http\Middleware\RedirectIfAuthenticated',
    ];

}

当我点击

http://localhost/l5/public/

有重定向到

http://localhost/l5/public/home

带有消息

该页面未正确重定向

问题是什么?

我刚试过https://stackoverflow.com/a/30116118但仍然没有工作:(

1 个答案:

答案 0 :(得分:2)

将其放入$routeMiddleware ..

protected $routeMiddleware = [
    'home' => 'App\Http\Middleware\OldMiddleware',
];

并在你的路线..

Route::get('/',  ['middleware' => 'home'], function() {
    return "blah";
}
Route::get('/home', function() {
    return "home";
}

然后,如果您转到example.com/,则转到中间件并将您重定向到/home

The page isn't redirecting properly是因为发生了循环。

PS:如果你不想内置登录等,你可以做

artisan fresh

..如果你只是想玩,有时最好重新开始! ;)