laracasts / PHP-Vars-To-Js-Transformer

Transform PHP data to JavaScript.
https://packagist.org/packages/laracasts/utilities
MIT License
2.21k stars 219 forks source link

Not working with redirects. #48

Closed neeravp closed 9 years ago

neeravp commented 9 years ago

I am trying to put the authenticated user's role to access it from angular front end. So in my AuthController

if ($this->auth->attempt(['email'=>$email, 'password' =>$password, 'status'=>'active'],$request->has('remember')))
{
    $user = $this->auth->user();
    JavaScript::put(['user' =>$user]);
    return redirect()->intended($this->redirectPath()); //doesn't work
    //But if I return view instead of a redirect as below it works
    return view('home'); //it works
}

What should I do to make it work with a redirect?

ShannonCronin commented 9 years ago

Redirects are not supported. You must pass your JavaScript data after the redirect has already taken place.

neeravp commented 9 years ago

I am sorry for responding this late. But I still can't figure out how to pass data after redirect. Any statement after return redirect() will be inaccessible.
For ex: In the following snippet from controller code, the JavaScript data assignment would be inaccessible.

if ($this->auth->attempt(['email'=>$email, 'password' =>$password,    'status'=>'active'],$request->has('remember')))
{
    $user = $this->auth->user();       
    return redirect()->intended($this->redirectPath()); 
     JavaScript::put(['user' =>$user]);  // this would be inaccessable
}
jeffwalsh commented 9 years ago

neeravp, it's because you are "returning" after the redirect. No code below a "return" will be processed - you have left that part of the ode and on to the intended redirect. You must pass the JavaScript in at a point like the intended redirect's Controller. If the redirect()->intended($this->redirectPath()) is something like AdminController@view, put it in that method.

neeravp commented 9 years ago

Thanks jeffwalsh. I didn't occur to me to put it in the intended's controller. I will give it a try. Thanks again.