Laravel - Passing variables from Middleware to controller/route

前端 未结 2 1970
旧巷少年郎
旧巷少年郎 2021-02-19 19:02

How can I pass variables from a middleware to a controller or a route that executes such middleware? I saw some post about appending it to the request like this:



        
2条回答
  •  一个人的身影
    2021-02-19 19:13

    pass key value pair like this

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

    or to your action

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

    You can pass data the view using with

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

    in your middleware

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

    in your controller

     return Request::get('token');
    

提交回复
热议问题