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 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:
http://localhost/mysite/ --- Outputs Hello World
http://localhost/mysite/marcosh --- Outputs a 404 ??
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 :
Webroot
-htaccess
- public
-- htaccess
-- assets
--- js
--- css
- templates
- app
- vendor
-- Slim
-- Twig
In the first .htaccess
located in the project root directory I added :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
Here public
matches the name of the app public folder
Then in the .htaccess
located in the public folder I added :
<IfModule mod_php5.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
</IfModule>
Then in SLIM -> Environment.php (line 142 - Virtual Path ) and try to edit as follow :
// Virtual path
// $env['PATH_INFO'] = substr_replace($requestUri, '', 0, strlen($physicalPath)); // <-- Remove physical path
$env['PATH_INFO'] = str_replace(str_replace('/public', "", dirname($_SERVER['PHP_SELF'])), '', "/".$requestUri); // remove public from URI
$env['PATH_INFO'] = str_replace('?' . $queryString, '', $env['PATH_INFO']); // <-- Remove query string
$env['PATH_INFO'] = '/' . ltrim($env['PATH_INFO'], '/'); // <-- Ensure leading slash
You need some kind of url rewriting for Slim to work. Since you are using internal PHP webserver you cannot use mod_rewrite
. Instead create route.php
file to same folder as index.php
with following code.
<?php
# Used only for running the app with internal PHP webserver
# php -S localhost:8080 route.php
if (file_exists(__DIR__ . "/" . $_SERVER["REQUEST_URI"])) {
return false;
} else {
include_once "index.php";
}
Then run it with php -S localhost:8080 route.php
. Everything works now as expected.
$ curl --include http://localhost:8080/foo
HTTP/1.1 200 OK
Host: localhost:8080
Connection: close
X-Powered-By: PHP/5.6.2
Content-type: text/html;charset=UTF-8
Hello, foo
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 the PATH_INFO
header of the HTTP request not to be compiled.
Changing directory to ./public
and launching the app using php -S localhost:8080 index.php
solved the problem