【问题标题】:Laravel 5: Library class not foundLaravel 5:找不到库类
【发布时间】:2016-07-13 06:14:23
【问题描述】:

我已经从 github 安装了这个库:

https://github.com/robholmes/term-extractor

我把文件放在public/term-extractor下。

我尝试创建一个路由来测试库的结果,但我不断收到错误Class 'TermExtractor' not found

路线如下:

Route::get('/test', function()
{
    require public_path() . '/term-extractor/src/TermExtractor/TermExtractor.php';

    $text = 'Inevitably, then, corporations do not restrict themselves merely to the arena of economics. Rather, as John Dewey observed, "politics is the shadow cast on society by big business". Over decades, corporations have worked together to ensure that the choices offered by \'representative democracy\' all represent their greed for maximised profits. This is a sensitive task. We do not live in a totalitarian society - the public potentially has enormous power to interfere. The goal, then, is to persuade the public that corporate-sponsored political choice is meaningful, that it makes a difference. The task of politicians at all points of the supposed \'spectrum\' is to appear passionately principled while participating in what is essentially a charade.';

    $extractor = new TermExtractor();
    $terms = $extractor->extract($text);
    // We're outputting results in plain text...
    header('Content-Type: text/plain; charset=UTF-8');
    // Loop through extracted terms and print each term on a new line
    foreach ($terms as $term_info) {
        // index 0: term
        // index 1: number of occurrences in text
        // index 2: word count
        list($term, $occurrence, $word_count) = $term_info;
        echo "$term\n";
    }
});

怎么了?

【问题讨论】:

  • 你把别名和提供者放在app.php中了吗?
  • @KuKeC 我到底要放什么?

标签: laravel laravel-5 laravel-5.2


【解决方案1】:

首先,您应该为 TermExtractor 添加您的 composer.json 依赖项

"require": {
"robholmes/term-extractor" : "3.*"
}

在文件app.php 中你需要做两件事(像这样,不知道别名和提供者的正确名称(一定要在做之前做composer update

首先添加提供者

'providers' => [
term-extractor/TermExtractorProvider::class
]

第二次添加别名

'aliases' => [
'TermExtractor' => term-extractor\TermExtractor\Facades\TermExtractor::class,
]

这应该给你别名TermExtractor,你可以在整个应用程序中使用它,而无需每次都做require public_path() . '/term-extractor/src/TermExtractor/TermExtractor.php';

希望对你有帮助

【讨论】:

  • 然后将其从 composer.json 中删除,并在 this 的帮助下手动添加
  • 如何找到别名和提供者的名称?
  • 我也在 Guthub 中寻找,但找不到。我认为你将不得不做你的别名和提供者。要获得更多帮助,请查看this
猜你喜欢
  • 2015-07-17
  • 1970-01-01
  • 2015-10-20
  • 2016-07-06
  • 2016-11-26
  • 2015-07-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多