在函数中两次使用Config :: set()的方式 [英] Way to use Config::set() twice in a function

查看:97
本文介绍了在函数中两次使用Config :: set()的方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经用Google搜索和stackoverflowed(如果可以的话),却找不到一个足够简洁的答案:(一些参考文献: laravel-4-当使用configset更改auth-model-then-authuser-not-wor> Laravel 4:使用Config :: set更改auth.model时,Auth :: user()不起作用, Laravel 5.2如何使用config :: set在中间件中)

如何在运行时使用Laravel 5.2.*中的Config::set()成功设置两次变量?

以下是我尝试实现的示例:

我有两个表companiesusers(它们都使用JWTAuth从不同的路由登录).现在,我想在此单个路径Route::get('/computers')

上获取所有computers记录

现在的问题是,我想使用相同的中间件来执行此操作,但是我想确保其中的任何用户(即公司或用户)在通过身份验证之前可以访问此资源

这是我尝试在中间件中使用Config::set()的内容:

//all.auth middleware
public function handle($request, Closure $next)
{
    Config::set('auth.providers.users.model', \App\Company::class);
    Config::set('jwt.user', \App\Company::class);

      //check if the request is for company
    if($company = JWTAuth::toUser(($request->has('token')) ? $request->token : $request->bearerToken())) 
    {
        return ['COMPANY' => $company];
    }

    //Unfortunately its not company, lets try users

    Config::set('auth.providers.users.model', \App\User::class);
    Config::set('jwt.user', \App\User::class);

    if($user = JWTAuth::toUser(($request->has('token')) ? $request->token : $request->bearerToken())) 
    {
        return ['USER' => $user];
    }
    throw new NotFoundHttpException('Your account could not be found.');
    ...............
}

现在,我注意到的行为是它成功地将JWT所使用的模型更改为Company,但并未将其更改为User,以防随请求一起发送的令牌是用于User的.

如果有人可以帮助我,我将不胜感激,至少了解这是否可行.

如果需要提供更多解释,我已经准备好了.

解决方案

@ jedrzej.kurylo指出,在启动JWTAuth外观之前按预期多次设置配置值,但是JWTAuth外观只能解析一次.因此,您必须强制忘记外观并重新解析外观才能更改配置.

在Lars 5.5中,jedrzej答案中提出的代码对我不起作用,似乎外观没有被正确地忘记.

通过消除app()->getBindings()中列出的其他实例的消除过程,我可以使用以下方法重置外观.

JWTAuth::clearResolvedInstances();
app()->forgetInstance('tymon.jwt.auth');
app()->forgetInstance('tymon.jwt.provider.user');

希望这对其他人正在挣扎的人有帮助.

I have googled and stackoverflowed (if you may) for this question, and couldn't find a succint enough answer to it: (some ref: Laravel 4: when using Config::set to change auth.model then Auth::user() not work , Laravel 5.2 how to use config::set in middleware)

How can I successfully set a variable twice at runtime with Config::set() in Laravel 5.2.*?

Here is an example of what I tried to achieve:

I have two tables companies and users (they both login from different routes with JWTAuth). Now I want to fetch all computers record at this single route Route::get('/computers')

Now the issue have is that I want to use the same middleware to do that, but I want to ensure that any of those users (i.e company or user) is authenticated before they can access this resource

Here is what I have attempted trying to use Config::set() in my middleware:

//all.auth middleware
public function handle($request, Closure $next)
{
    Config::set('auth.providers.users.model', \App\Company::class);
    Config::set('jwt.user', \App\Company::class);

      //check if the request is for company
    if($company = JWTAuth::toUser(($request->has('token')) ? $request->token : $request->bearerToken())) 
    {
        return ['COMPANY' => $company];
    }

    //Unfortunately its not company, lets try users

    Config::set('auth.providers.users.model', \App\User::class);
    Config::set('jwt.user', \App\User::class);

    if($user = JWTAuth::toUser(($request->has('token')) ? $request->token : $request->bearerToken())) 
    {
        return ['USER' => $user];
    }
    throw new NotFoundHttpException('Your account could not be found.');
    ...............
}

Now, the behaviour I noticed is that it successfully changed the model JWT would use to Company but didn't change it to User in case the token sent with the request is for a User.

I will be grateful if someone can help out, to understand at least if this is possible or not.

If there is a need to provide more explanations, I am ready to do that.

解决方案

As @jedrzej.kurylo pointed out setting the config values multiple times before initiating the JWTAuth facade works as expected, however the JWTAuth facade is only resolved once. So you'll have to force the facade to be forgotten and re-resolved to get the config changes.

The code proposed in jedrzej answer didn't work for me with Laravel 5.5, it seemed like the facade was not being forgotten properly.

Through the process of elimination by forgetting additional instances listed in app()->getBindings() I was able to reset the facade with the following.

JWTAuth::clearResolvedInstances();
app()->forgetInstance('tymon.jwt.auth');
app()->forgetInstance('tymon.jwt.provider.user');

Hope this helps if anyone else is struggling.

这篇关于在函数中两次使用Config :: set()的方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆