简体   繁体   English

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

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

For one of the projects, i am using Slim Framework http://www.slimframework.com/ to create restful APIs in PHP. 对于其中一个项目,我使用Slim Framework http://www.slimframework.com/在PHP中创建restful API。

I did a manual install for the framework by copying it in the PHP project folder using instructions at https://github.com/slimphp/Slim . 我通过使用https://github.com/slimphp/Slim上的说明将它复制到PHP项目文件夹中,对框架进行了手动安装。

Later i updated my .htaccess as well. 后来我也更新了我的.htaccess。

For my project, I have the following Directory Structure 对于我的项目,我有以下目录结构

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

For this, the Get call ie http://someIp/project/ works for me. 为此,Get call即http:// someIp / project /适合我。 It fetches the standard "Welcome to Slim! Congratulations! Your Slim application is running. If this is your first time using Slim, start with this "Hello World" Tutorial." 它取得了标准的“欢迎来到Slim!祝贺!你的Slim应用程序正在运行。如果这是你第一次使用Slim,请从这个”Hello World“教程开始。” However, post/patch/delete and other get are not working. 但是,post / patch / delete和其他get不起作用。 Not even get for hello. 甚至没有打招呼。 It gives the not found error. 它给出了未找到的错误。

http://someIp/project/hello/:name The requested URL /project/hello/:name was not found on this server. http:// someIp / project / hello /:name在此服务器上找不到请求的URL / project / hello /:name。

http://someIp/project/post The requested URL /project/post was not found on this server. http:// someIp / project / post在此服务器上找不到请求的URL /项目/帖子。

Updated my .htaccess file as : 将我的.htaccess文件更新为:

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

Still it is failing. 它还是失败了。

When i made changes into the apache config file to allowOverride = all, it failed even for the GET call on index.php. 当我将apache配置文件更改为allowOverride = all时,即使对index.php进行GET调用也失败了。 For sure, it is not mapping from .htaccess. 当然,它不是从.htaccess映射的。

I am still clueless what changes i need to make to .htaccess or any other file to get it working. 我仍然无知我需要对.htaccess或任何其他文件进行哪些更改才能使其正常工作。

Here is the code: 这是代码:

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

The following steps worked for me : 以下步骤对我有用:

Changes in apache2.conf 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

Changes in .htaccess .htaccess的变化

RewriteEngine on
RewriteBase /

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

Changes in .htaccess inside subdirectory 子目录内的.htaccess更改

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

Enable mod_rewrite for Apache 2.2 为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