Laravel return view not sending data, but echo view works

Viewed 757

I've stumbled across a rather strange issue. return view('myview', $data); works for everything except one unique set of route parameters. For example if I call:

http://example.com/category/692/626/0

The view is returned as expected. However if I call:

http://example.com/category/692/528/0

I am met with the following message:

This page isn’t working

example.com didn’t send any data.
ERR_EMPTY_RESPONSE

If I then echo the view at the end of the controller:

echo view('myview', $data);

Everything works as expected.

Upon examining nginx error.log I found the following line that is being written when this occurs:

2017/11/03 13:12:03 [alert] 498#0: *94 header already sent while sending response to client, client: 0.0.0.0, server: example.com, request: "GET /category/692/528/0 HTTP/1.1", subrequest: "/inc/footer.htm", host: "example.com"

I'm perplexed as to how this could be happening only when I attempt to return the view from the controller for this one particular set of parameters. It would make more sense if this error was happening all of the time.

I started to debug this issue by removing records that are returned, and removing one particular record fixes the issue, but there does not seem to be any difference between this record and any other that are returned (checked for hidden characters, nothing out of the ordinary).

Has anyone run across this issue in the past? Does anyone have any idea as to what could be causing this?

EDIT

Code below as requested. It's rather basic, just grab data, render a view and return it.

/* ROUTE in web.php
------------------------------------------------------*/
Route::get('/category/{category}/{subcategory}/{subsubcategory}', 'SearchController@category_search');


/* CONTROLLER FUNCTION
------------------------------------------------------*/
public function category_search($category_id, $subcategory_id, $subsubcategory_id, Request $request){

    // search_users() obtains resultset of users based on parameters passed.
    $users = $this->search_users($category_id, '', '', '');

    $data = array(
        'users'     => $users
    );

    // Data is obtained successfully and everything working as expected at this point
    //  echo '<pre>';
    //  echo var_export($data, true);
    //  echo '</pre>';
    //  die;

    //echo view('search/categories', $data); <-- This works
    return view('search/categories', $data); // <-- This does not

}
1 Answers

I have managed to rectify the issue, but I'm not at all convinced this is a solid fix. I'm not even sure how it could have fixed the issue. If anyone else has an idea as to why this worked, please let me know.

The original loop to display members in the .blade file is as follows:

@if(count($members) > 0)

    @php $count = 0; @endphp
    @foreach($members as $member)
        @php $count++; @endphp
        <div class="well {{($count % 2 == 0) ? 'results-well-even' : 'results-well-odd'}}">
            <a href="/members/{{$member->id}}">{{$member->name}}</a>
            <br/>
            <span class="hometown">
                {{$member->city}}, {{$member->state}}
            </span>
            <br/>
            @if((property_exists($member, "distance")))
            <span class="distance">
                Distance: {{$member->distance}} miles
            </span>
            @endif
        </div>

    @endforeach

@else



@endif

The revised loop is as follows:

@if(count($members) > 0)

    @php $count = 0; @endphp
    @foreach($members as $member)
        @php $count++; @endphp
        <!-- LINE BREAK ADDED HERE -->
        <div class="well {{($count % 2 == 0) ? 'results-well-even' : 'results-well-odd'}}">
            <a href="/members/{{$member->id}}">{{$member->name}}</a>
            <br/>
            <span class="hometown">
                {{$member->city}}, {{$member->state}}
            </span>
            <br/>
            @if((property_exists($member, "distance")))
            <span class="distance">
                Distance: {{$member->distance}} miles
            </span>
            @endif
        </div>

    @endforeach

@else



@endif

Literally the only thing that changed was a line break. I'm baffled by this. Does anyone know why this resolved the issue, because if so, you deserve the credit for the answer!

Related