2

I have a problem with the Slim framework (version 2). If I run on the default page it shows the content without a problem but when I make a request for a route like $app->get('/test'... I always get a 404 error.

This is my index.php.

<?php
require 'vendor/autoload.php';

\Slim\Slim::registerAutoloader();

$app = new Slim\Slim();

// Welcome message.
$app->get("/", function() use($app) {
    echo 'Appointment API<br />';
});

$app->get("/test", function () use($app) {
   echo "This is a test";
});

$app->run();

My .htaccess file:

RewriteEngine On

# Some hosts may require you to use the `RewriteBase` directive.
# If you need to use the `RewriteBase` directive, it should be the
# absolute physical path to the directory that contains this htaccess file.
#
#RewriteBase /

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

And my composer file:

{
    "require": {
        "slim/slim": "^2.6",
        "slim/extras": "*",
        "slim/middleware": "*"
    }
}

To this point I don't really know what to do.

alexw
  • 8,468
  • 6
  • 54
  • 86
  • is that 404 error provided by apache or slim? – Federkun Oct 04 '15 at 14:00
  • The error is provided by Slim. I get the default error page from slim – Bob van der Valk Oct 04 '15 at 14:01
  • 1
    Seems good to me. Can you try to access by `/index.php/test`? – Federkun Oct 04 '15 at 14:04
  • 1
    are you sure that that 404 error isn't from apache? you should access from `yoursite.com/test`, not `yoursite.com/test/`. Try to add `Options +FollowSymLinks` to your `.htaccess` – Federkun Oct 04 '15 at 14:21
  • 2
    Just to add to Federico's diagnosis, the fact that you can apparently access the page ok from `/index.php/test` implies that your .htaccess file is not being processed (at all), which is to do with your Apache config, rather than the "Slim Framework". – MrWhite Oct 04 '15 at 14:38
  • Ahh thank you guys for your help! – Bob van der Valk Oct 05 '15 at 09:27
  • Can you confirm that your `.htaccess` file is in the same dir as your `index.php`? If so, check this question on how to verify if your `.htaccess` is being read and applied: http://stackoverflow.com/questions/9234289/verify-if-htaccess-file-is-running – Gustavo Straube Oct 06 '15 at 13:56

2 Answers2

0

Use:

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

If you're still getting 404, then the problem is not your .htaccess.

Gustavo Straube
  • 3,744
  • 6
  • 39
  • 62
Qiniso
  • 2,587
  • 1
  • 24
  • 30
  • 1
    Why you simply removed the condition for when the request is not matching an existing directory? How do you think it'll fix the OP problem? – Gustavo Straube Oct 06 '15 at 13:51
0

If you getting 404 that means .htaccess issue if mode rewrite enabled.if not than enable it first then change .htaccess with following and it works:

<ifModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1 [NC,QSA,L]
</ifModule>
RVPatel
  • 41
  • 3