2

I'm trying to pass a token from middleware to view and controller. But all the steps I've tried:

  1. Laravel - Passing variables from Middleware to controller/route
  2. Pass variable from middleware to templates
  3. Pass variable from middleware to view via controller in Laravel 5.2

Haven't helped much. Here is my setup:

Requests come in the form of:

https://website.com/reviews?linker=129b08e19014420049da7d6d7aa8fc35fc6279c4

Then gets parsed and checked by middleware:

Middleware

class CheckReviewLink
{
    /**
     * Check Review Link - granting clients access to submit review
     * =================
     * Check that user's link matches the 40 character string generated for user
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $response = $next($request);

        $url = $_SERVER['REQUEST_URI'];
        $parts = parse_url($url);
        $data['token'] = parse_str($parts['query'], $query);

        $testimonies = Testimony::all();

        foreach ($testimonies as $testimony) {
            if ($query['linker'] == $testimony->token)  {
              Session::flash('token', $data);
              return $next($request);

            }
        }


    }
}

** View **

<div class="col-lg-6">
   <article>
       <input disabled type="text" placeholder="{{Session::get('token', $data)}}" id="token" name="token" size="100" class="form-control border-form white font4light">
   </article>
 </div>

When I go to get the session data within my view/controller, I get the error:

Undefined variable: data

Any ideas anyone?

Community
  • 1
  • 1
sogeniusio
  • 111
  • 3
  • 14
  • 1
    see answer here https://stackoverflow.com/questions/30212390/laravel-middleware-return-variable-to-controller/31454023#31454023 – GWed Nov 15 '17 at 16:10

4 Answers4

0

You don't need to use a variable when you're trying to get data from the session. So, do this instead:

{{ Session::get('token') }}

Or simply:

{{ session('token') }}

Also, instead of flash() method do this:

session(['token' => $data]);

And then delete it manually if needed:

session()->forget('token');
Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279
0

Session flash storage is for retaining data between execution times of the application, ie passing from one page load to the other. In your case you are only wanting to pass data from one part of the app to the other during a single run, thus vars in the memory will be cleaner and quicker.

This guy answered it already: Laravel Middleware return variable to controller

You now have better and clearer control of the code and can pass through to, if required, your data to the views and don't have to worry about clearing up your old session data.

Community
  • 1
  • 1
0

First of all you don't need first line in your handle method. You aren't using $response variable.

Secondly use $request->url() instead of $_SERVER['REQUEST_URI'].

And your answer is to simply use session('token') to gat token you want.

zgabievi
  • 1,162
  • 6
  • 28
0

If you want to retrieve a session data, you just have to call the session() helper method, and give it the key of the data that you want:

$myToken = session('token');

But, in your case, your are storing an associative array to the session, so when you call it, you have to do like this:

$myToken = session('token')['token'];

So in your view, you will end up with:

<div class="col-lg-6">
   <article>
       <input disabled type="text" placeholder="{{ session('token')['token'] }}" id="token" name="token" size="100" class="form-control border-form white font4light">
   </article>
 </div>
Artenes Nogueira
  • 1,422
  • 1
  • 14
  • 23