php - 致命错误:require():需要打开失败
问题描述
我正在尝试使用 PHP 创建一个 MVC 路由器以用于学习目的。
但是,我无法加载我想要访问的视图。例如,我将尝试访问 homestead.blog/about,这将被返回:
Fatal error: require(): Failed opening required 'controllers/about.php' (include_path='.:/usr/share/php') in /home/vagrant/projects/blog/public/index.php on line 8
这是我的 public/index.php 的第 8 行:
require Router::load('../routes.php')
->direct(Request::uri());
这是路由文件:
<?php
$router->define([
'' => 'public/index.php',
'about' => 'controllers/about.php',
'blog' => 'controllers/blog.php',
'contact' => 'controllers/contact.php'
]);
这些路线绝对是准确的。“about”端点绝对属于“controllers/about.php”。
这是路由器对象:
<?php
class Router
{
protected $routes = [];
public static function load($file)
{
$router = new static;
require $file;
return $router;
}
public function define($routes)
{
$this->routes = $routes;
}
public function direct($uri)
{
if (array_key_exists($uri, $this->routes)) {
return $this->routes[$uri];
}
throw new Exception('No route defined for this URI.');
}
}
这是 Request 对象:
<?php
class Request
{
public static function uri()
{
return trim($_SERVER['REQUEST_URI'], '/');
}
}
public/index.php 中需要这两个对象。
为什么我会收到此错误?
解决方案
错误说明/home/vagrant/projects/blog/public/controllers/about.php
不存在。
您要么必须在路由文件(或require
文件所述的位置)中指定正确的关系路径或绝对路径。
推荐阅读
- android - React Native null 不是对象(评估'RNGestureHandlerModule.Direction')
- python - 如何将字符串转换为 Unicode 字符
- reactjs - 无法放置
在可重用函数中 - assembly - 有条件地将零移入寄存器?
- r - 哪些选项可用于访问 AWS CodeCommit 上的 R 包?
- javascript - 由于时区不匹配,GitLab 管道中的 Jest 快照测试失败
- javascript - 为什么调试不像 JavaScript 那样容易或内置于 C++ 的 VSCode 中?
- spring - 在 API 响应中返回 java obect 时 Spring 添加额外的键
- python - 每次另一个函数结束时,Python都会转到输入函数
- javascript - Angular 需要 Polyfill-es5 吗?