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') working in a strange way.

Hi there. I am working on a local machine. My problem is when Submit button is clicked leaving the input fields empty it should redirect to "http://localhost:7777/treehouse_php_project/contact" for me But, I am redirecting to "http://localhost:7777/contact" url. Can you guys Please help me to solve this? Thanks.

<?php

$app->post('/contact', function() use($app){
    $name = $app->request->post('name');
    $email = $app->request->post('email');
    $msg = $app->request->post('msg');

    if(!empty($name) && !empty($email) && !empty($msg)) {

    } else{
        //message the user was a problem
        $app->redirect('/contact');
    }
});

I have used "$app->redirect(DIR)" instead of "$app->redirect('/contact')". This worked for me. But, How to make my previous code work?

4 Answers

To me the easy fix was rename it to $app->redirect('contact');

Thanks for your feedback.

Thank you for sharing your easy fix! It worked great.

Greg Kaleka
Greg Kaleka
39,021 Points

Hm, so when you use a root relative url like '/contact' php will redirect to whatever the server is configured to think the root is. Your local server thinks the root is 'localhost:7777/treehouse_php_project'. Did you configure Apache with that url for a previous project?

A simple fix would be to use the full URL you want to redirect to - an absolute URL - or you could redirect to the existing page with '.' As the path. That's what I usually do, in fact.

Let me know if that works!

Thanks. I didn't configure Apache for a previous project. This line of code worked nicely for me. '$app->redirect(DIR.'/contact');'

Taking a look at the source code, this makes sense (your problem looks like it's solved - but this is just a deeper explanation why)

The redirect method sits in Slim\Http\Headers.php and is called from Slim\Slim.php:

<?php

// In Slim.php

    /**
     * Redirect
     *
     * This method immediately redirects to a new URL. By default,
     * this issues a 302 Found response; this is considered the default
     * generic redirect response. You may also specify another valid
     * 3xx status code if you want. This method will automatically set the
     * HTTP Location header for you using the URL parameter.
     *
     * @param  string   $url        The destination URL
     * @param  int      $status     The HTTP redirect status code (optional)
     */
    public function redirect($url, $status = 302)
    {
        $this->response->redirect($url, $status);
        $this->halt($status);
    }


// In Headers.php

    /**
     * Redirect
     *
     * This method prepares this response to return an HTTP Redirect response
     * to the HTTP client.
     *
     * @param string $url    The redirect destination
     * @param int    $status The redirect HTTP status code
     */
    public function redirect ($url, $status = 302)
    {
        $this->setStatus($status);
        $this->headers->set('Location', $url);
    }

So the string you pass in will work exactly the same as using the php header function (which is what it will eventually end up being). Therefore the difference is the same as using:

<?php

header("Location: /some-root-url");

instead of

<?php

header("Location: some-root-url");

If you want to see where the header() function is actually called, that's in Slim\Slim.php in the 'run' method. i.e. the very same method you call on $app:

<?php

$app->run();

I was able to get this to work by using

$app->redirect('contact');

and

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