Slim Framework always return 404 Error

58,410

Solution 1

Problem is solved!

My apache is actually normal, and the .htaccess file provided earlier also normal.

The clue is the URL that I used. Previously I used the invalid URL, thus it returned the 404 page error. I just realized it when I Tried to access the newer GET URL via browser with this one;

http://localhost/dev/index.php/getUsers/user1

and now that works!

I just realized it once I found these statements;

If Slim does not find routes with URIs that match the HTTP request URI, Slim will automatically return a 404 Not Found response.

If Slim finds routes with URIs that match the HTTP request URI but not the HTTP request method, Slim will automatically return a 405 Method Not Allowed response with an Allow: header whose value lists HTTP methods that are acceptable for the requested resource.

Solution 2

I found this post while googling "slimframework 404". The post led me to a solution to my problem.

The Problem

I set up a site with the Slim framework using Composer and create an index.php with the following example code form slimframework.com:

<?php
$app = new \Slim\Slim();
$app->get('/hello/:name', function ($name) {
    echo "Hello, $name";
});
$app->run();

Then I try to access the page using http://localhost/hello/bob. I get back a 404 Page Not Found page.

I was able to get to access the page using http://localhost/index.php/hello/bob.

The Solution

I found the solution by looking at /vendor/slim/.htaccess (included w/ Composer Slim install) and the URL Rewriting section of the Slim framework documentation.

I added a copy of the /vendor/slim/.htaccess file to the same folder as my index.php file. The contents of the file are:

RewriteEngine On

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

Now I can access the page using http://localhost/hello/bob.

Solution 3

You're right about to include the index.php on your file, but that happen because you're not working properly with your mod-rewrite Please check the following link:

https://github.com/codeguy/Slim

In the Get Started part you could see how to configure your server (in case that you were using apache / lighttpd / ngynx)

Solution 4

For me the problem was that I'd forgotten to provide a .htaccess file in my document root.

.htaccess

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]

Solution 5

I think your problem is at the "Resource parser" because you are no defining $id parameter at the request so, please, try this:

//1. Require Slim
require('Slim/Slim.php');

//2. Instantiate Slim
$app = new Slim();

//3. Define routes
$app->get('/books/:id/', function ($id) {
    echo json_encode( getBook($id) );
});

// some stuff
$app->run();

Please, tell us if it's ok

Share:
58,410

Related videos on Youtube

gumuruh
Author by

gumuruh

Highly creative and multi-talented Graphic Designer, Software Developer, and Audio Composer with extensive experience in several tools: Photoshop (Graphic Design), Visual Studio (Vb.net), Netbeans (java), Eclipse &amp; Notepad++ (html, php, and css) and Audacity (audio). I have collaborative and interpersonal skills; with a dynamic team player with well-developed written and verbal English communication abilities. Highly talented in building and maintaining long term “win-win” partnerships. Passionate and innovative in art and designs; accustomed to performing in deadline-driven environments with emphasis on working within budget requirements.

Updated on May 29, 2020

Comments

  • gumuruh
    gumuruh almost 4 years

    These days i'm using Slim Framework as my simplest tool to develop the php web api. Using these two articles:

    I follow some of the steps from there. Downloading the Slim Framework, putting the correct directory & files. Adjusting the initation statements such as;

    //1. Require Slim
    require('Slim/Slim.php');
    
    //2. Instantiate Slim
    $app = new Slim();
    
    //3. Define routes
    $app->get('/books', function ($id) {
        //Show book with id = $id
    });
    

    And then, I modify the rest accordingly.

    Such as my checklist that already done:

    • LoadModule rewrite_module modules/mod_rewrite.so -> enabled
    • Slim .htaccess:

    RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ bootstrap.php [QSA,L]

    But, after Once I run this statement;

    $app->run();
    

    And I run it on my browser.... then, I got 404 Error while testing it on my Localhost. What's the solution for fixing that?

    FYI, here is my simplest PHP file that i'm currently using it. (shared Link)

    • Omar McClellan
      Omar McClellan almost 5 years
      For people who come with this problem in the future ... let me help you with this issue! This problem can be solved not just by writting the right url, but also by having the right implementation... please visit this site github.com/codeguy/Slim and follow the steps, and voilà! you´ll get it hope it helps, have a great one everyone!
  • RPDeshaies
    RPDeshaies over 10 years
    Just to clarify the problem, what was the URL that you were trying to use before ?
  • anj
    anj almost 9 years
    If proble persist even after adding the correct .htaccess file then make sure your virtual host is configured with the AllowOverride option so that the .htaccess rewrite rules can be used: <Directory /var/www/html> AllowOverride All Order deny,allow Allow from All </Directory>
  • SAMUEL OSPINA
    SAMUEL OSPINA over 8 years
    Hi @cutRateGamer, i use AllowOverride All but server show me error 500.
  • rnpd
    rnpd over 6 years
    Tod-birdsall answer + anj comment = the solution
  • EternalHour
    EternalHour over 6 years
    Thanks, would have been digging forever to discover this solution.