【问题标题】:Fatal error: Uncaught TypeError: call_user_func(): Argument #1 ($callback) must be a valid callback致命错误:未捕获的 TypeError:call_user_func():参数 #1 ($callback) 必须是有效的回调
【发布时间】:2022-01-22 07:40:17
【问题描述】:

我尝试使用php mvc,我该如何解决这个错误?

致命错误:未捕获的类型错误:call_user_func():参数 #1 ($callback) 必须是有效回调,数组在 C:\xampp\htdocs\03_good\Router.php:53 中必须恰好有两个成员 堆栈跟踪:
#0 C:\xampp\htdocs\03_good\Router.php(53): call_user_func(Array, Object(app\Router))
#1 C:\xampp\htdocs\03_good\public\index.php(20): app\Router->resolve()
#2 {main} 在第 53 行的 C:\xampp\htdocs\03_good\Router.php 中抛出

这是我的Router

<?php
/**
 * User: TheCodeholic
 * Date: 10/11/2020
 * Time: 10:05 AM
 */

namespace app;


/**
 * Class Router
 *
 * @author  Zura Sekhniashvili <zurasekhniashvili@gmail.com>
 * @package app
 */
class Router
{
    public array $getRoutes = [];
    public array $postRoutes = [];

    public ?Database $database = null;

    public function __construct(Database $database)
    {
        $this->database = $database;
    }

    public function get($url, $fn)
    {
        $this->getRoutes[$url] = $fn;
    }

    public function post($url, $fn)
    {
        $this->postRoutes[$url] = $fn;
    }

    public function resolve()
    {
        $method = strtolower($_SERVER['REQUEST_METHOD']);
        $url = $_SERVER['PATH_INFO'] ?? '/';

        if ($method === 'get') {
            $fn = $this->getRoutes[$url] ?? null;
        } else {
            $fn = $this->postRoutes[$url] ?? null;
        }
        if (!$fn) {
            echo 'Page not found';
            exit;
        }
        echo call_user_func([$fn], $this);
    }

    public function renderView($view, $params = [])
    {
        foreach ($params as $key => $value) {
            $$key = $value;
        }
        ob_start();
        include __DIR__."/views/$view.php";
        $content = ob_get_clean();
        include __DIR__."/views/_layout.php";
    }
}

这是index.php:

<?php

use app\controllers\ProductController;
use app\Router;

require_once __DIR__.'/../vendor/autoload.php';

$database = new \app\Database();
$router = new Router($database);

$router->get('/', [ProductController::class, 'index']);
$router->get('/products', [ProductController::class, 'index']);
$router->get('/products/index', [ProductController::class, 'index']);
$router->get('/products/create', [ProductController::class, 'create']);
$router->post('/products/create', [ProductController::class, 'create']);
$router->get('/products/update', [ProductController::class, 'update']);
$router->post('/products/update', [ProductController::class, 'update']);
$router->post('/products/delete', [ProductController::class, 'delete']);

$router->resolve();

【问题讨论】:

  • 您需要检查$fn 以确保它是可调用的。您正在手动将该变量包装在一个数组中,这需要吗?已经是数组了吗?
  • array(2) { [0]=&gt; string(33) "app\controllers\ProductController" [1]=&gt; string(5) "index" }这是$fn的值
  • 看看那个,我想你可以改成echo call_user_func($fn, $this);,去掉它周围的方括号。
  • 我去掉了那个括号但是这个错误还没有修复
  • 请分享更多细节。去掉括号后的错误信息真的完全一样吗?另外,那个 ProductController 类包含什么?

标签: php model-view-controller


【解决方案1】:

根据您的回复,您传递给数组的函数似乎不是正确的可调用对象。

假设自动加载不是问题,因为错误消息的最后一部分将是未找到类“xx”。

我建议更改您的路由器并添加 callabale 类型提示,您可能会更早看到错误。

public function get($url, callable  $fn)
{
    $this->getRoutes[$url] = $fn;
}

public function post($url, callable $fn)
{
    $this->postRoutes[$url] = $fn;
}

   public function resolve()
    {
        $method = strtolower($_SERVER['REQUEST_METHOD']);
        $url = $_SERVER['PATH_INFO'] ?? '/';

        if ($method === 'get') {
            $fn = $this->getRoutes[$url] ?? null;
        } else {
            $fn = $this->postRoutes[$url] ?? null;
        }
        if (!$fn) {
            echo 'Page not found';
            exit;
        }
        echo call_user_func($fn, $this);
    }

然后它会工作。

我也不会将路由器与视图混合在一起。

【讨论】:

  • 请分享更多细节。给定错误消息的哪些部分让您认为自动加载器有任何问题? “参数 #1 ($callback) 必须是有效的回调,数组必须正好有两个成员”没有提到任何此类问题
  • @NicoHaase 你说得对,我只看到标题中的错误,而不是帖子中的错误
  • @VitalijMik 我用您的代码替换了我的代码,错误已更改。 ,这是新错误:Fatal error: Uncaught TypeError: app\Router::get(): Argument #2 ($fn) must be of type callable, array given, called in C:\xampp\htdocs\03_good\public\index.php on line 11 and defined in C:\xampp\htdocs\03_good\Router.php:28 Stack trace: #0 C:\xampp\htdocs\03_good\public\index.php(11): app\Router-&gt;get('/', Array) #1 {main} thrown in C:\xampp\htdocs\03_good\Router.php on line 28 我分享了我的index.php 了解更多详情
猜你喜欢
  • 2023-02-08
  • 2021-10-17
  • 2021-07-05
  • 2021-06-14
  • 2023-03-27
  • 1970-01-01
  • 2017-06-24
  • 2021-11-27
  • 2014-01-29
相关资源
最近更新 更多