Laravel 4.2:将一个PHP文件(库)包含到控制器中

9

我正在使用Laravel 4.2进行一个项目,其中需要将一个PHP文件(用于将PDF转换为文本的库)包含到控制器中,然后返回一个带有文本的变量。有什么想法吗?

这是我的控制器

public function transform() {
    include ('includes/vendor/autoload.php');
}

我的/app/start/global.php文件:

ClassLoader::addDirectories(array(
    app_path().'/commands',
    app_path().'/controllers',
    app_path().'/models',
    app_path().'/database/seeds',
    app_path().'/includes',

));

这是一个错误提示:
include(includes/vendor/autoload.php): failed to open stream: No such file or directory
2个回答

16
你可以在应用程序目录中的某个位置创建一个新目录,例如:app/libraries 然后在你的composer.json文件中,你可以将app/libraries包含在你的自动加载类映射中:
{
    "name": "laravel/laravel",
    "description": "The Laravel Framework.",
    "keywords": ["framework", "laravel"],
    "license": "MIT",
    "require": {
        "laravel/framework": "4.2.*",
    },
    "autoload": {
        "classmap": [
            "app/commands",
            "app/controllers",
            "app/models",
            "app/libraries", <------------------ YOUR CUSTOM DIRECTORY
            "app/database/migrations",
            "app/database/seeds",
            "app/tests/TestCase.php"
        ]
    },
    "scripts": {
        "post-install-cmd": [
            "php artisan clear-compiled",
            "php artisan optimize"
        ],
        "post-update-cmd": [
            "php artisan clear-compiled",
            "php artisan optimize"
        ],
        "post-create-project-cmd": [
            "php artisan key:generate"
        ]
    },
    "config": {
        "preferred-install": "dist"
    },
    "minimum-stability": "stable",
}

请在修改composer.json文件后务必运行composer dump-autoload命令。
假设你的类名为CustomClass.php,并且它位于app/libraries目录下(因此完整路径为app/libraries/CustomClass.php)。如果你已经按照惯例正确地给你的类设置了命名空间,那么你的命名空间可能会被命名为libraries。为了清晰起见,我们将使用custom作为命名空间名称,以避免与目录混淆。
$class = new \custom\CustomClass();

或者,您可以在app/config/app.php文件中为其设置别名:

/*
|--------------------------------------------------------------------------
| Class Aliases
|--------------------------------------------------------------------------
|
| This array of class aliases will be registered when this application
| is started. However, feel free to register as many as you wish as
| the aliases are "lazy" loaded so they don't hinder performance.
|
*/

'aliases' => array(
    ...
    'CustomClass'   => 'custom\CustomClass',
    ...
)

您可以像实例化其他类一样,在应用程序的任何地方实例化该类:

$class = new CustomClass();

希望这可以帮助你!

我已经在composer.json的autoload->classmap部分中添加了“ app / libraries”。 然后我将TextCleaner => 'libraries \ TextCleaner'添加到app / config / app.php别名中。 我尝试使用$imageCleaner = new \ libraries \ TextCleaner()$imageCleaner = new TextCleaner();实例化我的类,但都报错,说类不存在。 是的,我已经成功运行了composer dump-autload。 - Bill Garrison
@BillGarrison 请确保你的类已经命名空间化。假设你的类的命名空间叫做“helpers”(在你的类顶部加上namespace helpers;)。在你的config/app.php文件中,你需要将它包含进去,例如:'TextCleaner' => 'helpers\TextCleaner'。我编辑了原始答案以帮助澄清。 - eluong
今天帮了我很多,谢谢!+1 - Sajad Karuthedath

6

我认为你是对的,但我找到了另一种方法,也许不是正确的方法,但它可以工作。

就像这样,我创建了一个名为Includes的新文件夹,并将我的文件放在其中,然后在/app/start/global.php中添加了这一行:

require app_path().'/includes/vendor/autoload.php';

现在已经可以工作了 :D


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接