简体   繁体   English

找不到PHP Slim API 404

[英]PHP Slim API 404 not found

I'm trying to create an API using php and php slim. 我正在尝试使用php和php slim创建一个API。

my folder structure: 我的文件夹结构:

API: API:

  • app -> app.js 应用-> app.js
  • libs -> Slim 库->苗条
  • v1 -> .htaccess and index.php v1-> .htaccess和index.php

htaccess htaccess

RewriteEngine On 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^(.*)$ %{ENV:BASE}index.php [QSA,L]

index.php index.php

require '.././libs/Slim/Slim.php';
\Slim\Slim::registerAutoloader();

$app = new \Slim\Slim();
$app = \Slim\Slim::getInstance();

$app->get('/orderoverview/:customerID', function ($customerID) {
        echo "Hello " . $customerID;

});
$app->run();

This should be easy but every time I go to http://localhost/api/v1/orderoverview/2 I get a 404 Page Not Found. 这应该很容易,但是每次我访问http:// localhost / api / v1 / orderoverview / 2时 ,都会得到404页面未找到的信息。

BUT when I go to http://localhost/api/v1/index.php/orderoverview/2 I do get the result I want to become! 但是当我去http://localhost/api/v1/index.php/orderoverview/2时,我确实得到了想要成为的结果!

Any remarks or solutions? 有什么意见或解决方案吗?

You need to add a RewriteBase to your htaccess. 您需要将RewriteBase添加到htaccess中。 I noticed you don't have grouping, so I assume you are placing this in a subdirectory of /api/v1 我注意到您没有分组,因此我假设您将其放在/ api / v1的子目录中

RewriteBase /localsites/serf/weap/api/v1

You have to create the .htaccess file at your root and put below code. 您必须在根目录下创建.htaccess文件,并将其放在下面的代码中。

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /api/v1/

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L,QSA]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
RewriteRule ^ %1 [R=301,L]

I created this one for you,just Change your .httpaccess file to - 我为您创建了此文件,只需将.httpaccess文件更改为-

DirectoryIndex /v1/index.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^(.*)$ %{ENV:BASE}index.php [QSA,L]

Here v1 is the subfolder and index.php is where your functions are. 这里的v1是子文件夹,而index.php是您的函数所在。 Hope it helps! 希望能帮助到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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