Routing issue with Slim framework

I just started with Slim. My application currently looks 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 run it on localhost using PHP built into the web server. For every request that I try to use in the browser (either Postman or CURL), I always get “hello world”, as if the first route was not considered. Moreover, if I delete the second route, I always get 404.

Did I forget something?

For debugging purposes, which HTTP header does SLIM use to determine the route?

+1
source share
3 answers

In the end, I found out that the problem was posed by the wrong document root.

I started the application from the main folder of my project using php -S localhost:8080 public/index.php , and this led to the fact that the PATH_INFO header of the HTTP request was not compiled.

Changing the directory to ./public and launching the application using php -S localhost:8080 index.php solved the problem

+1
source

You cannot delete the second route $app->get('/') , since this is the default route, and it’s normal to get 404, because $app->get('/:name', function ($name) {}) expects an argument to the feedback function 'name' , which is missing.

You are trying to do the following:

 http://localhost/mysite/ --- Outputs Hello World http://localhost/mysite/marcosh --- Outputs a 404 ?? 

If so, then, as a77icus5 suggested, we may need to look into your htacess file and what is the structure of the project structure ...

I have a fresh Slim Skeleton , and I decided to share my configuration with you ...

The directory of my web project is as follows:

 Webroot -htaccess - public -- htaccess -- assets --- js --- css - templates - app - vendor -- Slim -- Twig 

In the first .htaccess located in the root directory of the project, I added:

 <IfModule mod_rewrite.c> RewriteEngine On RewriteRule ^(.*)$ public/$1 [L] </IfModule> 

Here public corresponds to the name of the application’s public folder

Then in the .htaccess located in the shared 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 changing as follows:

  // 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 
+2
source

You need some kind of URL recycling for Slim to work. Since you are using the internal PHP web server, you cannot use mod_rewrite . Instead, create a route.php file in the same folder as index.php with the 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 using php -S localhost:8080 route.php . Everything is working 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 
+1
source

All Articles