问题
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',
];
}
when I hit
http://localhost/l5/public/
there is a redirect to
http://localhost/l5/public/home
with the message
The page isn't redirecting properly
What's the problem ?
I've just tried https://stackoverflow.com/a/30116118 but still not working :(
回答1:
Put it in the $routeMiddleware..
protected $routeMiddleware = [
'home' => 'App\Http\Middleware\OldMiddleware',
];
and in your route..
Route::get('/', ['middleware' => 'home'], function() {
return "blah";
}
Route::get('/home', function() {
return "home";
}
Then if you go to example.com/ it go to the middleware and redirect's you to /home.
The The page isn't redirecting properly comes because a loop occurs.
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! ;)
来源:https://stackoverflow.com/questions/30374520/laravel5-oldmiddleware-the-page-isnt-redirecting-properly