I just got started with Slim. My application for the moment is something like this:
<?php
require 'vendor/autoload.php';
$app = new \Slim\Slim([
'debug' => true
]);
var_dump($app->request());
$app->get('/:name', function ($name) {
echo "Hello, $name";
});
$app->get('/', function () {
echo 'hello world';
});
$app->run();
I am running it on localhost using PHP built in web server. For every request I try in the browser (or Postman, or CURL), what I get is always "hello world", as if the first route is not considered. Moreover, if I remove the second route, I always get a 404.
Am I forgetting something?
For debugging purposes, which HTTP header is used by SLIM to determine the route?
You need some kind of url rewriting for Slim to work. Since you are using internal PHP webserver you cannot use
mod_rewrite
. Instead createroute.php
file to same folder asindex.php
with following code.Then run it with
php -S localhost:8080 route.php
. Everything works now as expected.Eventually, I found out that the problem was given by a wrong document root.
I was launching the application from the main folder of my project using
php -S localhost:8080 public/index.php
and this caused thePATH_INFO
header of the HTTP request not to be compiled.Changing directory to
./public
and launching the app usingphp -S localhost:8080 index.php
solved the problemYou can't remove the second route
$app->get('/')
as it is the Home default route and it is quite normal to get a 404 because$app->get('/:name', function ($name) {})
is expecting a callback function's argument'name'
that is missing.Are you trying the following:
If this is the case then as a77icus5 suggested we may need to look into your htacess file and what is the project directory structure...
I have a fresh Slim Skeleton install and I thought I'd share my configuration with you...
My Web project directory is as follow :
In the first
.htaccess
located in the project root directory I added :Here
public
matches the name of the app public folderThen in the
.htaccess
located in the public folder I added :Then in SLIM -> Environment.php (line 142 - Virtual Path ) and try to edit as follow :