如何覆盖供应商类文件?

时间:2017-04-10 16:28:35

标签: laravel override

我的laravel版本:5.2

我想覆盖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);
            }

例如,我想在未提供令牌时返回自定义消息。

if (! $token = $this->auth->setRequest($request)->getToken()) {

            return my_custom_message;
        }

由于我无法直接在此文件上进行更改,我应该如何创建自定义类来覆盖供应商的GetUserFromToken.php?

如果可能,请详细说明所需步骤。感谢。

更新#2

Kernel.php

protected $routeMiddleware = [

    'jwt.auth' => \App\Http\override\MyGetUserFromToken::class,

我在下面的目录中创建了GetUserFromToken.php。 myProject的\应用\ HTTP \倍率\ 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;
        }

1 个答案:

答案 0 :(得分:1)

由于它只是一个中间件,你应该能够扩展这个类:

class MyGetUserFromToken extends GetUserFromToken 

并在Kernel.php中将此类列为中间件。像这样:

'jwt.auth' => App\MyGetUserFromToken::class