Laravel 4.2: include PHP file (library) in the controller

I am doing a project using Laravel 4.2, where I need to include a PHP file (a library for converting PDF to text) in the controller, and then return a variable with text, any idea how?

This is my controller :

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

And my file /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 a mistake :

 include(includes/vendor/autoload.php): failed to open stream: No such file or directory 
+7
php laravel laravel-4
source share
2 answers

I think you are right, but I find another way, maybe not the right way, but it works.

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

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

And now it works: D

+4
source share

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

Then, in your composer.json file, you can include app/libraries in your startup class map:

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

Suppose your class name is called CustomClass.php and is located in the app/libraries directory (so the full path is app/libraries/CustomClass.php ). If you correctly placed your class on your class, your convention will most likely be called libraries . For clarity, we will call our custom namespace to avoid directory confusion.

 $class = new \custom\CustomClass(); 

Alternatively, you can give it an alias in the 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 create an instance of the class from anywhere in your application, as with any other class:

 $class = new CustomClass(); 

Hope this helps!

+16
source share

All Articles