<small id='kpPAv'></small><noframes id='kpPAv'>

  1. <legend id='kpPAv'><style id='kpPAv'><dir id='kpPAv'><q id='kpPAv'></q></dir></style></legend>
      • <bdo id='kpPAv'></bdo><ul id='kpPAv'></ul>

      <i id='kpPAv'><tr id='kpPAv'><dt id='kpPAv'><q id='kpPAv'><span id='kpPAv'><b id='kpPAv'><form id='kpPAv'><ins id='kpPAv'></ins><ul id='kpPAv'></ul><sub id='kpPAv'></sub></form><legend id='kpPAv'></legend><bdo id='kpPAv'><pre id='kpPAv'><center id='kpPAv'></center></pre></bdo></b><th id='kpPAv'></th></span></q></dt></tr></i><div id='kpPAv'><tfoot id='kpPAv'></tfoot><dl id='kpPAv'><fieldset id='kpPAv'></fieldset></dl></div>
    1. <tfoot id='kpPAv'></tfoot>
    2. 如何在中间件中将用户对象绑定到请求

      How to bind user object to request in a middleware(如何在中间件中将用户对象绑定到请求)

        <small id='psCBq'></small><noframes id='psCBq'>

        1. <legend id='psCBq'><style id='psCBq'><dir id='psCBq'><q id='psCBq'></q></dir></style></legend>

          • <bdo id='psCBq'></bdo><ul id='psCBq'></ul>
                <tfoot id='psCBq'></tfoot>
                <i id='psCBq'><tr id='psCBq'><dt id='psCBq'><q id='psCBq'><span id='psCBq'><b id='psCBq'><form id='psCBq'><ins id='psCBq'></ins><ul id='psCBq'></ul><sub id='psCBq'></sub></form><legend id='psCBq'></legend><bdo id='psCBq'><pre id='psCBq'><center id='psCBq'></center></pre></bdo></b><th id='psCBq'></th></span></q></dt></tr></i><div id='psCBq'><tfoot id='psCBq'></tfoot><dl id='psCBq'><fieldset id='psCBq'></fieldset></dl></div>

                  <tbody id='psCBq'></tbody>
                本文介绍了如何在中间件中将用户对象绑定到请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在用 Laravel Spark 1.0 (Laravel 5.2) 编写一个应用程序.我为代理(api)身份验证编写了一个自定义中间件.这是代码:

                i'm writing an application in Laravel Spark 1.0 (Laravel 5.2). I wrote a custom middleware for agent (api) authentication. This is the code:

                <?php
                
                namespace AppHttpMiddleware;
                
                use AppAgent;
                use Closure;
                use IlluminateHttpRequest;
                
                class AgentAuth
                {
                    /**
                     * Handle an incoming request.
                     *
                     * @param  IlluminateHttpRequest  $request
                     * @param  Closure  $next
                     * @return mixed
                     */
                    public function handle($request, Closure $next)
                    {
                        if( isset($request->token) &&  !empty($request->token) )
                        {
                            $agent = Agent::where('token', '=', $request->token)->first();
                
                            if( $agent != NULL )
                            {
                                $team = $agent->Team()->first();
                                $user = $team->User()->first();
                
                                $request->merge(['team' => $team ]);
                                $request->merge(['user' => $user ]);
                
                                return $next($request);
                            }
                            else {
                                return response('Unauthorized 2.', 401);
                            }
                
                        }
                        else {
                            return response('Unauthorized 1.', 401);
                        }
                    }
                }
                

                在默认的 Laravel 身份验证中,用户对象被注入到请求中(参见 Laravel 文档):https://laravel.com/docs/5.2/authentication#retrieving-the-authenticated-user

                In the default laravel authentication the user object is injected in the request (see laravel docs): https://laravel.com/docs/5.2/authentication#retrieving-the-authenticated-user

                因此您可以使用以下方法检索用户:

                So you can retrieve the user using:

                $request->user();
                

                Spark 显然是用这个方法来检查用户订阅是否有效(laravelsparksrcHttpMiddlewareVerifyUserIsSubscribed):

                Spark obviously use this method to check if user subscription is valid (laravelsparksrcHttpMiddlewareVerifyUserIsSubscribed):

                if ($this->subscribed($request->user(), $subscription, $plan, func_num_args() === 2)) {
                            return $next($request);
                        }
                

                而且它不起作用,因为使用我的中间件,您可以使用以下方法检索用户:$request->user; 但不能使用 Laravel 默认值 $request->user();

                And it's not working because, with my middleware, you can retrieve the user using: $request->user; but not with the laravel defaults $request->user();

                我应该如何将用户对象注入到请求中?

                How should i inject the user object into the request?

                提前致谢

                服务提供者中的 Laravel (IlluminateAuthAuthServiceProvider@registerRequestRebindHandler)

                Laravel in the service provider (IlluminateAuthAuthServiceProvider@registerRequestRebindHandler)

                使用此代码将对象用户绑定到请求:

                Use this code to bind object user to the request:

                /**
                     * Register a resolver for the authenticated user.
                     *
                     * @return void
                     */
                    protected function registerRequestRebindHandler()
                    {
                        $this->app->rebinding('request', function ($app, $request) {
                            $request->setUserResolver(function ($guard = null) use ($app) {
                                return call_user_func($app['auth']->userResolver(), $guard);
                            });
                        });
                    }
                

                我尝试在中间件中插入此代码并进行适当的更正,但我不知道如何使其工作.

                I tried to insert this code, with the appropriate correction, in the middleware but i can't figure out how to make it work.

                推荐答案

                我没有 Spark 的副本来尝试这个 &确保我所做的对你来说是正确的,但我认为这会有所帮助:

                I don't have a copy of Spark to try this & ensure what I'm doing is correct for you, but I think this will help:

                1) 一个假设 - 我相信你是说是的,这条线会让你找到你想要的用户:

                1) An assumption - I believe you are saying that yes, this line will get you the user you want:

                $user = $team->User()->first();

                并且您只想将其绑定到请求,以便您稍后可以通过以下方式在您的应用中访问此用户:

                and you merely want to bind it to the request so that you can access this user later in your app via:

                $request->user()

                2) 如果这是真的,那么我所做的就是简化您提供的代码以添加:

                2) If this is true, then all I did was simplify the code you provided to add:

                $request->merge(['user' => $user ]);
                
                //add this
                $request->setUserResolver(function () use ($user) {
                    return $user;
                });
                
                // if you dump() you can now see the $request has it
                dump($request->user());
                
                return $next($request);
                

                我还在路由闭包中使用了 $request->user(),它就在那里.

                I also $request->user() in the route closure, and it is there.

                应用重新绑定对我来说有点奇怪,似乎没有必要.我不确定你正在做的事情是否真的需要这个.

                The app rebinding was a little strange to me, and didn't seem necessary. I'm not sure that anything would really need this for what you are doing.

                这篇关于如何在中间件中将用户对象绑定到请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
                PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
                mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
                Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
                Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
                Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)
              1. <i id='oAity'><tr id='oAity'><dt id='oAity'><q id='oAity'><span id='oAity'><b id='oAity'><form id='oAity'><ins id='oAity'></ins><ul id='oAity'></ul><sub id='oAity'></sub></form><legend id='oAity'></legend><bdo id='oAity'><pre id='oAity'><center id='oAity'></center></pre></bdo></b><th id='oAity'></th></span></q></dt></tr></i><div id='oAity'><tfoot id='oAity'></tfoot><dl id='oAity'><fieldset id='oAity'></fieldset></dl></div>

                      <small id='oAity'></small><noframes id='oAity'>

                        <tbody id='oAity'></tbody>

                      <tfoot id='oAity'></tfoot>
                    • <legend id='oAity'><style id='oAity'><dir id='oAity'><q id='oAity'></q></dir></style></legend>

                          <bdo id='oAity'></bdo><ul id='oAity'></ul>