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

前端之家收集整理的这篇文章主要介绍了Laravel 4.2:在控制器中包含一个PHP文件(库)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用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
您可以在app目录中的某个位置创建新目录,例如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","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).如果按照惯例对类进行了正确命名,则命名空间可能会命名为库.为了清楚起见,我们将调用我们的命名空间自定义以避免与目录混淆.

$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();

希望这可以帮助!

原文链接:https://www.f2er.com/laravel/138411.html

猜你在找的Laravel相关文章