我在http://www.example.com/API/
中有SLIM PHP应用程序
我的index.php
中有这样的路由:
$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();
当我用curl http://example.com/API/getstudents
尝试url时,出现错误在此服务器上未找到所请求的URL / API / getstudents。
如果我使用curl http://example.com/API/index.php/getstudents
,则会按预期返回学生名单。我四处搜寻,并了解这可能是由于.htaccess
文件和/或虚拟主机设置。
因此,我已将.htaccess
从API/vendor/slim/slim/.htaccess
复制到API文件夹,因此它与index.php
在同一文件夹中。在这个文件中,我有:
# 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]
没有运气。我是否需要同时更改API目录和API / vendor / slim / slim /中.htaccess
文件的内容?
我也不完全确定我需要对虚拟主机文件执行什么操作。在/ etc / apache2 / sites-available中,我正在编辑example.com.conf,内容为:
<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>
有人可以给我一些指向我在这里做错什么的指示吗?谢谢!
$app->get('/API/getstudents', function() use($app) {
解决了使您的路线像这样的问题:
$app->get('/api/book', function () {
header('Content-Type: application/json; charset=utf-8');
$data = Book::getBookObjects();
echo $data;
});
这是我的一个项目的示例,您可以使用API而不是api。
。htaccess文件:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]
我项目的工作树是这样的:
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();
尝试在API位置创建带有数据的index.php文件的地方创建.htaccess
文件
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ %{ENV:BASE}index.php [QSA,L]
如Slim论坛上的this answer中所述:
如果您的Slim应用不在服务器根路径(http://<host>/[index.php]
)上,则必须说明路径的完整路径。
让我们以这个网址为例:http://<host>/staging/
。
然后您有两种选择来实现我们的目标:
$app->get('/staging/', handler);
等,或$app->SetBasePath('/staging');
声明一次