Laravel5 OldMiddleware页面无法正确重定向 [英] Laravel5 OldMiddleware The page isn't redirecting properly
问题描述
我只是先看看laravel5 因此,在进行新安装时,我开始玩转 (和往常一样:))
I'm just taking the first look at laravel5 so with a new install I'm starting playing around (as usual :) )
php artisan make:middleware 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 ,但仍然无法正常工作:(
I've just tried https://stackoverflow.com/a/30116118 but still not working :(
推荐答案
将其放入$routeMiddleware
..
protected $routeMiddleware = [
'home' => 'App\Http\Middleware\OldMiddleware',
];
以及您的路线.
Route::get('/', ['middleware' => 'home'], function() {
return "blah";
}
Route::get('/home', function() {
return "home";
}
然后,如果您转到example.com/
,它将转到中间件并将您重定向到/home
.
Then if you go to example.com/
it go to the middleware and redirect's you to /home
.
The page isn't redirecting properly
之所以出现,是因为发生了循环.
The The page isn't redirecting properly
comes because a loop occurs.
PS::如果您不希望内置登录名等,可以这样做
PS: If you don't want the built in login etc. you can do
artisan fresh
..有时候,如果您只是想玩的话,最好重新开始! ;)
..sometimes it's better to start fresh if you just want to playing around! ;)
这篇关于Laravel5 OldMiddleware页面无法正确重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!