首页 > 解决方案 > 在 yii2 控制器中使用 composer 包

问题描述

我已经使用composer安装了一个composer包,我还检查了包是否加载到index.php入口脚本中,这就是它的样子

<?php

// comment out the following two lines when deployed to production
defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'dev');

require __DIR__ . '/../vendor/autoload.php';
require __DIR__ . '/../vendor/yiisoft/yii2/Yii.php';

$config = require __DIR__ . '/../config/web.php';

(new yii\web\Application($config))->run();

我已经安装了这个包composer require php-http/curl-client typesense/typesense-php,我在我的控制器中使用它

<?php

namespace app\controllers;


use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\web\Response;
use yii\filters\VerbFilter;
use yii\mongodb\Query;
use Symfony\Component\HttpClient\HttplugClient;
use Typesense\Client;

class UsersController extends Controller
{
    
public function init() 

{
$client = new Client(
        [
            'api_key' => '',
            'nodes' => [
                [
                    'host' => 'localhost',
                    'port' => '8108',
                    'protocol' => 'http',
                ],
            ],
            'client' => new HttplugClient(),
        ]
    );
}

我什至用构造函数替换了 init()public function __construct() 但我得到了这个错误

找不到类“Typesense\Client”

在这一行为$client = new Client( 什么没有加载类?我已经检查了它在我的供应商目录中的存在。

标签: phpyii2typesense

解决方案


推荐阅读