Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

camlynfx's avatar

How to get location from a response?

In lesson 10 of "Lets build a forum with Laravel" series, Jefferey did this dd($response->headers->get('Location')); to get the location like http://forum.dev/threads/some-channel/5. However, I get 'null' only. I am using Laravel 5.5.32 so I am assuming that key isn't available any more. Is there any other method to achieve the same.

Here's the test method -

/** @test */
function an_authenticated_user_can_create_new_forum_threads()
{
    $this->signIn();
    $thread = make('App\Thread');
    $response = $this->post('/threads', $thread->toArray());
    $this->get($response->headers->get('Location'))
        ->assertSee($thread->title)
        ->assertSee($thread->body);
}
0 likes
4 replies
skliche's avatar
skliche
Best Answer
Level 42

@camlynfx What is the HTTP status code of the response? The location header would not be sent with an HTTP status code of 200. You would get it with a 302. So unless you redirect (return redirect(...)) after storing the new thread you won't get that header.

1 like
camlynfx's avatar

Hi @skliche, Spot on, it was indeed 200. I was missing "return" in my Store method as I had put "redirect($thread->path());" only. I corrected it to return redirect($thread->path()); and it gives me the 302 status code. This resolves my issue. Thank you very much.

1 like
skliche's avatar

@camlynfx Great to see that you found the cause, a missing ˋreturnˋ is easily missed.

In tests like this you might want to assert the correct status code first. In most cases it seems to be unnecessary but I like to check the code so I don‘t get confused when other assertions fail.

camlynfx's avatar

@skliche yes that's good point to add status code check - it will be very helpful in debugging. I am new to laravel so those little tips are very helpful indeed. Cheers :)

Please or to participate in this conversation.