[SOLVED] Slim framework cannot make routing work

In Slim framework, cannot make routing work well as in the example provided on the site:

$app->get(‘/hello/:name’, function ($name) {
echo "Hello, $name";

Only / will work. All the other URLs like /hello/Bob will throw a 404.
Solution is to setup apache correctly.
1. In the same folder with your index.php create a .htaccess file:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]

2. In the httpd.conf of apache server put this at the end. Change as needed – mysite.com should be replaced with something like slim.localhost, also documentRoot and Directory should point to the correct path of apache:

<VirtualHost *:80>
ServerAdmin me@mysite.com
DocumentRoot "/path/www.mysite.com/public_html"
ServerName mysite.com
ServerAlias www.mysite.com

#ErrorLog "logs/mysite.com-error.log"
#CustomLog "logs/mysite.com-access.log" combined

<Directory "/path/www.mysite.com/public_html">
AllowOverride All
Order allow,deny
Allow from all

3. Restart apache

Leave a Reply

Your email address will not be published. Required fields are marked *