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

前端 未结 2 872
名媛妹妹
名媛妹妹 2020-12-15 10:57

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 t

相关标签:
2条回答
  • 2020-12-15 11:22

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

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

    {
        "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-autoload after modifying your composer.json.

    Let's assume your class name is called CustomClass.php and it is located in the app/libraries directory (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 custom to avoid any confusion with the directory.

    $class = new \custom\CustomClass();
    

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

    /*
    |--------------------------------------------------------------------------
    | 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!

    0 讨论(0)
  • 2020-12-15 11:22

    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:

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

    And now is working :D

    0 讨论(0)
提交回复
热议问题