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 Building Websites with PHP Contact Form & Sending Email Testing For POST Data

$app->redirect("/contact"); or $app->redirect("contact");

I'm working locally with MAMP. My project folder is newphp, it's inside of htdocs. When I write:

<?php
$app->redirect("/contact");
?>

It appears "404 Not Found". The Url is this: http://localhost:8888/contact

But when I chance to this:

<?php
$app->redirect("contact");
?>

It works. The Url is this: http://localhost:8888/newphp/contact

Can anyone explain me why? I was reading the documentation and every example starts with "/". Is the string inside parentheses a root-relative Url??? If it is, why these lines don't work when I type this Url http://localhost:8888/contact.

<?php
    $app->get('/contact', function () use($app){
        $app->render("contact.twig");
    })->name("contact");
?>

Thanks for your help. I'm sorry for my English.

7 Answers

My code is not on github. I don't know how to say this in English... But I think you can use google translate hehe. He notado que cuando escribo esto:

<?php
    //When I write a relative url this appears in the address bar:
    //http://localhost:8888/newphp/newphp/contact
    $app->redirect("newphp/contact");

    //And when I write a root relative url this appears:
    //http://localhost:8888/newphp/contact
    $app->redirect("/newphp/contact");
?>

I think when you use a relative url the redirect method takes the root of your project and when you use a root relative url (obviously) the redirect method takes the root of your server. Es por eso que cuando escribo:

<?php
     $app->redirect("contact");
?>

This works because it is a relative url. The method takes the root of my project: "/newphp/" and adds "contact".

me gusta! I think that means "I like it"

Yeah that's exactly what's going on - I think I have failed to explain my thought process there lol

If you don't have the project sitting in the root, this is a 'thing that happens'.

This question might be better answered referring to the php header function. I imagine Slim is just passing that string into the header function. The responses you've both described are what I would expect to happen.

But yes! You've solved your thing :)

Kevin Phillips
Kevin Phillips
15,693 Points

Yet another answer for the local MAMP folks. I define a base URL for my working folder in htdocs first: for me it's

define("BASE_URL","/php_notes/web_dev_slim");

then when setting up the route in the else block of this lesson one can use:

$app->redirect(BASE_URL . '/contact');
```.
The other answers certainly get you up and running with less code however, the approach of setting a base URL is something to keep in mind when going between development and production environments.

With the 'redirect' method, I'm guessing you can redirect to a defined route.

Here:

<?php

    $app->get('/contact', function () use($app){
        $app->render("contact.twig");
    })->name("contact");

you've defined a route at /contact and called that route 'contact'. If you redirect to '/contact,' you're going to go to

http://localhost:8888/contact

if you redirect to 'contact', the app will know to keep /newphp/ in the url.

http://localhost:8888/newphp/contact

I imagine this wouldn't be a problem if you weren't keeping the project in it's own directory.

Hope this helps :)

Hi Tom. Thanks for answering. If I change the name to "contactos":

<?php
    $app->get('/contact', function () use($app){
        $app->render("contact.twig");
    })->name("contactos");
?>

It still works $app->redirect("contact");

Hmm then it sounds like there's some magic going on behind the scenes!

If it is looking at the url path, then redirecting to '/contact' will remove '/newphp/' from the url.

Out of interest, what happens when you do:

<?php

    $app->get('/some-contact-url function () use($app){
        $app->render("contact.twig");
    })->name("contact");

// and then

$app->redirect("contact");

Also, is anything being cached?

I've had a poke around the documentation on redirect

it turns out that $app->redirect() takes the url as the argument.

E.g. this should work:

<?php
    $app->get('/newphp/contact', function () use($app){
        $app->render("contact.twig");
    })->name("contact");

$app->redirect("/newphp/contact");

// or

    $app->get('/contact', function () use($app){
        $app->render("contact.twig");
    })->name("contact");

$app->redirect("contact");

and this will NOT work

<?php

    $app->get('/newphp/contact', function () use($app){
        $app->render("contact.twig");
    })->name("contact");

$app->redirect("contact");

// or

$app->redirect("/contact");

When you specify the root or the url ('/contact') then it breaks your local setup because you're redirecting to '/contact' instead of '/newphp/contact' right?

So you can either stay as you were:

<?php

$app->redirect("contact");

and slim will add 'contact' to '/newphp/' OR If you want to use the named route you should go with:

<?php

$app->response->redirect($app->urlFor('contact'));

The reason why you're different from the documentation is because in your local setup, your project is not sitting in the root. You therefore cannot refer to the root when redirecting or you'll go to the wrong place!

Hope that helps.

El primer ejemplo no funciona. The first example doesn't work:

<?php
    $app->get('/newphp/contact', function () use($app){
        $app->render("contact.twig");
    })->name("contact");

    $app->redirect("/newphp/contact");
?>

No funciona porque ni siquiera me deja acceder a la página por medio de GET: 404 Page Not Found.

The second one works:

<?php
    $app->get('/contact', function () use($app){
        $app->render("contact.twig");
    })->name("contact");

    $app->redirect("contact");
?>

El problema no es con el GET, el GET solo funciona si lo escribo así (GET method isn't the problem. GET method only works if I write it like this):

<?php
    $app->get('/contact', function () use($app){
        $app->render("contact.twig");
    })->name("contact");
?>

La duda es con el redirect(). El cual funciona de las 2 formas siguientes. These examples work:

<?php
    $app->get('/contact', function () use($app){
        $app->render("contact.twig");
    })->name("contact");

    $app->redirect("contact");

    //AND

    $app->get('/contact', function () use($app){
        $app->render("contact.twig");
    })->name("contact");

    $app->redirect("/newphp/contact");
?>

¿Entonces cuál es el argumento que toma? $app->redirect("¿¿¿???"). El segundo lo puedo entender porque es un root-relative url redirect("/newphp/contact"). But I don't understand why redirect("contact") also works.

Say whaaaaaaatt! This seems totally strange. Is your code on github?

Also have you set any configuration things within the app to say you're within '/newphp/'?

Darryn Smith
Darryn Smith
32,043 Points

Chiming in here to say I have discovered precisely the same problem as the OP. I haven't figured out the cause of the behavior.

However, here is some added insight (or facemelting-confusion as the case may be...).

Throughout this course, I simultaneously built the project and wrote the code in both the Treehouse workspace AND in my own local MAMP setup.

When I got to this part of the course, which adds the 'redirect' code to the project, it worked fine in the workspace but broke to a '404' in MAMP.

I discovered the same fix as OP, but only in MAMP. The following gets the code to behave properly in MAMP:

    $app->redirect('contact');

... while the following works in the Treehouse workspace:

    $app->redirect('/contact');

Just to verify, I took the time to completely copy all of the working files (index.php, main.twig, contact.twig, about.twig) into my MAMP setup, in case a typo somewhere was the culprit for the discrepancy. But the results were the same. The slash works in the workspace and breaks locally.

Note, however, that the slash is (behaving as if it's) optional in the Treehouse workspace. Delete the slash in the redirect command, and the Treehouse workspace version of the page still behaves normally.

It's fine and all to have the working solution, but as with the OP, I think, I'd really like to know WHY this is happening.

(... and thanks to Tom for all the effort he's put in so far... :) )

If I write the root relative url it doesn't work:

<?php
    $app->get('/newphp/contact', function () use($app){
        $app->render("contact.twig");
    })->name("contact");
?>

$app->redirect("contact");

¡¡¡Muchísimas gracias Tom!!! Sin tu ayuda no hubiese podido llegar a esta conclusión. Thank you so much Tom!!! I couldn't have done this without your help. You are a great Treehouse Moderator.

No worries, thanks Eduardo! Seeing as you solved it, I've gone ahead and awarded you best answer.

Tom