Error 404 with slim framework - php

I am learning to code in PHP and mysql and need to be able to run PHP files using a wampserver. I have correctly installed wamp and have it online through Windows 8, however when I go to run my PHP file I am always given Error 404. I have ensured that my Slim file and .php file are in the correct directory and I have also modified the .htaccess accordingly so that it reroutes to the proper file if a URL is not found since I am testing this on my local machine. Here is a some of my sample code:
require 'C:\wamp\www\easysites\codeguy-Slim-b8181de\Slim\Slim.php';
\Slim\Slim::registerAutoloader();
$api = new \Slim\Slim();
$api->run();
I have traced the error to $api->run() and was getting an error from the call stack at one point that indicated an error had happened in Slim's environment.php at line 123 where the URL is first dealt with. If I run the basic index.php file generated by Netbeans, I get the usual text that alerts me that it is working, however try to run the php file using Slim it errors out which are in the same sub directory under root. Slim is correctly loaded by the php file, but will not allow me to run anything else thus preventing me from seeing the database that I have imported.
Does anyone have an idea of what could be going wrong? I have a co-worker who has successfully run the file but we are unable to make it run on my machine using the same types of changes.
The .htaccess file is as follows:
RewriteEngine ON
RewriteRule ^API(.*)$ /API/api.php [QSA, L]
The file I am trying to run is in a folder contained in the root directory named API, and the file I am trying to run is api.php.

When I followed all the instructions of the installation of slim exactly (installing it on easyphp on my local windows machine), I got the error 404.
I changed the .htaccess to the following, and then it worked fine:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ %{ENV:BASE}index.php [QSA,L]

Related

.htaccess working on XAMPP localhost but not on Webhosting

Hei guys,
for my webpage I wanted to implement some simple REST-Services to serve some Json.
To achieve that I created subfolder api and in this folder I created index.php and .htaccess file.
index.php uses AltoRouter to handle Routes called on /api.
.htaccess locks like following:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]
mod_rewrite is enable on my webhoster. (I was abelt to test this using some simple examples served by the webhoster)
I tested everthing on my local computer using localhost xampp.
Everthing is working fine. (GET and POST Requests)
After uploading this to my webpage I figured out, that POST Requests are Redirected to GET and therefore are not working.
Can anybody help with that issue. I need this POST Requests to be redirected to my index.php and be handled there to make some data update on my webpage.
Regards
Manuel
There is an update:
My Webhoster seams to use a nginx proxy that redirects to Apache.
POST seams to get coverted to GET and therefore my mechanism is not working anymore.
Can anyone tell me how to force nginx to redirect POST as POST?
Manuel
You can include the PHP file in your /api folder in your PHP file in your Root folder.
Just add the line include "./api/index.php" to your PHP in the Root folder.
Then the functions work in the PHP file in the /api folder, without having to open that specific PHP file in the /api folder.
https://www.php.net/manual/en/function.include.php

Using Codeigniter application on freeBSD system

I have freeBSD installed on IBM server. There is also apache, php and mysql installed on the server. A PHP application running on this machine is working just fine, but our new version of the same application developed in Codeigniter gives a "Page Not Found" error.
The thing is the application is accessible and runs well (only the non-english character are displayed not well) if the index.php is included in the url.
I have modified the file .htaccess to remove the index.php from the url, I kind of need to use the .htaccess in the application's root directory. This (using .htaccess) is actually causing the error.
Please let me know what configuration do I need to do.
Edit:
Bellow is the content of .htaccess in the root directory of the application:
RewriteEngine on
RewriteCond $1 !^(index\.php|assets|robots\.txt)
RewriteRule ^(.*)$ /football/index.php/$1 [L]

simple php Slim project errors

I've created a simple Slim microframework project using PHPStorm 8 on Windows 8.1 with WAMP Server installed. All WAMP Server settings are set by default.
I created a new project called pr1
I used 'Init Composer...' and then added some dependencies like slim/slim, slim/views, twig/twig.
Then I tried to create a simple application just like a given example on main Slim page:
I've created file called index.php in my project folder
index.php
code:
require 'app.php';
Then I've created file app.php
code
require 'vendor/autoload.php';
$app = new \Slim\Slim();
$app->get('/:name', function ($name) {
echo "Hello, $name";
});
$app->run();
After this I tried to run my project in Chrome and there an error 404 occured.
Then I tried to pass my name through url: http://localhost:63342/pr1/wade and there was PHPStorm error.
After this steps I've tried to close PHPStorm and my project in browser:
and it seemed like there's a typical Slim 404 error,but when I tried to pass my name through url again it gave me this error:
you need to include index.php for example http://localhost/pr1/index.php/test
to get rid of index.php use .htaccess or something equal based on your webserver
All it was because Apachi doesn't know what to do with all browser requests. For this reason I've created a simple .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]
After this I needed to use rewrite_module in my WAMP settings.
It perfectly works with project when I open it in browser through the explorer.
And after this I set up my server like this:
And that allows me to preview all my changes through PHPStorm

404 error with Slim - tried everything

I'm trying to build an API for the first time by using PHP to access a MySQL server, and going off of AndroidHive's tutorial here but customizing it to be my own simple API instead of the task manager one listed. My problem is, I can't even get to the part where I test anything (I'm using XAMPP for Mac) because I keep getting 404 errors when I attempt to access a method in my index.php file.
Here's my .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ %{ENV:BASE}index.php [QSA,L]
I already changed my XAMPP Apache httpd.conf file to "AllowOverride All" as per the Slim documentation instructions, and tried restarting the server a billion times but to no avail. No matter what I do, if I ever try to test any of the URLs in my index file (for instance at localhost/w_manager/v1/register) it just returns a 404 not found error.
I even updated my .htaccess file to add in "RewriteBase /v1" though that did not seem to make much of a difference - got another 404 object not found error.
I've also looked around on Stackoverflow a ton. I'm about to pull out my hair. Does anyone have any other suggestions? Thanks!
Well, I seem to have fixed it - not exactly sure how, unfortunately, but what I did was go back and copy the sample code into my own and make alterations (after I noticed that the sample code worked.) One major change I made was that in my index.php file, I moved the Slim request definitions at the beginning and moved all other functions to the end, just before $app->run(). Hope this will help someone else!

Restler always showing NOT FOUND on server, but not on local machine

When I go to
http://localhost/api/auth/login
I get what I expect.. some data that lets me log in.
However, when I go to
http://myurl.com/api/auth/login
I just get
{
"error": {
"code": 404,
"message": "Not Found"
}
}
Currently we have 2 people working on this project, and both of us, when we run it from our local machines, have no issues. We're to the point where we are putting the app on to a hosted server so that we can start testing it outside of our own machines.
The code is identical, and I know this because we use a git repository in which the server is also pulling from.
My machine is a mac, my buddy's is a windows machine, and our host is a linux box. This shouldn't really matter, since all of them should work with mod_rewrite, a requirement for Restler 3.
The only other details I can think of is that the server is hosted by HostMonster, running PHP 5.4.7
Any help would be GREATLY appreciated. Do you need more information?
=== Edit:
This is my .htaccess file. Also, the server runs CGI/FastCGI not mod_php
Options -MultiViews
DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^$ index.php [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
<IfModule mod_php5.c>
php_flag display_errors Off
</IfModule>
I just googled 'fastcgi mod_rewrite' and none of those links helped :P
When ever you come across such issue first try without url rewriting
instead of
http://myurl.com/api/auth/login
try
http://myurl.com/api/index.php/auth/login
If it works you will know that the issue is with url rewriting
Since you are using CGI/FastCGI make sure you have set
cgi.fix_pathinfo=0
in your php.ini it will make sure that path info is passed to Restler properly so that restler can find the route
If it does not work with index.php in the url, you can try generating and verifying routes.php as explained below
routes.php
Restler looks at all api methods and its doc comments to generate routes accordingly every time we run it on debug mode. When we run it on production mode it captures the information in routes.php and uses it instead of generating routes every time thus improving efficiency.
You can check for the generated routes by initializing restler in production mode and refresh on every call
$r = new Restler(true, true);
and then check the generated routes.php
If your routes.php contains no routes it will appear as follows
<?php $o = array();
// ** THIS IS AN AUTO GENERATED FILE. DO NOT EDIT MANUALLY **
return $o;
If this is your case, it means some how autoloader is failing to load the api class, you can confirm it by manually including the API classes
If it starts to work, please file a bug using github issues stating that the autoloader is failing for your server configuration and give us more detils so that we can reproduce it and fix it.
If it still does not work, please file a bug using github issues mentioning your server configuration and give us any detail that could help so that we can reproduce it and fix it.
Thaks to you, we will also write a trouble shooting guide based on above to help the restler community :)

Categories