简体   繁体   中英

404 Error when using SLIM framework routing

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 webmaster@example.co
  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!

First thing to do:

Change your route to this:

$app->get('/API/getstudents', function() use($app) {

If that does not work do the following:

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

Adapting your code:

$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();

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]

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 :

  • States the path in all your routes $app->get('/staging/', handler); and so on, or
  • States it a single time after the app creation $app->SetBasePath('/staging');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM