redirect to 404 page instead of Fatal error: Uncaught ArgumentCountError: Too few arguments to function
redirect to 404 page instead of Fatal error: Uncaught ArgumentCountError: Too few arguments to function
我制作了一个用于训练的 MVC 框架,但出现此错误,我不确定我的函数有什么问题。
当我这样打开 link 时:
http://mvctrav.com/posts/show/6 it show me the post but when deleting the id like this http://mvctrav.com/posts/show/ 它告诉我错误
我的错误是这样的:
Fatal error: Uncaught ArgumentCountError: Too few arguments to function Posts::show(), 0 passed in C:\xampp\htdocs\trav\app\libraries\Core.php on line 51 and exactly 1 expected in C:\xampp\htdocs\trav\app\controllers\Posts.php:124 Stack trace: #0 C:\xampp\htdocs\trav\app\libraries\Core.php(51): Posts->show() #1 C:\xampp\htdocs\trav\public\index.php(4): Core->__construct() #2 {main} thrown in C:\xampp\htdocs\trav\app\controllers\Posts.php on line 124
我的职能是:
public function show($id)
{
$post = $this->postModel->getPostById($id);
$user = $this->userModel->getUserById($post->user_id);
$data = [
'post' => $post,
'user' => $user
];
$this->view('posts/show', $data);
}
这是我的核心class:
<?php
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()
{
$url = isset($_GET['url']) ? $_GET['url'] : "/";
$url = filter_var(trim($url, "/"), FILTER_SANITIZE_URL);
$url = explode('/', $url);
return $url;
}
}
我的问题是: link http://mvctrav.com/posts/show 找不到或重定向到特定页面而不是显示错误
感谢您提供帮助!
1-这个错误意味着你没有将参数传递给你调用它的函数。 post 在你的情况下的 id。你应该在视图页面中解释你的代码
2- 生成 404 页面是使用 http_response_code:
<?php
http_response_code(404);
include('my_404.php'); // provide your own HTML for the error page
die();
die() 不是绝对必要的,但它确保您不会继续正常执行。
对于 2 你可以看到 How can I create an error 404 in PHP?
我认为您需要将 show()
函数编辑成这样:
public function show($id = null)
{
$post = $this->postModel->getPostById($id);
$user = $this->userModel->getUserById($post->user_id);
$data = [
'post' => $post,
'user' => $user
];
if ($user !== false && null !== $id) {
$this->view('posts/show', $data);
} else {
header('Location:'.'404.php');
}
}
我制作了一个用于训练的 MVC 框架,但出现此错误,我不确定我的函数有什么问题。
当我这样打开 link 时: http://mvctrav.com/posts/show/6 it show me the post but when deleting the id like this http://mvctrav.com/posts/show/ 它告诉我错误
我的错误是这样的:
Fatal error: Uncaught ArgumentCountError: Too few arguments to function Posts::show(), 0 passed in C:\xampp\htdocs\trav\app\libraries\Core.php on line 51 and exactly 1 expected in C:\xampp\htdocs\trav\app\controllers\Posts.php:124 Stack trace: #0 C:\xampp\htdocs\trav\app\libraries\Core.php(51): Posts->show() #1 C:\xampp\htdocs\trav\public\index.php(4): Core->__construct() #2 {main} thrown in C:\xampp\htdocs\trav\app\controllers\Posts.php on line 124
我的职能是:
public function show($id)
{
$post = $this->postModel->getPostById($id);
$user = $this->userModel->getUserById($post->user_id);
$data = [
'post' => $post,
'user' => $user
];
$this->view('posts/show', $data);
}
这是我的核心class:
<?php
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()
{
$url = isset($_GET['url']) ? $_GET['url'] : "/";
$url = filter_var(trim($url, "/"), FILTER_SANITIZE_URL);
$url = explode('/', $url);
return $url;
}
}
我的问题是: link http://mvctrav.com/posts/show 找不到或重定向到特定页面而不是显示错误 感谢您提供帮助!
1-这个错误意味着你没有将参数传递给你调用它的函数。 post 在你的情况下的 id。你应该在视图页面中解释你的代码
2- 生成 404 页面是使用 http_response_code:
<?php
http_response_code(404);
include('my_404.php'); // provide your own HTML for the error page
die();
die() 不是绝对必要的,但它确保您不会继续正常执行。
对于 2 你可以看到 How can I create an error 404 in PHP?
我认为您需要将 show()
函数编辑成这样:
public function show($id = null)
{
$post = $this->postModel->getPostById($id);
$user = $this->userModel->getUserById($post->user_id);
$data = [
'post' => $post,
'user' => $user
];
if ($user !== false && null !== $id) {
$this->view('posts/show', $data);
} else {
header('Location:'.'404.php');
}
}