【问题标题】:Getting 404 Page Not Found when accessing my index.php file访问我的 index.php 文件时找不到 404 页面
【发布时间】:2019-05-24 23:19:26
【问题描述】:

我一直在开发一个 Android 应用程序,现在我想在服务器上建立一个数据库,我的应用程序将从该数据库中获取数据。

我发现用 PHP 制作一个 REST API 来访问 MySQL 数据库是最简单的解决方案,但对 php 完全陌生(第一次接触它)。

当我尝试点击时收到“404 Page not found” -- http://localhost/oneview_alerts/v1/notifications

我的 WAMP 服务器已启动并正在运行。

我的代码非常小而且简单,我参考了下面的 URL 来寻求帮助: 1)https://www.androidhive.info/2014/01/how-to-create-rest-api-for-android-app-using-php-slim-and-mysql-day-12-2/

2)https://www.androidhive.info/2014/01/how-to-create-rest-api-for-android-app-using-php-slim-and-mysql-day-23/

我对 PHP 完全陌生,不知道从哪里开始调试。它不像我的 Java 或 Python,我可以直接在 logcat 或调试器中看到错误日志。或者可能有办法,我只是不知道。

请帮忙!!

在我的 access.log 文件中 "127.0.0.1 - - [26/Dec/2018:19:15:59 +0000] "GET /oneview_alerts/v1/notifications HTTP/1.1" 404 532"

index.php

<?php
/**
 * Created by PhpStorm.
 * User: JM00490784
 * Date: 12/26/2018
 * Time: 6:05 PM
 */

ini_set('display_errors', 'On');
error_reporting(E_ALL);

require_once '../include/DbHandler.php';
require '.././libs/Slim/Slim.php';

\Slim\Slim::registerAutoloader();

$app = new \Slim\Slim();

function echoResponse($status_code, $response) {
    $app = \Slim\Slim::getInstance();
    // Http response code
    $app->status($status_code);

    // setting response content type to json
    $app->contentType('application/json');

    echo json_encode($response);
}

$app->run();

$app->get('/notifications', function() {

    $response = array();
    $db = new DbHandler();

    // fetching all user tasks
    $result = $db->getAllNotifications();

    $response["error"] = false;
    $response["notifications"] = array();

    // looping through result and preparing tasks array
    while ($notification = $result->fetch_assoc()) {
        $tmp = array();
        $tmp["id"] = $notification["id"];
        $tmp["notification_text"] = $notification["notification_text"];
        $tmp["hostname"] = $notification["hostname"];
        $tmp["service"] = $notification["service"];
        $tmp["value"] = $notification["value"];
        $tmp["timestamp"] = $notification["timestamp"];
        array_push($response["notifications"], $tmp);
    }

    echoResponse(200, $response);
});

?>

DBHandler.php

<?php

/**
 * Class to handle db operations
 * This class will have CRUD methods for database tables
 *
 * @author Jotinder Singh Matta
 */
class DbHandler {

    private $conn;

    function __construct() {
        require_once dirname(__FILE__) . './DbConnect.php';
        // opening db connection
        $db = new DbConnect();
        $this->conn = $db->connect();
    }

    public function getAllNotifications() {
        $stmt = $this->conn->prepare("SELECT * FROM notification ORDER BY timestamp DESC");
        $stmt->execute();
        $notifications = $stmt->get_result();
        $stmt->close();
        return $notifications;
    }


}

?>

DBConnect.php

<?php

/**
 * Handling database connection
 *
 * @author Jotinder Singh Matta
 */
class DbConnect {

    private $conn;

    function __construct() {
    }

    /**
     * Establishing database connection
     * @return database connection handler
     */
    function connect() {
        include_once dirname(__FILE__) . './Config.php';

        // Connecting to mysql database
        $this->conn = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_NAME);

        // Check for database connection error
        if (mysqli_connect_errno()) {
            echo "Failed to connect to MySQL: " . mysqli_connect_error();
        }

        // returing connection resource
        return $this->conn;
    }

}

?>

Config.php

<?php
/**
 * Database configuration
 */
define('DB_USERNAME', 'root');
define('DB_PASSWORD', '');
define('DB_HOST', 'localhost');
define('DB_NAME', 'oneview_alerts');

?>

.htaccess

RewriteEngine On 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^(.*)$ %{ENV:BASE}index.php [QSA,L] 

描述通知表

desc notification



id  int(11) NO  PRI 
    NULL
    auto_increment  
notification_text   varchar(250)    YES     
    NULL

hostname    varchar(255)    NO      
    NULL

service varchar(255)    NO      
    NULL

value   varchar(255)    NO      
    NULL

timestamp   timestamp   YES     CURRENT_TIMESTAMP       

【问题讨论】:

  • 添加您的项目文件夹路径。
  • 我需要在哪里添加它以及以什么格式。
  • 我的意思是,在这里将路径名发布到您的项目文件夹。
  • 项目路径 - E:\wamp64\www\oneview_alerts 下面是这个父目录下的目录结构:- E:\wamp64\www\oneview_alerts\include E:\wamp64\www\oneview_alerts\include \Config.php E:\wamp64\www\oneview_alerts\include\DbConnect.php E:\wamp64\www\oneview_alerts\include\DbHandler.php E:\wamp64\www\oneview_alerts\libs E:\wamp64\www\oneview_alerts \libs\Slim E:\wamp64\www\oneview_alerts\v1 E:\wamp64\www\oneview_alerts\v1\.htaccess E:\wamp64\www\oneview_alerts\v1\index.php
  • 我假设您使用的是 windows 要获取 windows 中的文件夹路径,请转到包含项目的 index.php 的文件夹。按住键盘上的 Shift 键,然后右键单击文件。然后,从菜单中选择“复制为路径”。

标签: php .htaccess http-status-code-404 wamp


【解决方案1】:

根据您的 cmets,我假设您的 Document 根目录是 E:\wamp64\www\oneview_alerts\v1\ 并且有一个与 index.php 处于同一级别的.htaccess 文件

用这个替换.htaccess文件的内容。

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

然后创建一个vhost文件

<VirtualHost *:80>
DocumentRoot "E:/wamp64/www/oneview_alerts/v1/"
ServerName oneview_alerts.local
<Directory "E:/wamp64/www/oneview_alerts/v1/">
</Directory>
</VirtualHost>

将以下内容添加到您的主机文件中

127.0.0.1 localhost oneview_alerts.local

然后重启WAMP服务器

【讨论】:

  • 仍然得到相同的“404 页面无法显示错误,即使在如上所述更新 .htaccess 文件之后。
  • 尝试创建一个虚拟主机文件。 cloudways.com/blog/…
  • 要在 wamp 上创建虚拟主机,请点击此链接john-dugan.com/wamp-vhost-setup
  • 我做了上述更改。现在我在点击“oneview.localhost”时看到了我的目录结构。但是仍然在点击“oneview.localhost/v1/notifications”时,我得到 404 Page not found。
  • 结果如何??重启wamp服务器,将域名添加到你的host文件中,然后在浏览器中输入YOURNEWDOMAIN
猜你喜欢
  • 1970-01-01
  • 2022-12-14
  • 2012-01-29
  • 1970-01-01
  • 2019-09-29
  • 2021-09-28
  • 2020-12-09
  • 2020-08-03
  • 1970-01-01
相关资源
最近更新 更多