无法在控制器中加载视图文件 - 出现错误 404

Unable to Load View File in Controller - Getting Error 404

提问人:Saboor Hamedi 提问时间:6/26/2023 更新时间:6/26/2023 访问量:32

问:

我正在开发 PHP MVC 应用程序,我遇到了一个问题,即控制器类无法加载视图文件,从而导致错误 404 页面。我已经检查了文件路径并确保存在必要的文件,但问题仍然存在。

我有一个 Controller 类,其中包含一个负责加载视图文件的视图方法。下面是 view 方法的代码:

<?php

namespace App\core;

class Controller
{
    protected function view($viewName, $data = [])
    {
        $viewPath = APP_PATH . '/app/views/' . $viewName . '.php';
        if (file_exists($viewPath)) {
            ob_start();
            extract($data);
            require($viewPath);
            return ob_get_clean();
        } else {
            throw new \Exception('View not found');
        }
    }
}

我有一个名为“Home”的 PHP 控制器类,它扩展了“Controller”类。“Home”类有一个“index”方法,该方法回显消息并呈现名为“home”的视图。但是,当我运行代码时,它不会加载“主页”视图,而是显示 404 页面。我已经检查了文件路径,它们似乎是正确的。谁能帮我识别问题并解决它?

<?php

namespace App\controllers;

use App\core\Controller;

/**
 * Summary of Home
 */
class Home extends Controller
{
    /**
     * Summary of index
     * @return void
     */
    public function index()
    {
        echo 'Home controller';
        $this->view('home'); // views/home
    }
}

在我的 MainApp 类中,我有一个 loadController 方法,用于加载指定的控制器并调用相应的方法。如果未找到控制器或方法,它将回退到加载Error_404.php视图文件。下面是代码的相关部分:

    <?php

namespace App\core;

/**
 * Summary of MainApp
 */
class MainApp
{

   
    /**
     * Summary of controller
     * @var string
     */
    private $controller = 'Home';
    private $method = 'index';

    /**
     * Summary of splitURL
     * @return array<string>|bool
     */
    private function splitURL()
    {

        $URL = $_SERVER['REQUEST_URI'] ?? 'home';
        // Get the requested URL path
        $requestUri = parse_url($URL, PHP_URL_PATH);
        // Remove leading and trailing slashes
        $requestUri = trim($requestUri, '/');
        // Split the path into segments
        $segments = explode('/', $requestUri);
        // The first segment will be the parameter
        // $url = isset($segments[0]) ? $segments[0] : '';
        // $segments = array_filter($segments);
        return $segments;
    }
    /**
     * Summary of loadController
     * @return bool|string
     */
    public function loadController()
    {
        try {
            $URL = $this->splitURL();
            $controllerNamespace = 'App\controllers\\';
            $controllerName = $controllerNamespace . ucfirst($URL[0]);
            $filename = APP_PATH ."app/controllers/" . $controllerName . '.php';
            if (file_exists($filename)) {
                require_once($filename); // Include the controller file
                
                $this->controller = new $controllerName();
                if (isset($URL[1])) {
                    $this->method = $URL[1];
                }
                if (method_exists($this->controller, $this->method)) {
                    ob_start();
                    $output = call_user_func([$this->controller, $this->method]);
                    return $output;
                } else {
                    throw new \Exception('Method not found');
                }
            } else {
                throw new \Exception('Controller not found');
            }
        } catch (\Exception $e) {
            return $this->load404Page();
        }
    }
    private function load404Page()
    {
        http_response_code(404);
        ob_start();
        $filePath = APP_PATH. 'app/views/Error_404.php';
        require($filePath);
        $this->controller = '_404';
        return ob_get_clean();
    }
}
php model-view-controller 命名空间 custom-error-pages

评论


答: 暂无答案