PHP Slim Framework:在此服务器上找不到请求的 URL

PHP Slim Framework: The requested URL was not found on this server

对于其中一个项目,我正在使用 Slim Framework http://www.slimframework.com/ 在 PHP 中创建 restful API。

我通过使用 https://github.com/slimphp/Slim 中的说明将其复制到 PHP 项目文件夹中,对框架进行了手动安装。

后来我也更新了我的.htaccess。

对于我的项目,我有以下目录结构

project\
----slim\
----tests\
----index.php
----.htaccess

为此,Get 调用即 http://someIp/project/ 对我有用。它获取标准 "Welcome to Slim! Congratulations! Your Slim application is running. If this is your first time using Slim, start with this "Hello World“教程”。 但是,post/patch/delete 和其他 get 不起作用。连打个招呼都不行。它给出了未找到的错误。

http://someIp/project/hello/:name 请求的 URL /project/hello/: 在此服务器上找不到名称。

http://someIp/project/post 在此服务器上未找到请求的 URL /project/post。

将我的 .htaccess 文件更新为:

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

仍然失败。

当我将 apache 配置文件更改为 allowOverride = all 时,它甚至对 index.php 上的 GET 调用也失败了。当然,它不是来自 .htaccess 的映射。

我仍然不知道我需要对 .htaccess 或任何其他文件进行哪些更改才能使其正常工作。

代码如下:

\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();

以下步骤对我有用:

apache2.conf

的变化
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

.htaccess 的变化

RewriteEngine on
RewriteBase /

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

子目录中 .htaccess 的变化

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

为 Apache 2.2 启用 mod_rewrite

1. Type the following command in the terminal
    a2enmod rewrite
2. Restart apache2 after
    service apache2 restart