Slim Framework always return 404 Error

后端 未结 8 405
独厮守ぢ
独厮守ぢ 2021-02-01 04:27

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

  • Coenraets
  • CodingThis
相关标签:
8条回答
  • 2021-02-01 04:52

    Additionally mod_rewrite.so module must be loaded in httpd.conf or you will receive error 500.

    LoadModule rewrite_module modules/mod_rewrite.so
    
    0 讨论(0)
  • 2021-02-01 04:55

    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:

        <IfModule mod_rewrite.c>
           RewriteEngine On
           RewriteCond %{REQUEST_FILENAME} !-d
           RewriteCond %{REQUEST_FILENAME} !-f
           RewriteRule ^ index.php [QSA,L]
        </IfModule>
    

    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

    0 讨论(0)
  • 2021-02-01 04:59

    For people who still search answers because previous doesn't work, this works for me :

    RewriteBase /<base_of_your_project>/
    
    0 讨论(0)
  • 2021-02-01 05:02

    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]
    
    0 讨论(0)
  • 2021-02-01 05:05

    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.

    0 讨论(0)
  • 2021-02-01 05:09

    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

    0 讨论(0)
提交回复
热议问题