9

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?

This is my controller:

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

And my /app/start/global.php file:

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

2 Answers 2

16

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!

Sign up to request clarification or add additional context in comments.

3 Comments

I have added "app/libraries" to my composer.json autoload->classmap section. I then added TextCleaner => 'libraries\TextCleaner to my app/config/app.php aliases. I have tried to instantiate my class like $imageCleaner = new \libraries\TextCleaner() AND like $imageCleaner = new TextCleaner(); and both error out for me saying the class doesn't exist. Yes I have run the composer dump-autload successfully.
@BillGarrison Ensure that you have namespaced your class. Assume that your namespace for your class is called 'helpers' (namespace helpers; at the top of your class). In your config/app.php file, you would include it as 'TextCleaner' => 'helpers\TextCleaner'. I edited the original answer to help clarify.
helped me today :) +1
6

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.