簡體   English   中英

車隊推車在路線中使用中間件,但我在項目中找不到任何 $routemiddleware……甚至在 kernel.php 中也找不到……我在哪里可以找到它?

[英]Fleet cart using middlewares in routes but i can not find any $routemiddleware in project…not even in kernel.php …where can i find it?

車隊推車在路線中使用中間件,但我在項目中找不到任何 $routemiddleware……甚至在 kernel.php 中也找不到……我在哪里可以找到它?

Laravel 版本:5.7


護照版本:7.5


CMS:車隊購物車


Kernel.php


namespace FleetCart\Http;

use Illuminate\Foundation\Http\Kernel as HttpKernel;

class Kernel extends HttpKernel
{
    /**
     * The application's global HTTP middleware stack.
     *
     * These middleware are run during every request to your application.
     *
     * @var array
     */
    protected $middleware = [
        \FleetCart\Http\Middleware\CheckForMaintenanceMode::class,
        \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
        \FleetCart\Http\Middleware\TrimStrings::class,
        \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
        \FleetCart\Http\Middleware\TrustProxies::class,
        \FleetCart\Http\Middleware\RedirectToInstallerIfNotInstalled::class,
        \FleetCart\Http\Middleware\RunUpdater::class,
    ];

    /**
     * The application's route middleware groups.
     *
     * @var array
     */
    protected $middlewareGroups = [
        'web' => [
            \FleetCart\Http\Middleware\EncryptCookies::class,
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
            \Illuminate\Session\Middleware\StartSession::class,
            \Illuminate\View\Middleware\ShareErrorsFromSession::class,
            \FleetCart\Http\Middleware\VerifyCsrfToken::class,
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
        ],
        'api' => [
            'throttle:60,1',
            'bindings',
        ],
    ];

    /**
     * The application's route middleware.
     *
     * These middleware may be assigned to groups or used individually.
     *
     * @var array
     */
    protected $routeMiddleware = [
        'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
        'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
    ];
}

\Modules\Accounts\Routes\public.php

Route::middleware('auth')->group(function () {
    Route::get('account', 'AccountDashboardController@index')->name('account.dashboard.index');

    Route::get('account/profile', 'AccountProfileController@edit')->name('account.profile.edit');
    Route::put('account/profile', 'AccountProfileController@update')->name('account.profile.update');

    Route::get('account/orders', 'AccountOrderController@index')->name('account.orders.index');
    Route::get('account/orders/{id}', 'AccountOrderController@show')->name('account.orders.show');

    Route::get('account/wishlist', 'AccountWishlistController@index')->name('account.wishlist.index');
    Route::delete('account/wishlist/{productId}', 'AccountWishlistController@destroy')->name('account.wishlist.destroy');

    Route::get('account/reviews', 'AccountReviewController@index')->name('account.reviews.index');
});

那個中間件('auth')來自哪里? 沒有任何其他 kernel 文件沒有任何其他中間件表示。 沒有什么.....!!

尋求幫助!

auth中間件在Modules/Core/Providers/CoreServiceProvider.php文件中注冊。

檢查registerMiddleware()方法。

要使用 Fleetcar 對 API 進行身份驗證,您需要創建一個 API 特定中間件並將其注冊到

Modules/Core/Providers/CoreServiceProvider.php

您現在將擁有兩個用於身份驗證的中間件,一個用於應用程序身份驗證,一個用於 api 身份驗證

 'auth' => \Modules\Core\Http\Middleware\Authenticate::class,
 'api' => \Modules\Core\Http\Middleware\APIAuthenticate::class,

在您的 APIAUthenticate 中間件 class 下,執行您的身份驗證檢查。 對於下面我正在檢查請求 header 是否包含不記名令牌,然后使用令牌檢查用戶的數據庫。

<?php

namespace Modules\Core\Http\Middleware;

use Closure;
use Log;
use Modules\User\Entities\User;

class APIAuthenticate
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  \Closure                 $next
     * @return \Illuminate\Http\Response
     */
    public function handle($request, Closure $next)
    {
    
        if ($request->header('Authorization')) {

            $key = explode(' ',$request->header('Authorization'));

            Log::info(json_encode($key));

            if(isset($key[1]) && !empty($key[1])){

                Log::info('key: '. $key[1]);

                $user = User::where('api_token', $key[1])->first();

                Log::debug('user', array($user));
                if(!empty($user)){
                   
                    return $next($request);

                    
                }else{
                    return response()->json(['error'=>'Unauthenticated']);
                }
            }
        }else{
            return response()->json(['error'=>'Unauthenticated']);
        }

        return response()->json(['error'=>'Unauthenticated']);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM