【问题标题】:Fatal error when instanciating Pusher - Can't find class实例化 Pusher 时出现致命错误 - 找不到类
【发布时间】:2018-06-04 15:28:27
【问题描述】:

我有一个项目要测试和玩,结构如下:

app/
    controllers/
        HomeController.php
    handlers/
    models/
vendor/
    composer/
    psr/
    pusher/
        pusher-php-server/
            src/
                Pusher.php
                PusherException.php
                PusherInstance.php
            tests/
            composer.json
    autoload.php
index.php

我试图在我的索引文件中要求 Pusher 自动加载器:

require 'vendor/autoload.php';

如下:

// autoload.php @generated by Composer
   require_once __DIR__ . '/composer/autoload_real.php';
   return ComposerAutoloaderInite16b90ab01042d2a69b1d54243c9e23a::getLoader();

现在,在我的HomeController.php 中,我有以下代码:

namespace App\controllers;

use \App\handlers\Views as View;
use \App\models\Home_model as Home;
use \App\controllers\SessionController;
use \Pusher\Pusher as Pusher;

class HomeController {

    private $pusher;

    public function __construct() {
        $options = array(
            'cluster' => 'hmm',
            'encrypted' => true
        );

        $this->pusher = new Pusher(
            'secret',
            'secret',
            'secret',
            $options
        );
    }

    public function index() {

        $data['message'] = 'hello world';
        $this->pusher->trigger('my-channel', 'my-event', $data);

        return $this->view->render('views/home/index.php');
    }
}    

但这会给我一个错误:

Fatal error: Class 'Pusher\Pusher' not found in

而且我不确定我做错了什么。有人可以解释一下我做错了什么吗?

composer.json 我得到以下信息:

{
    "name": "pusher/pusher-php-server",
    "description" : "Library for interacting with the Pusher REST API",
    "keywords": ["php-pusher-server", "pusher", "rest", "realtime", "real-time", "real time", "messaging", "push", "trigger", "publish", "events"],
    "license": "MIT",
    "require": {
        "php": "^5.4 || ^7.0",
        "ext-curl": "*",
        "psr/log": "^1.0"
    },
    "require-dev": {
        "phpunit/phpunit": "^4.8 || ^5.7"
    },
    "autoload": {
        "psr-4": {
            "Pusher\\": "src/"
        }
    },
    "autoload-dev": {
        "psr-4": { "": "tests/" }
    },
    "config": {
        "preferred-install": "dist"
    },
    "extra": {
        "branch-alias": {
            "dev-master": "3.0-dev"
        }
    },
    "minimum-stability": "dev",
    "prefer-stable": true
}

在 Github 上,他们提到该库依赖于 cURL 和 JSON 模块。

不确定这是否与我的问题有关? 我仍然卡住了,非常感谢任何帮助。

另外,我正在使用 .htaccess 文件来重写我的网址。

【问题讨论】:

  • 您能告诉我们您的autoload.php 和更多关于vendor/pusher 下目录的信息吗?
  • 我已经更新了我的问题。
  • 命名空间Pusher 好像没有定义好,你能在composer.json或者文件夹vendor/composer里面找到它的声明吗?
  • 我又更新了,是composer.json文件的内容
  • class HomeController ... 在哪里?我看到 namespaceuse 声明,然后是 private $pusher 但没有 class 声明

标签: php namespaces pusher


【解决方案1】:

我已管理您的代码以与composer.json 旁边的index.php 一起使用:

{
    "name": "awesome/project",
    "type": "project",
    "license": "MIT",
    "authors": [
        {
            "name": "Author",
            "email": "author@gmail.com"
        }
    ],
    "autoload": {
        "psr-4": {
            "": ""
        }
    },
    "require": {
        "pusher/pusher-php-server": "dev-master"    
    }
}

然后运行composer install。 我的index.php 内容:

<?php

require 'vendor/autoload.php';

use app\controllers\HomeController;

$ctrl = new HomeController();

【讨论】:

  • 我已经尝试过了,但它仍然给我同样的错误。没有新的行为。您是指项目根目录中的作曲家文件吗?
  • 是的。 index.php 中应该有自动加载器。
  • 查看github.com/EgorBurykin/pusher。克隆后做composer install。然后运行php index.php
  • 这确实有效,但不适用于我的项目。我确实设法通过在我的自动加载器中包含所有必需的目录来使其工作。
  • 好吧,我只是使用提供的信息来解决问题。确实,如果你有不同的问题解决方案会有所不同。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-10-27
  • 2014-10-03
  • 2015-05-27
  • 1970-01-01
  • 2010-11-16
  • 1970-01-01
相关资源
最近更新 更多