Reputation: 3977
I have my SLIM PHP app in http://www.example.com/API/
I have routing in my index.php
like so:
$app->get('/getstudents', function() use($app) {
$db = new DbHandler();
$result = $db->getAllStudents();
if ($result === false) {
echo "Failed to fetch";
} else {
echo json_encode($result);
}
});
$app->run();
When I try the url with curl http://example.com/API/getstudents
I get the error The requested URL /API/getstudents was not found on this server.
If I use curl http://example.com/API/index.php/getstudents
the list of students is returned as expected. I've googled around and understand that this may be because of the .htaccess
file and/or the virtual host setup.
So I have made a copy of .htaccess
from API/vendor/slim/slim/.htaccess
to the API folder so it is in the same folder as index.php
. In this file I have:
# 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 /
RewriteEngine On
RewriteBase /API/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
No luck with this. Do I need to change the contents of the .htaccess
file in both the API directory and API/vendor/slim/slim/?
I'm also not entirely sure what I need to do to my virtual host file. In /etc/apache2/sites-available I am editing example.com.conf, contents are:
<VirtualHost *:80>
# Admin email, Server Name (domain name), and any aliases
ServerAdmin [email protected]
ServerName www.example.co
ServerAlias example.co
# Index file and Document Root (where the public files are located)
DirectoryIndex index.html index.php
DocumentRoot /var/www/html/example.co/public_html
# Log file locations
LogLevel warn
ErrorLog /var/www/html/example.co/logs/error.log
CustomLog /var/www/html/example.co/logs/access.log combined
<Directory "/var/www/html/example.co/public_html">
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
Could someone please give me some pointers to what I might be doing wrong here? Thanks!
Upvotes: 0
Views: 3022
Reputation: 837
As stated in this answer on the Slim forum :
If your Slim app is not on the server root path (http://<host>/[index.php]
), you'll have to state the full path to your route.
Let's take this url for example : http://<host>/staging/
.
You have then two options to achieve our goal :
$app->get('/staging/', handler);
and so on, or$app->SetBasePath('/staging');
Upvotes: 0
Reputation: 546
$app->get('/API/getstudents', function() use($app) {
Solve this problem making your routes like this:
$app->get('/api/book', function () {
header('Content-Type: application/json; charset=utf-8');
$data = Book::getBookObjects();
echo $data;
});
This is an example of one project of mine, you can user API instead of api.
The .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]
The working tree of my project is like this:
Project Folder -> public -> .htaccess, index.php
Project Folder -> api -> book
$app->get('/api/getstudents', function() use($app) {
$db = new DbHandler();
$result = $db->getAllStudents();
if ($result === false) {
echo "Failed to fetch";
} else {
echo json_encode($result);
}
});
$app->run();
Upvotes: 1
Reputation: 661
Try to create .htaccess
file in API place where is your index.php file with data
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ %{ENV:BASE}index.php [QSA,L]
Upvotes: 0