Laravel redirect::route is showing a message betwe

2019-02-15 09:54发布

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!

3条回答
男人必须洒脱
2楼-- · 2019-02-15 10:12

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.

查看更多
Juvenile、少年°
3楼-- · 2019-02-15 10:22

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

查看更多
萌系小妹纸
4楼-- · 2019-02-15 10:24

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.

查看更多
登录 后发表回答