php - 注意:尝试访问第 15 行 H:\xampp\htdocs\ecommerce\app\libraries\Core.php 中 null 类型值的数组偏移量
问题描述
<?php
/*
* App Core Class
* Creates URL & loads core controller
* URL FORMAT - /controller/method/params
*/
class Core {
protected $currentController = 'Pages';
protected $currentMethod = 'index';
protected $params = [];
public function __construct(){
//print_r($this->getUrl());
$url = $this->getUrl();
// Look in controllers for first value
if(file_exists('../app/controllers/' . ucwords($url[0]). '.php')){
// If exists, set as controller
$this->currentController = ucwords($url[0]);
// Unset 0 Index
unset($url[0]);
}
// Require the controller
require_once '../app/controllers/'. $this->currentController . '.php';
// Instantiate controller class
$this->currentController = new $this->currentController;
// Check for second part of url
if(isset($url[1])){
// Check to see if method exists in controller
if(method_exists($this->currentController, $url[1])){
$this->currentMethod = $url[1];
// Unset 1 index
unset($url[1]);
}
}
// Get params
$this->params = $url ? array_values($url) : [];
// Call a callback with array of params
call_user_func_array([$this->currentController, $this->currentMethod], $this->params);
}
public function getUrl(){
if(isset($_GET['url'])){
$url = rtrim($_GET['url'], '/');
$url = filter_var($url, FILTER_SANITIZE_URL);
$url = explode('/', $url);
return $url;
}
}
}
上面的代码包含类似这样的错误提示: Trying to access array offset on value of type of null in H:\xampp\htdocs\ecommerce\app\libraries\Core.php 在第 18 行
需要一次'../app/controllers/'。$this->currentController 。'.php';
这是行。当我使用 domain.com 进入网站时显示此错误,当我输入 domian.com/post 时它没有问题
解决方案
我也遇到了这个问题。
if(file_exists('../app/controllers/' . ucwords($url[0]). '.php')){
// If exists, set as controller
$this->currentController = ucwords($url[0]);
// Unset 0 Index
unset($url[0]);
}`
在这个地方,也许你必须先添加!empty($url[0])&&
,file_exists
这肯定会集中你的问题
推荐阅读
- java - 为什么 REST 控制器返回 404 状态码?
- c# - C# Tcp 异步编程
- excel - 在 Laravel 中将 excel 转换为数组
- php - 从谷歌地图解析 KML 坐标 - PHP
- ruby-on-rails - 如何在 Rails 5 中查询“有很多通过”关系
- couchdb - 如何保证 Hyperledger Fabric 网络中 couchDB 的安全性?
- css - 如何在元素中包含填充以使高度和宽度相同?
- c - 这个算法更准确的时间复杂度是多少?
- java - 如何制作一个匹配字符输入和方法的正则表达式并调用它?
- javascript - 为什么 CSS 属性 'stroke' 会这样命名?