实施Pusher时的致命错误 - 无法找到课程

时间:2018-06-04 15:28:28

标签: php namespaces pusher

我有一个测试和玩耍的项目,具有以下结构:

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文件来重写我的网址。

1 个答案:

答案 0 :(得分:1)

我已将您的代码设置为使用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(); 内容:

{{1}}