簡體   English   中英

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 call即http:// someIp / project /適合我。 它取得了標准的“歡迎來到Slim!祝賀!你的Slim應用程序正在運行。如果這是你第一次使用Slim,請從這個”Hello World“教程開始。” 但是,post / patch / delete和其他get不起作用。 甚至沒有打招呼。 它給出了未找到的錯誤。

http:// someIp / project / hello /:name在此服務器上找不到請求的URL / project / hello /:name。

http:// someIp / project / post在此服務器上找不到請求的URL /項目/帖子。

將我的.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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM