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 trialWilliam Bruntrager
11,473 PointsProblem with redirect on local server (XAMPP)
My redirect statements are not redirecting where I would like them to.
I am using XAMPP for Windows, and I have my project folder set up like so:
C:\xampp\htdocs\emerson
The two $app->get() statements in the code are working fine, so I can get to the main page by entering:
and the contact page at
http://localhost/emerson/contact
However, the
$app->redirect('/')
statement used after the email is sent in this lesson redirects my browser to:
How can I get the redirect to go back to the main page for this project, not the htdocs folder?
2 Answers
Jeff Lemay
14,268 PointsJust add your directory to the redirect path:
<?php
$app->redirect('/emerson/');
?>
Andrew Goninon
14,925 PointsUsing the following will achieve the same result:
$app->redirect('./contact');
$app->redirect('./');
William Bruntrager
11,473 PointsWilliam Bruntrager
11,473 PointsIt's a good solution but I was hoping for something more universal. For example, when I upload this project to a server the structure won't be the same, so I wonder if there is a way to write it where I won't have to change the code for local vs. online use.
Jeff Lemay
14,268 PointsJeff Lemay
14,268 PointsYou could set up a config.php file and then include it within the files of your project. Then in that config file, set a variable for the root directory. When you deploy to production, you only need to update that config file.
William Bruntrager
11,473 PointsWilliam Bruntrager
11,473 PointsSince the links created with Slim-views were working properly I looked for an equivalent function to use in the index.php file. The function I was looking for was called getRootUri(). So I rewrote the redirects as:
That works, and I believe I won't have to change the code at all in order to upload it.
Jeff Lemay
14,268 PointsJeff Lemay
14,268 PointsLooks good!
Just wanted to bring up a point, if this were my project, I'd set the method to a variable so you don't have to write so much each time you create a link. I'm not sure specifically where you would set this in Slim though.