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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25695408/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 10:03:14  来源:igfitidea点击:

Laravel 4.2: Include a PHP file (library) into controller

phplaravellaravel-4

提问by Oscar Gallo

I'm doing a project using Laravel 4.2 where I need to include a PHP file (a library to transform a PDF into Text) into the controller, and then return a variable with the text, any idea how?

我正在使用 Laravel 4.2 做一个项目,我需要在控制器中包含一个 PHP 文件(一个将 PDF 转换为文本的库),然后返回一个带有文本的变量,知道怎么做吗?

This is my controller:

这是我的控制器

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

And my /app/start/global.phpfile:

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

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

));

And this is the error:

这是错误

include(includes/vendor/autoload.php): failed to open stream: No such file or directory

采纳答案by Oscar Gallo

I think you're right bro, but, i find another way, maybe not the right way, but it works.

我认为你是对的兄弟,但是,我找到了另一种方法,也许不是正确的方法,但它有效。

Is like this, I created a new folder called Includes and put my files in there, then in the /app/start/global.php i added this line:

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

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

And now is working :D

现在正在工作:D

回答by eluong

You can create a new directory somewhere in your app directory, for example, app/libraries

您可以在应用程序目录的某处创建一个新目录,例如, app/libraries

Then in your composer.json file, you can include app/librariesin your autoload classmap:

然后在您的 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",
}

Be sure to run a composer dump-autoloadafter modifying your composer.json.

请务必composer dump-autoload在修改您的 composer.json 后运行 a 。

Let's assume your class name is called CustomClass.phpand it is located in the app/librariesdirectory (so the full path is app/libraries/CustomClass.php). If you have properly namespaced your class, by convention, your namespace would probably be named libraries. Just for the sake of clarity, we will call our namespace customto avoid any confusion with the directory.

让我们假设你的类名被调用CustomClass.php并且它位于app/libraries目录中(所以完整路径是app/libraries/CustomClass.php)。如果您已正确命名您的类,按照惯例,您的命名空间可能会被命名为libraries. 为了清楚起见,我们将调用我们的命名空间custom以避免与目录混淆。

$class = new \custom\CustomClass();

Alternatively, you can give it an alias in your app/config/app.phpfile:

或者,您可以在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',
    ...
)

And you can instantiate the class from anywhere in your application as you would with any other class:

您可以像使用任何其他类一样从应用程序的任何位置实例化该类:

$class = new CustomClass();

Hope this helps!

希望这可以帮助!