我创建了一个中间件来将用户重定向到某个网页,除非某个用户已登录。我还想包含此中间件应用的路由的异常。然而,我得到错误'localhost重定向你太多次了'。为什么会这样?这是我的代码:
web.php
Route::get('/lanzamiento', function () {
return view('coming-soon');
})->name('coming.soon');
Kernel.php
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,
\App\Http\Middleware\ComingSoon::class,
];
ComingSoon.php (我创建的中间件)
namespace App\Http\Middleware;
use Closure;
use Auth;
class ComingSoon
{
public function handle($request, Closure $next)
{
if (!Auth::check()) {
return redirect()->route('coming.soon');
}
if (Auth::user()->email != 'joaquin@test.com') {
return redirect()->route('coming.soon');
}
return $next($request);
}
protected $except = [
'/lanzamiento'
];
}
提前致谢!
**编辑 - 路线列表**
+--------+----------------------------------------+-----------------------------------------------------------------+---------------------------------+---------------------------------------------------------------------------------+--------------+
| Domain | Method | URI | Name | Action | Middleware |
+--------+----------------------------------------+-----------------------------------------------------------------+---------------------------------+---------------------------------------------------------------------------------+--------------+
| | GET|HEAD | / | | Closure | web |
| | POST | alquilar/detallealquiler/{id}/{horariollegada}/{horariopartida} | alquiler.detalle | App\Http\Controllers\EspaciosController@detalleAlquiler | web |
| | POST | alquilar/disponible/{id}/{horariollegada}/{horariopartida} | alquiler.disponible | App\Http\Controllers\EspaciosController@disponible | web |
| | POST | alquilar/{id} | alquilar | App\Http\Controllers\AlquileresController@alquilar | web |
| | GET|HEAD | anfitrion | | Closure | web |
| | GET|HEAD | api/user | | Closure | api,auth:api |
| | POST | cargar-vehiculo/datos | create.upload.vehicle | App\Http\Controllers\UploadVehicleController@UploadVehicle | web,auth |
| | GET|HEAD | cargar-vehiculo/datos | show.upload.vehicle | App\Http\Controllers\UploadVehicleController@showUploadVehicle | web,auth |
| | GET|HEAD | cargar-vehiculo/datos/editar/{id?} | show.edit.vehicle | App\Http\Controllers\UploadVehicleController@showEditVehicle | web,auth |
| | PUT | cargar-vehiculo/datos/editar/{id} | edit.vehicle | App\Http\Controllers\UploadVehicleController@editVehicle | web,auth |
| | DELETE | cargar-vehiculo/datos/{id} | delete.vehicle | App\Http\Controllers\UploadVehicleController@deleteVehicle | web,auth |
| | GET|HEAD | creditos | | Closure | web |
| | GET|HEAD | espacio/{id} | show.espacio | App\Http\Controllers\EspaciosController@showEspacio | web |
| | GET|HEAD | faqs | | Closure | web |
| | GET|HEAD | home | home | App\Http\Controllers\HomeController@index | web |
| | POST | lanzamiento | | App\Http\Controllers\ContactController@sendContact | web |
| | GET|HEAD | lanzamiento | coming.soon | Closure | web |
| | GET|HEAD | login | login | App\Http\Controllers\Auth\LoginController@showLoginForm | web,guest |
| | POST | login | | App\Http\Controllers\Auth\LoginController@login | web,guest |
| | GET|HEAD | login/{provider} | | App\Http\Controllers\Auth\SocialAuthController@redirectToProvider | web,guest |
| | GET|HEAD | login/{provider}/callback | | App\Http\Controllers\Auth\SocialAuthController@handleProviderCallback | web,guest |
| | POST | logout | logout | App\Http\Controllers\Auth\LoginController@logout | web |
| | GET|HEAD | mantenimiento | | Closure | web |
| | POST | password/email | password.email | App\Http\Controllers\Auth\ForgotPasswordController@sendResetLinkEmail | web,guest |
| | POST | password/reset | | App\Http\Controllers\Auth\ResetPasswordController@reset | web,guest |
| | GET|HEAD | password/reset | password.request | App\Http\Controllers\Auth\ForgotPasswordController@showLinkRequestForm | web,guest |
| | GET|HEAD | password/reset/{token} | password.reset | App\Http\Controllers\Auth\ResetPasswordController@showResetForm | web,guest |
| | GET|HEAD | perfil | profile | App\Http\Controllers\ProfileController@mostrarPerfil | web,auth |
| | POST | perfil/editar-imagen | update_profile_image | App\Http\Controllers\ProfileController@updateProfileImage | web |
| | GET|HEAD | perfil/editar-imagen | show.update.profile.image | App\Http\Controllers\ProfileController@showUpdateProfileImage | web |
| | GET|HEAD | politica-y-privacidad | | Closure | web |
| | GET|HEAD | quienes-somos | | Closure | web |
| | POST | register | | App\Http\Controllers\Auth\RegisterController@register | web,guest |
| | GET|HEAD | register | register | App\Http\Controllers\Auth\RegisterController@showRegistrationForm | web,guest |
| | GET|HEAD | resultados | show.search | App\Http\Controllers\EspaciosController@search | web |
| | GET|HEAD | signin | | App\Http\Controllers\Auth\LoginController@showLoginForm | web,guest |
| | GET|HEAD | signup | | App\Http\Controllers\Auth\RegisterController@showRegistrationForm | web,guest |
| | GET|HEAD | testfunction | test | App\Http\Controllers\EspaciosController@test | web |
| | GET|HEAD | upload-espacio/diasyhorarios/{espacio} | upload.espacio.3 | App\Http\Controllers\UploadEspacioController@showUploadEspacio3 | web,auth |
| | PUT | upload-espacio/diasyhorarios/{id} | insert.upload.espacio.3 | App\Http\Controllers\UploadEspacioController@insertAndShowUploadEspacio3 | web,auth |
| | DELETE | upload-espacio/espacio/{id} | delete.espacio | App\Http\Controllers\UploadEspacioController@deleteEspacio | web,auth |
| | POST | upload-espacio/estadias | create.espacio.upload.espacio.2 | App\Http\Controllers\UploadEspacioController@createEspacioAndShowUploadEspacio2 | web,auth |
| | GET|HEAD | upload-espacio/estadias/{espacio} | upload.espacio.2 | App\Http\Controllers\UploadEspacioController@showUploadEspacio2 | web,auth |
| | PUT | upload-espacio/estadias/{id} | insert.upload.espacio.2 | App\Http\Controllers\UploadEspacioController@insertAndShowUploadEspacio2 | web,auth |
| | GET|HEAD|POST|PUT|PATCH|DELETE|OPTIONS | upload-espacio/infogeneral/editar/{espacio} | editar.upload.espacio.1 | App\Http\Controllers\UploadEspacioController@showEditarUploadEspacio1 | web,auth |
| | DELETE | upload-espacio/infogeneral/eliminarfoto/{id} | deletepic.upload.espacio | App\Http\Controllers\UploadEspacioController@deletePicEspacio | web,auth |
| | GET|HEAD | upload-espacio/infogeneral/{espacio?} | upload.espacio.1 | App\Http\Controllers\UploadEspacioController@showUploadEspacio1 | web,auth |
| | GET|HEAD | upload-espacio/precios/{espacio} | upload.espacio.4 | App\Http\Controllers\UploadEspacioController@showUploadEspacio4 | web,auth |
| | PUT | upload-espacio/precios/{id} | insert.upload.espacio.4 | App\Http\Controllers\UploadEspacioController@insertAndShowUploadEspacio4 | web,auth |
| | GET|HEAD | upload-espacio/resumen/{espacio} | upload.espacio.resumen | App\Http\Controllers\UploadEspacioController@showUploadEspacioResumen | web,auth |
| | PUT | upload-espacio/resumen/{id} | insert.upload.espacio.resumen | App\Http\Controllers\UploadEspacioController@insertAndShowUploadEspacioResumen | web,auth |
+--------+----------------------------------------+-----------------------------------------------------------------+---------------------------------+---------------------------------------------------------------------------------+--------------+
答案 0 :(得分:1)
原因是当您重定向时,用户再次进入服务器并点击相同的中间件。您应该检查路由名称以排除它。您必须自己编写排除逻辑。
namespace App\Http\Middleware;
use Closure;
use Auth;
class ComingSoon
{
protected $exceptRoutes = [
'coming.soon'
];
public function handle($request, Closure $next)
{
$route = $request->route()->getName();
if(!in_array($route, $this->exceptRoutes)) {
if (!Auth::check() or Auth::user()->email !== 'joaquin@test.com') {
return redirect()->route('coming.soon');
}
}
return $next($request);
}
}