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]
- httpd.conf: (Shared Link).
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)
If you're using Slim on Ubuntu 16.04 and you're getting the 404 error. Try this test from Tod Birdsall above:
If this works
http://localhost/index.php/hello/bob
and this doesnt work
http://localhost/hello/bob
and your .htaccess file in the same directory as your index.php and is configured as follows:
And you still get the 404 error on Apache.
Try turning on Apache mod_rewrite as follows and restart Apache:
$sudo a2enmod rewrite
$sudo service apache2 restart
The Slim API should work correctly now as the provided .htaccess file will only work if mod_rewrite is enabled and that not the default on a clean install.
Here is how to turn off mod_rewirte if you need to.
$sudo a2dismod rewrite
$sudo service apache2 restart
For me the problem was that I'd forgotten to provide a
.htaccess
file in my document root..htaccess
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:
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:Now I can access the page using
http://localhost/hello/bob
.For people who still search answers because previous doesn't work, this works for me :
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;
and now that works!
I just realized it once I found these statements;
I think your problem is at the "Resource parser" because you are no defining $id parameter at the request so, please, try this:
Please, tell us if it's ok