5

I'm currently developing a web app using Laravel, and the app was working perfectly fine until recently. I had no idea what triggered it but here's a summary of the issue I'm having:

Logging in used to work as I have an AccountController that does this:

$auth = Auth::attempt(array(
    'username' => Input::get('username'),
    'password' => Input::get('password'),
    'active'=>1);
if ($auth) {
     return Redirect::route('home');
}

return Redirect::route('account-sign-in');

And the home route looks like so:

Route::get('/', array('as'=>'home', 'use'=>'HomeController@show'));

The app would usually return to home page immediately upon successful login. And in my home.blade.php, I would have an @if(Auth::check()) in place to make sure that once a user is logged in, the home page would serve a different set of texts.

Recently however, I noticed that after submitting a request to log in, there's an intermediate page showing a message "Redirecting to http://localhost.com/". The message was not there prior to this, and the errors started showing up along with this message.

I looked up all the information I can online, and someone suggested that there were line break/space issues with the source code. I looked at all the source code I had and nothing would've suggested there's something wrong.

Desperate at the time, I removed Redirect::route('home') and replaced that with View::make('home') instead. That stopped the message from showing up, and I'm able to login as usual again.

So I have two questions: 1) what is causing this odd issue? 2) is there anything wrong with using View::make() in this case vs Redirect::route()?

Thanks!

tereško
  • 58,060
  • 25
  • 98
  • 150
Zhia Chong
  • 51
  • 1
  • 3

6 Answers6

5

I faced same issue and after spending my whole weekend to find actual cause and fix this issue. I landed on this stackoverflow question and felt its same issue as that of mine.

I used following core php function to redirect instead of returning a view file from controller.

header('Location: /');

It printed actual file which had blank line. Removing this line fixed my problem.

There were thousands of files in my code base. My assumption was that I have tried different scripts to find such blank lines at start of any file and there was no such file as per those scripts results. I assumed there is is no blank line in any of my files. But header('Location: /') proved that my assumption was not correct and I was working on wrong lines.

Mohsin Saeed
  • 488
  • 5
  • 15
2

I had the same problem, the problem for me was I had a space in the base controller before the <?php tag

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Jitendra Tyagi
  • 173
  • 1
  • 6
1

Although Without seeing more of your setup, it will be tricky to debug, you do have an error within your route....

Route::get('/', array('as'=>'home', 'use'=>'HomeController@show'));

Should be:

Route::get('/', array('as'=>'home', 'uses'=>'HomeController@show'));

Note the 'uses' as opposed to 'use'.

Also, theres nothing wrong with Using View::make, however its possibly not the best logic to use, as the user will have content displayed that doesn't fit with the url. Best practice would be to redirect them as you currently are..... Just make sure the route is there for them to hit once redirected.

j5Dev
  • 2,022
  • 14
  • 18
  • Sorry typo there, it should've been 'uses'. Thanks for noticing that. What other things would you need in order to further debug the issue? Through trial and error, I came to the conclusion that the "Redirecting you to..." message was causing the error. Though the message itself is probably harmless, I am assuming there's something in my code that is triggering that message because it wasn't there previously. And using View::make instead of Redirect::route solved the issue. Hacky, I know. – Zhia Chong Jun 10 '14 at 17:50
  • Could you update the question with more of your codebase... Its hard to guess how you implemented your message without seeing the code, especially as it could be down to how you are filtering the auth redirects (filters, manually etc). Either drop it in the original question, or a pastebin may be better. – j5Dev Jun 12 '14 at 15:38
1

In my app the problem was caused by a cast to string:

public function myAction(): string {
   return redirect('/');
}

I had to remove : string to prevent the response from being cast to a string.

0

I was working on a group project. when deploying in production started to present this error. After a long time looking for the problem I discovered that it was an "echo" returned from a model (user) inserted by a collaborator. When removing this problem, the application returned to normal in the redirects

flpdev
  • 76
  • 3
0

in my case what was causing this was the call of a setStatusCode=201 in the middleware, I believe that this influences the standard status code of the redirect which is 302