问题描述
我的laravel版本:5.2
My laravel version : 5.2
我想覆盖tymondesigns/jwt-auth GetUserFromToken.php
I want to override tymondesigns/jwt-auth GetUserFromToken.php
\vendor\tymon\jwt-auth\src\Middleware\GetUserFromToken.php
class GetUserFromToken extends BaseMiddleware
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, \Closure $next)
{
if (! $token = $this->auth->setRequest($request)->getToken()) {
return $this->respond('tymon.jwt.absent', 'token_not_provided', 400);
}
例如,我想在不提供令牌的情况下返回自定义消息.
For example, I want to return a custom message when token is not provided.
if (! $token = $this->auth->setRequest($request)->getToken()) {
return my_custom_message;
}
由于我不能直接在此文件上进行更改,因此我应该如何创建一个自定义类以覆盖供应商的GetUserFromToken.php?
since I cannot make changes directly on this file, how should I create a custom class to override vendor's GetUserFromToken.php ?
请尽可能详细描述所需的步骤.谢谢.
Please describe required steps in detail if possible. Thanks.
update#2
Kernel.php
Kernel.php
protected $routeMiddleware = [
'jwt.auth' => \App\Http\override\MyGetUserFromToken::class,
我在下面的目录中创建了GetUserFromToken.php.myProject \ app \ Http \ override \ MyGetUserFromToken.php
I created GetUserFromToken.php in below directory.myProject\app\Http\override\MyGetUserFromToken.php
<?php
namespace App\Services\Middleware;
use Tymon\JWTAuth\Middleware\GetUserFromToken;
class MyGetUserFromToken extends GetUserFromToken
{
public function handle($request, \Closure $next)
{
if (! $token = $this->auth->setRequest($request)->getToken()) {
return my_custom_message;
}
推荐答案
由于它只是中间件,因此您应该可以扩展此类:
Since it is simply a middleware, you should just be able to extend this class:
class MyGetUserFromToken extends GetUserFromToken
并将此类在Kernel.php
中列出作为中间件.像这样:
and list this class in Kernel.php
as middleware. Like this:
'jwt.auth' => App\MyGetUserFromToken::class
这篇关于如何覆盖供应商类文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!