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.
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');