user4737789
user4737789

Reputation:

can't add route in Slim

My problem is I can't add a route other than '/'. if I change / to /hello I get a 404 error. I think I have a mistake in my paths or .htaccess.

my .htaccess:

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

here is my code and my project structure

require '../../vendor/slim/slim/Slim/Slim.php';
\Slim\Slim::registerAutoloader();
require_once '../../vendor/autoload.php';
$app = new \Slim\Slim();
$app->get('/hello', function () {   //'/' works fine
    echo "Hello";
});
$app->run();

enter image description here

Upvotes: 1

Views: 765

Answers (1)

Gustavo Straube
Gustavo Straube

Reputation: 3861

In your .htaccess file you have the following rule:

RewriteRule ^ index.php [QSA,L] –

Since you're not specifying a path for index.php, Apache will try to load an index.php file in the current directory. But since that file is not there you'll get a response with error 404.

But since the .htaccess file is not under the directory you are accessing, it's not even loaded by the server. You need to do one of the following:

  1. Move index.php file to project root dir, as people suggested in the comments on your question (which is the best solution).
  2. Move .htaccess to the same directory as index.php (it seems to be DEVOLO_UI/form).

By the way, have you considered use only Composer's autoload? You don't need to call both autoloads: Slim's and Composer's. In your index.php you may write something like this:

// Set the current dir to application's root.
// You may have to change the path depending on 
// where you'll keep your index.php.
$path = realpath('../../');
chdir($path);

require 'vendor/autoload.php';

$app = new \Slim\Slim();

// Your routes followed by $app->run();
// ...

Upvotes: 1

Related Questions