车队推车在路线中使用中间件,但我在项目中找不到任何 $routemiddleware...甚至在 kernel.php 中也找不到...我在哪里可以找到它?
Posted
技术标签:
【中文标题】车队推车在路线中使用中间件,但我在项目中找不到任何 $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? 【发布时间】:2020-02-03 10:25:26 【问题描述】:Fleet cart 在路由中使用中间件,但我在项目中找不到任何 $routemiddleware...甚至在 kernel.php 中也找不到...我在哪里可以找到它?
Laravel 版本:5.7
护照版本:7.5
CMS : FleetCart
内核.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')是从哪里来的?没有任何其他内核文件,也没有任何其他中间件表示。没有.....!!
寻求帮助!
【问题讨论】:
auth 中间件来自 Laravel auth,它是框架文件,你不应该编辑这个文件。你可以阅读更多关于这个资源laravel.com/api/5.8/Illuminate/Auth/Middleware/… 如果我再次在内核中创建 'auth'.... 用户作为 Laravel 护照的 'auth:api' 中间件使用(来自 laravel 的'auth')我该如何处理?每次想要验证我的令牌时,它都会将我重定向到登录页面。我想要的只是评估我的令牌并获得'auth()->user()' 但是fleetcart没有使用laravel自己的'auth'中间件,它有一个Modules\Core\Http\Middleware\Authenticate.phppublic function handle($request, Closure $next) if (auth()->check()) return $next($request); $url = url()->full(); if (! $request->isMethod('get')) $url = url()->previous(); session()->put('url.intended', $url); return redirect()->route('login');
嘿@sahamnadeem 你能解决这个问题吗?如果是,如何?我遇到了同样的问题,尝试使用auth:api
,但它会将我重定向到登录页面,从您作为参考的 Fleetcar 的 Authenticate 模块中可以看出
我想继续为我所有的网络路由使用fleetcart,但也为我所有的/api/*
端点使用laravel的auth:api
中间件
【参考方案1】:
auth
中间件在Modules/Core/Providers/CoreServiceProvider.php
文件中注册。
检查registerMiddleware()
方法。
【讨论】:
我怎样才能继续为我的所有网络路由使用fleetcart,但也为我的所有/api/*端点/使用laravel的auth:api
中间件?谢谢!。目前所有请求都进入您在此处引用的CoreServiceProvider
,因此进入 Fleetcar 的自定义 Auth mw【参考方案2】:
要使用 Fleetcar 验证 API,您需要创建一个特定于 API 的中间件并将其注册到
Modules/Core/Providers/CoreServiceProvider.php
您现在将拥有两个用于身份验证的中间件,一个用于应用程序身份验证,一个用于 api 身份验证
'auth' => \Modules\Core\Http\Middleware\Authenticate::class,
'api' => \Modules\Core\Http\Middleware\APIAuthenticate::class,
在您的 APIAUthenticate 中间件类下,执行您的身份验证检查。对于下面我正在检查请求标头是否包含不记名令牌,然后使用令牌检查用户的数据库。
<?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']);
【讨论】:
以上是关于车队推车在路线中使用中间件,但我在项目中找不到任何 $routemiddleware...甚至在 kernel.php 中也找不到...我在哪里可以找到它?的主要内容,如果未能解决你的问题,请参考以下文章
/hibernate.cfg.xml 在 Intellij 中找不到