Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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
like image 284
Oscar Gallo Avatar asked Sep 05 '14 23:09

Oscar Gallo


People also ask

How do I add a controller in Laravel?

Open the command prompt or terminal based on the operating system you are using and type the following command to create controller using the Artisan CLI (Command Line Interface). Replace the <controller-name> with the name of your controller. This will create a plain constructor as we are passing the argument — plain.

Where are controllers file located in Laravel?

In Laravel, a controller is in the 'app/Http/Controllers' directory. All the controllers, that are to be created, should be in this directory. We can create a controller using 'make:controller' Artisan command.


2 Answers

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!

like image 126
eluong Avatar answered Oct 07 '22 17:10

eluong


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

like image 21
Oscar Gallo Avatar answered Oct 07 '22 15:10

Oscar Gallo