简体   繁体   English

中间件不工作(Laravel Livewire)

[英]Middleware is not working (Laravel Livewire)

Middleware is not working (Laravel Livewire) I am trying to authenticate user using custom auth in laravel livewire but it is not working.中间件不工作 (Laravel Livewire) 我正在尝试在 laravel livewire 中使用自定义身份验证对用户进行身份验证,但它不工作。 It opens all the route whether user is authenticated or not无论用户是否通过身份验证,它都会打开所有路由

CustomAuth (This is my middleware file) CustomAuth (这是我的中间件文件)

<?php
namespace App\Http\Middleware;

use Closure;
use Session;
use Illuminate\Http\Request;
class CustomAuth
{
    public function handle(Request $request, Closure $next)
    {
        $path = $request->path();
        if(($path=='login' ||$path=='register') && Session::get('user'))
        {
            return redirect()->route('/userpanel');
        }
        else if(($path!='login' && !Session::get('user')) && ($path!='register' && !Session::get('user')))
        {
            return redirect()->route('/login');
        }
        return $next($request);
    }
}

Kernal.php (It is the Registration of middleware) Kernal.php (中间件注册)

<?php

namespace App\Http;
use Illuminate\Foundation\Http\Kernel as HttpKernel;

class Kernel extends HttpKernel
{
    protected $middleware = [
        // \App\Http\Middleware\TrustHosts::class,
        \App\Http\Middleware\TrustProxies::class,
        \Fruitcake\Cors\HandleCors::class,
        \App\Http\Middleware\PreventRequestsDuringMaintenance::class,
        \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
        \App\Http\Middleware\TrimStrings::class,
        \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
    ];

    protected $middlewareGroups = [
        'web' => [
            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
            \Illuminate\Session\Middleware\StartSession::class,
            //  \Illuminate\Session\Middleware\AuthenticateSession::class,
            \Illuminate\View\Middleware\ShareErrorsFromSession::class,
            \App\Http\Middleware\VerifyCsrfToken::class,
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
        ],

        'api' => [
            // \Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,
            'throttle:api',
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
        ],
    ];

    protected $routeMiddleware = [
        'auth' => \App\Http\Middleware\Authenticate::class,
        'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
        'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
        'can' => \Illuminate\Auth\Middleware\Authorize::class,
        'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
        'password.confirm' => \Illuminate\Auth\Middleware\RequirePassword::class,
        'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
        'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
        'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
        'customauth' => \App\Http\Middleware\CustomAuth::class,
    ];
}

web.php (This is my Route file) web.php (这是我的路由文件)

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Livewire\home;
use App\Http\Livewire\Gallery;
use App\Http\Livewire\Video;
use App\Http\Livewire\Faq;
use App\Http\Livewire\Team;
use App\Http\Livewire\Price;
use App\Http\Livewire\Auth\Login;
use App\Http\Livewire\Auth\Register;
use App\Http\Livewire\Userpanel;




Route::group(['middleware'=>'customauth'],function () { 
    Route::get('/register',Register::class)->name('register');
    Route::get('/login',Login::class)->name('login');
    Route::get('/', Home::class)->name('home')->middleware('auth');    
    Route::get('/gallery',Gallery::class)->name('gallery');
    Route::get('/video',Video::class)->name('video');
    Route::get('/faq',Faq::class)->name('faq');
    Route::get('/team',Team::class)->name('team');
    Route::get('/price',Price::class)->name('price');
    Route::get('/userpanel',Userpanel::class)->name('user');
});

this would be an example of the use of middleware in the route page这将是在路由页面中使用中间件的示例

Route::get('/nuevopoais', Nuevopoais::class)->middleware('can:Nuevopoais_listar')->name('nuevopoais') ; Route::get('/nuevopoais', Nuevopoais::class)->middleware('can:Nuevopoais_listar')->name('nuevopoais') ;

Livewire has the config to set a middleware for its routes. Livewire 具有为其路由设置中间件的配置。 Check this please.请检查这个。

https://laravel-livewire.com/docs/2.x/authorization https://laravel-livewire.com/docs/2.x/authorization

I got it.我知道了。 Middleware is not working because of route cache after running the following command it's work fine运行以下命令后,由于路由缓存,中间件无法正常工作它工作正常

php artisan route:cache

Thank you @Jesus Emanuel Becerra Santamar and @Mohamed Tahan for helping me!!!谢谢@Jesus Emanuel Becerra Santamar 和@Mohamed Tahan 帮助我!!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM