Slim Framework always return 404 Error

后端 未结 8 406
独厮守ぢ
独厮守ぢ 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 05:10

    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:

    <?php
    $app = new \Slim\Slim();
    $app->get('/hello/:name', function ($name) {
        echo "Hello, $name";
    });
    $app->run();
    

    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:

    RewriteEngine On
    
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [QSA,L] 
    

    Now I can access the page using http://localhost/hello/bob.

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

    You're right about to include the index.php on your file, but that happen because you're not working properly with your mod-rewrite Please check the following link:

    https://github.com/codeguy/Slim

    In the Get Started part you could see how to configure your server (in case that you were using apache / lighttpd / ngynx)

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