Laravel5 OldMiddleware页面没有正确重定向(Laravel5 OldMiddle

2019-10-23 04:13发布

我只是把在laravel5第一次看这样一个新的安装,我开始玩弄(像往常一样:))

PHP的工匠制作:中间件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但仍然没有工作:(

Answer 1:

把它放在$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

..sometimes最好还是从头开始,如果你只是想玩弄! ;)



文章来源: Laravel5 OldMiddleware The page isn't redirecting properly