【问题标题】:Laravel 5.2 - Override vendor function from MiddlewareLaravel 5.2 - 从中​​间件覆盖供应商功能
【发布时间】:2018-07-23 09:05:50
【问题描述】:

我正在处理一个包:

https://github.com/lucadegasperi/oauth2-server-laravel

我想在https://github.com/lucadegasperi/oauth2-server-laravel/blob/master/src/Authorizer.php 中覆盖一个函数“getResourceOwnerId()”

所以我需要重写该函数并将新函数“getAccessToken_custom”添加到类中,如下所示:

public function getResourceOwnerId()
    {
        $check = $this->getAccessToken_custom();

        if($check != 0){
            return $this->getAccessToken()->getSession()->getOwnerId();
        }else{
            return 0;
        }

    }

    public function getAccessToken_custom(){

        $accessToken = $this->getChecker()->getAccessToken();

        if (is_null($accessToken)) {

            return 0;

        }else{
            return $accessToken;
        }
        //return $accessToken;
    }

我正在像这样从我的中间件调用函数:

use LucaDegasperi\OAuth2Server\Facades\Authorizer;

public function handle($request, Closure $next)
    {
        // Return user ID or return 0
        $user_id = Authorizer::getResourceOwnerId();

        if($user_id == 0){
            return $next($request);
        }else{
            $user=\App\User::find($user_id);
            Auth::login($user);
            return $next($request);
        }

    }

你能帮我知道如何覆盖函数 vendor 吗?所以我可以返回我的自定义值。谢谢各位!

【问题讨论】:

    标签: php laravel oauth-2.0


    【解决方案1】:

    您可以在您的应用文件夹中创建 Authorizer.php 并放置此代码

    namespace app;
    
    class Authorizer extends \LucaDegasperi\OAuth2Server\Authorizer
    {
       public static function getResourceOwnerId()
       {
           //your code
       }
    }
    

    在中间件中使用你的Authorizer

    use app\Authorizer;
    
    public function handle($request, Closure $next){}
    

    【讨论】:

      猜你喜欢
      • 2019-02-20
      • 2018-04-08
      • 1970-01-01
      • 2018-06-04
      • 2021-04-22
      • 1970-01-01
      • 2014-01-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多