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 Introducing MVC Frameworks in PHP Model View Controller Slim MVP

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,253 Points

Unable to set up route on localhost

Hi everyone,

I've looked and looked again at how to set up this MVP for this course. It works on my web server https://landing.jonniegrieve.co.uk/slimmin/hello/Jonnie (as of today 18th March) but not on localhost. I don't quite know how Alena opened the localhost browser either by URL or from the terminal

Maybe I'm supposed to keep the project at the localhost root for my local server (I use XAMPP, btw) but this doesn't work for me in my setup.

I know that my local server is working. It picks up the "page not find" error before I try to request the route. I don't even need to use a port number

RewriteEngine on
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]
<?php
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;

require 'vendor/autoload.php';

$app = new \Slim\App;
$app->get('/hello/{name}', 
    function (Request $request, Response $response, array $args) {
        $name = $args['name'];
        $response->getBody()->write("Hello, $name");

        return $response;
    }
);
$app->run();

3 Answers

jamesjones21
jamesjones21
9,260 Points

This would be the same for laravel, inside the xampp folder, you should see htdocs? Is the project inside of there?

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,253 Points

it is, but something like 4 directories inside that folder.. :-)

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,253 Points

I've got it! I knew I had to account for the place of the project in the filepath for my .htaccess file but nothing I tried yesterday seemed to work. Looks like I found the path I need now. Thanks for trying! :)

Ksenia Breitenmoser
Ksenia Breitenmoser
20,874 Points

Hey Jonathan! How did you solve the problem? I am using virtualbox with Debian on it. I set up a port 8099 on my localhost, and did everything what Alena did, but I keep getting the 404 error. Thanks a lot

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,253 Points

Hi Ksenia,

From my point of view, it depends on where your project is located within the root of your local server. That was the main issue with me, I was several layers inside the directory tree.

What worked for me was something like this

RewriteRule ^ path/to/project/index.php [QSA,L]

Make sure there's a space between RewriteRule the ^ and the file path. Good luck.

Ksenia Breitenmoser
Ksenia Breitenmoser
20,874 Points

Thanks a lot, Jonathan, though didn't seem to help me :( Did you change anything in RewriteBase / ?

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,253 Points

Not really, I left that alone. Keep at it, I'm sure you'll find the answer.

Is it possible there's something you need to do in your code as well?