Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP PHP User Authentication Building the Library Building the Book List

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,253 Points

Redirects aren't working

Hi all,

So I'm almost ready to get the book added but it's not working as we're working from inside the procedures folder. How does workspaces seem to know to redirect back outside that folder? All I get is object not found.

addBook.php
<?php


require_once __DIR__ . '/../inc/bootstrap.php';

$bookTitle = request()->get('title');
$bookDescription = request()->get('description');

try {
    $newBook = addBook($bookTitle, $bookDescription);

    $response = \Symfony\Component\HttpFoundation\Response::create(null, \Symfony\Component\HttpFoundation\Response::HTTP_FOUND, ['Location' => '/books.php']);
    $response->send();
    exit;

} catch(\Exception $e) {
    $response = \Symfony\Component\HttpFoundation\Response::create(null, \Symfony\Component\HttpFoundation\Response::HTTP_FOUND, ['Location' => '/add.php']);
    $response->send();
    exit;

}

```Object not found!

The requested URL was not found on this server. The link on the referring page seems to be wrong or outdated. Please inform the author of that page about the error.

If you think this is a server error, please contact the webmaster.

Error 404

Thanks
Antonio De Rose
Antonio De Rose
20,885 Points

Hey, could you please try below

header("Location:/books.php");

instead of ['Location' => '/books.php']

not working..tried to change to header but not working..anyone else has a clue??

1 Answer

Simon Coates
Simon Coates
28,694 Points

um, not sure what the problem is, but mine worked okay in workspaces with :

<?php

require_once "../inc/bootstrap.php";
$request = request();
$bookTitle = $request->get("title");
$description = $request->get("description");

try {
  $newBook = addBook($bookTitle, $description);

  $response =  \Symfony\Component\HttpFoundation\Response::create(
     null, // body of response, mixed type
    \Symfony\Component\HttpFoundation\Response::HTTP_FOUND, //status code.
    [
      "Location"=>'/books.php'
    ]
  );
  $response->send();
  exit;
} catch (\Exception $e){
  $response =  \Symfony\Component\HttpFoundation\Response::create(
     null, // body of response, mixed type
    \Symfony\Component\HttpFoundation\Response::HTTP_FOUND, //status code.
    [
      "Location"=>'/add.php'
    ]
  );
  $response->send();
  exit;
}

um, jumping out of the procedure dir is the result of the "/" in the url.