PHP Slim Framework:在此服务器上找不到请求的URL
对于其中一个项目,我正在使用Slim框架在PHP中创建restful API 我通过在PHP项目文件夹中复制框架,并使用上的说明进行了手动安装 后来我也更新了我的.htaccess 对于我的项目,我有以下目录结构PHP Slim Framework:在此服务器上找不到请求的URL,php,.htaccess,api,rest,slim,Php,.htaccess,Api,Rest,Slim,对于其中一个项目,我正在使用Slim框架在PHP中创建restful API 我通过在PHP项目文件夹中复制框架,并使用上的说明进行了手动安装 后来我也更新了我的.htaccess 对于我的项目,我有以下目录结构 project\ ----slim\ ----tests\ ----index.php ----.htaccess 对于这一点,Get call即适用于我。它获取标准的“欢迎使用Slim!恭喜!您的Slim应用程序正在运行。如果这是您第一次使用Slim,请从本“Hello World
project\
----slim\
----tests\
----index.php
----.htaccess
对于这一点,Get call即适用于我。它获取标准的“欢迎使用Slim!恭喜!您的Slim应用程序正在运行。如果这是您第一次使用Slim,请从本“Hello World”教程开始。”
但是,post/patch/delete和其他get不起作用。连打招呼的机会都没有。它给出了未找到的错误
在此服务器上找不到请求的URL/project/hello/:名称
在此服务器上找不到请求的URL/project/post
已将my.htaccess文件更新为:
RewriteEngine On
RewriteBase /project/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
但它仍在失败
当我将apache配置文件更改为allowOverride=all时,甚至对index.php的GET调用都失败了。当然,它不是从.htaccess映射的
我仍然不知道需要对.htaccess或任何其他文件进行哪些更改才能使其正常工作
代码如下:
以下步骤对我有效: apache2.conf中的更改 子目录内.htaccess中的更改 为Apache 2.2启用mod_重写
你真的在index.php中定义了路由吗?我在index.php中有默认路由,它们似乎不起作用。如果你在index.php的末尾添加$app->run()?这是我经常犯的错误:)@Tuim它已经在那里了。。我正在使用他们为Helloth提供的现有代码。自述文件中的hello world示例只定义了一个GET路由。它不定义任何POST或PUT路由。请在你的问题中包含你正在使用的代码,因为我们可以理解你正在使用的代码。完美答案。。。浪费了我宝贵的6个小时。。谢谢你帮助太多了。完美答案!!对我来说,apache conf中的更改是可行的,默认域工作正常,但新虚拟主机中的一个域工作不正常。这就解决了问题。
\Slim\Slim::registerAutoloader();
/**
* Step 2: Instantiate a Slim application
*
* This example instantiates a Slim application using
* its default settings. However, you will usually configure
* your Slim application now by passing an associative array
* of setting names and values into the application constructor.
*/
$app = new \Slim\Slim();
/**
* Step 3: Define the Slim application routes
*
* Here we define several Slim application routes that respond
* to appropriate HTTP request methods. In this example, the second
* argument for `Slim::get`, `Slim::post`, `Slim::put`, `Slim::patch`, and `Slim::delete`
* is an anonymous function.
*/
// GET route
$app->get(
'/',
function () {
$template = "hi";
echo $template;
}
);
//$app->get(
// '/v1/status/',
// function() {
// echo "status";
// }
//);
//
$app->get('/hello/:name', function ($name) {
echo "Hello, $name";
});
// POST route
$app->post(
'/post',
function () {
echo 'This is a POST route';
}
);
// PUT route
$app->put(
'/put',
function () {
echo 'This is a PUT route';
}
);
// PATCH route
$app->patch('/patch', function () {
echo 'This is a PATCH route';
});
// DELETE route
$app->delete(
'/delete',
function () {
echo 'This is a DELETE route';
}
);
/**
* Step 4: Run the Slim application
*
* This method should be called last. This executes the Slim application
* and returns the HTTP response to the HTTP client.
*/
$app->run();
1. Get the path of running Apache
ps -ef | grep apache
Append -V argument to the path
/usr/sbin/apache2 -V | grep SERVER_CONFIG_FILE
2. Naviagte to apache2.conf
vi /etc/apache2/apache2.conf
3. Update the file Replace "AllowOverride None" to "AllowOverride All"
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
4. Restart apache2 after
service apache2 restart
OR
apachectl -k graceful
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
RewriteEngine On
RewriteBase /project
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
1. Type the following command in the terminal
a2enmod rewrite
2. Restart apache2 after
service apache2 restart