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:

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

also others sugested using flash:

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

but I am not sure if that is best practice, or if there is a better way to do this? Here is my Middleware and route:

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?...
    ]);
}]);

FYI the reason I decided to use a middleware for this is because I plan to cache the token for its lifecycle otherwise this would be a horrible implementation, since I would request a new token on every request.

shareimprove this question
 

2 Answers 正确答案

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');
shareimprove this answer
 
3 
note for Laravel 5 this is how to add parameters in the request: $request->attributes->add(['myAttribute' => 'myValue']); – Ben Dubuisson Nov 8 '16 at 1:28

I would leverage laravel's IOC container for this.

in your AppServiceProvider's register method

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

This will mean that wherever it you DI (dependancy inject) this class in your application, the same exact instance will be injected.

In your TwilioWorkspaceCapability class:

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();
}

Then, in your route:

Route::get('/manage', ['middleware' => 'twilio.workspace.capability', function (Request $request, TwilioWorkspaceCapability $twilio) {
    return view('demo.manage', [
        'manage_link_class' => 'active',
        'token' => $twilio->getToken(),
    ]);
}]);
来自 https://stackoverflow.com/questions/32228176/laravel-passing-variables-from-middleware-to-controller...