【问题标题】:Laravel - Passing variables from Middleware to controller/routeLaravel - 将变量从中间件传递到控制器/路由
【发布时间】:2015-11-20 14:01:45
【问题描述】:

如何将变量从中间件传递到控制器或执行此类中间件的路由?我看到一些关于将其附加到请求的帖子,如下所示:

$request->attributes->add(['key' => $value);

还有其他人建议使用闪光灯:

Session::flash('key', $value);

但我不确定这是否是最佳做法,或者是否有更好的方法来做到这一点?这是我的中间件和路由:

namespace App\Http\Middleware;

use Closure;

class TwilioWorkspaceCapability
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  \Closure $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $workspaceCapability = new \Services_Twilio_TaskRouter_Workspace_Capability("xxxxx", "xxxx", "xxxx");
        $workspaceCapability->allowFetchSubresources();
        $workspaceCapability->allowDeleteSubresources();
        $workspaceCapability->allowUpdatesSubresources();
        $token = $workspaceCapability->generateToken();
        //how do I pass variable $token back to the route that called this middleware
        return $next($request);
    }
}

Route::get('/manage', ['middleware' => 'twilio.workspace.capability', function (Request $request) {
    return view('demo.manage', [
        'manage_link_class' => 'active',
        'twilio_workspace_capability' => //how do I get the token here?...
    ]);
}]);

仅供参考,我决定为此使用中间件的原因是因为我计划在其生命周期内缓存令牌,否则这将是一个可怕的实现,因为我会在每个请求上请求一个新令牌。

【问题讨论】:

    标签: laravel laravel-middleware


    【解决方案1】:

    像这样传递键值对

    $route = route('routename',['id' => 1]);
    

    或者你的行动

    $url = action('UserController@profile', ['id' => 1]);
    

    您可以使用 with 向视图传递数据

     return view('demo.manage', [
        'manage_link_class' => 'active',
        'twilio_workspace_capability' => //how do I get the token here?...
    ]) -> with('token',$token);
    

    在你的中间件中

     public function handle($request, Closure $next)
     {
        $workspaceCapability = new .....
        ...
        $request -> attributes('token' => $token);
    
        return $next($request);
     }
    

    在你的控制器中

     return Request::get('token');
    

    【讨论】:

    • 注意 Laravel 5 这是如何在请求中添加参数:$request->attributes->add(['myAttribute' => 'myValue']);
    【解决方案2】:

    我会为此利用 laravel 的 IOC 容器。

    在您的 AppServiceProvider 的注册方法中

    $this->app->singleton(TwilioWorkspaceCapability::class, function() { return new TwilioWorkspaceCapability; });
    

    这意味着无论您在应用程序中对此类进行 DI(依赖注入)何处,都将注入完全相同的实例。

    在您的 TwilioWorkspaceCapability 类中:

    class TwilioWorkspaceCapability {
    
        /**
         * The twillio token
         * @var string
         */
        protected $token;
    
    
        /**
         * Get the current twilio token
         * @return string
         */
        public function getToken() {
            return $this->token;
        }
    
        ... and finally, in your handle method, replace the $token = ... line with:
        $this->token = $workspaceCapability->generateToken();
    }
    

    然后,在你的路线中:

    Route::get('/manage', ['middleware' => 'twilio.workspace.capability', function (Request $request, TwilioWorkspaceCapability $twilio) {
        return view('demo.manage', [
            'manage_link_class' => 'active',
            'token' => $twilio->getToken(),
        ]);
    }]);
    

    【讨论】:

      猜你喜欢
      • 2016-03-03
      • 1970-01-01
      • 2015-01-06
      • 2015-07-08
      • 1970-01-01
      • 2017-06-14
      • 1970-01-01
      • 1970-01-01
      • 2019-12-09
      相关资源
      最近更新 更多