2

Hi I am trying to do the following in Laravel 5.2.

Summary: Pass some variables from middleware to view, via controller

Detailed: When a client is logged in, no matter what route they want, we need to check if they have completed all "setup steps" (each step refers to a different route, e.g. one could be company info, another could be product settings, etc). If they have completed all setup steps, then let them proceed to their chosen route, otherwise we redirect to the appropriate "setup steps" route, whichever one they haven't completed yet.

All client controllers run the same middleware, called NonSuperAdmin. I would like to put the checking of "setup steps" in this middleware, and redirect from there as appropriate. If client is redirected to a setup route by this middleware, we need the "incompleteSetupTasks" collection to be passed on to the relevant view, via the appropriate setup steps controller method.

Is this possible? Any help is much appreciated.

Roman Ali
  • 65
  • 1
  • 8
  • You can return redirects inside the middleware which would greatly benefit your controllers by keeping all that logic outside of them and contained inside of your middleware. – user1669496 Jul 21 '16 at 15:12

3 Answers3

3

In the middleware use session handler

if($condition === true){
  $data = [ //your content ];
  Session::flash('your_key', $data);
}
next($request);

This data will also be available in your controller and in view

This is how you can access data in controller

public function  yourControllerAction($request)
{
   $somevariable = Session::get('your_key');
   $viewdata = [
      'content' => $somevariable
   ]
   return view('yourview', $viewdata); 
}

Or directly access the session data in view

//inblade
<p>
  Your html content
  @foreach(Session::get('your_key' as $data)
     //your stuff
  @endif
</p>
Max Gaurav
  • 1,835
  • 2
  • 13
  • 26
1

May be use Laravel Session to store and read values?

Kamil Kiełczewski
  • 85,173
  • 29
  • 368
  • 345
  • I tried that, and failed. Maybe I am not doing it right. Would you be kind enough to give me a basic example? – Roman Ali Jul 21 '16 at 15:09
  • I give you link in my answer to documentation - there are examples. But may be put your code here and I will look on it. I never use session in middelware so Im not 100% sure that my aboce advice is right - you must check it by yourself. – Kamil Kiełczewski Jul 21 '16 at 16:02
  • here you have some good video- tutorial: https://laracasts.com/series/laravel-5-from-scratch/episodes/15 , – Kamil Kiełczewski Jul 21 '16 at 16:13
0

You can pass your setup steps to get or post parameters and check in routes with middleware if these parameters are empty:

Route::get('post/{setup1?}/{setup2?}', ['middleware' => 'role:admin', function ($setup1, $setup2) {
    if(empty($setup1) and empty($setup2)){
        // do smth
    } else {
       // redirect
    }
}]);

Question marks mean, that they are optional parameters. Hope it was helpful.

Stas Coder
  • 309
  • 5
  • 10