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.
$request->attributes->add(['myAttribute' => 'myValue']);
– Ben Dubuisson Nov 8 '16 at 1:28