Autoloading 3rd party composer packages into Laravel 5 Application - php

I am trying to get my head around Composer. I want to integrate this package into my app: https://github.com/thiagoalessio/tesseract-ocr-for-php
I have done the following so far:
composer require thiagoalessio/tesseract_ocr
composer dump-autoload
and I have used the library within a Controller method as such:
$tesseract = new TesseractOCR($url);
But I'm getting the dreaded:
Class 'App\Http\Controllers\TesseractOCR' not found
How do I make sure that composer autoloads the package and that it is available throughout the apps, for usage within controllers?
Sorry, newbie in Composer here..

Composer has auto-loaded the package, you’re just referencing the class name incorrectly.
TesseractOCR lives in the “global” namespace, so you need to import it:
use TesseractOCR;
You can then use it in your controllers etc as normal.

Related

How to implement composer in yii 1?

Hello I have old project written in php framework Yii 1.1. Dependencies are added manually by uploading extensions in extension folder.
I want to make my project use composer to track third-party code into vendor directory. So "extensions" directory would not exist.
Existing extensions are not namespaced and are used manually in controllers: Yii::import('application.models.black_lists.domains');
So is this possible to achieve and how?
Thanks
You have to require the composer autoloader before any vendor is used in your code.
To do so, you have to unregister Yii autoloader before you require the composer one
spl_autoload_unregister(array('YiiBase','autoload'));
require Yii::getPathOfAlias('application.vendor').DIRECTORY_SEPARATOR.'autoload.php';
spl_autoload_register(array('YiiBase','autoload'));
After this, you should be able to call any class in the vendor folder, the composer way.
new \Owner\Module();

Symfony - manually add phpxmlrpc to vendor

I need to use XML-RPC on my project. I have found a library phpxmlrpc (http://phpxmlrpc.sourceforge.net/) and I need to add it to vendor. I have copied the files in vendor folder (/vendor/phpxmlrpc/) and I need to see the xmlrpc_client class in my Controller. But I am not able to manage how to edit autoload.php to see the class, after a few attemps I am still getting "Attempted to load class "xmlrpc_client" from the global namespace.
Did you forget a "use" statement?" so I am pretty sure that there is some mess in my structure. I would really appreciate any help.
You must use a composer install tools for integrate 3third party code in your project a lot of possible time.
For XML-RPC you have this bundle : Symfony-rpc-bundle
When you install with composer install your bundle a lot of tricks run in your project symfony. Don't forget to add this bundle in your AppKernel.php file.
With this your code for XML-RPC is more upkeeping and stable.
A bit late with the answer, I fear, but phpxmlrpc can now be installed using Composer as you would do with any other package.
When checking out info about that library, just make sure that you look up the latest version on GitHub and not any more on SourceForge.

"plugin" composer project, dependencies to be able to access main project

So I can't quite figure this one out. We have an API as a composer project.
No we which to add functionality to this API in a modular fashion creating separate composer projects for each module.
But how do I resolve dependencies while developing? Each module need access to the "core" API project to be able to test out code.
The current API is not a real composer package yet. But my initial thought is to create a new package for the module I'm about to develope, and then add a dev-dependency for the "core" API.
Or how should I do it?
Are your modules standalone resp. without the "Core" package usable?
I suppose not, so the "Core" module is a dependency for each of your modules.
Ergo the "you/core" package needs to be in the require block of composer.json of every module.
Note that this is not a dev-dependancy then, because i suppose your module wouldnt be usable without the core package.
Phpunit is a classic dev-dependency because the functionality of your module would still work if there would be no phpunit.
To test & develop the module, you would run a composer install in your module project folder to fetch all dependencies into a vendor folder. Then you can develop and have all your dependencies present. You would need to require the composer autoloader, though. F.e in your phpunit.xml.dist.
Dont forget to add vendor to your .gitignore

Issue when using a package (pagseguro/php) with composer in a laravel app, apparently it is not loading or initializing itself

I have a laravel app and i want to use the pagseguro/php package.
I added it to the composer.json and updated. I can access the main class (PagSeguroPaymentRequest) without a problem.
At some point I have to call this:
PagSeguroConfig::getAccountCredentials();
But it throws an exception. After reading code around I thought on trying to init the library by myself and suddenly everything worked:
PagSeguroLibrary::init();
This method is inside the only php file in source/PagSeguroLibrary/
Shouldn't composer automatically execute this method? What is exactly "loading" a package? Is there anyway to fix this using composer only?
Thank you all.
Shouldn't composer automatically execute this method?
No, it shouldn't. Composer is a package and dependency manager program. It's job is to
Get PHP files into your vendor folder
If using those PHP files means you need other PHP files, get those other PHP files into your vendor folder
Setup things so that class files from the packages are correctly autoloaded in PHP (i.e. no need to require or include stuff yourself)
Composer packages work independent of frameworks. Someone could distribute a laravel service provider via a computer package, or someone could distribute code that doesn't know anything about Laravel. How each composer package works is up to the author (always read the README)
In the case of pagseguro/php, it looks like you're supposed to instantiate a PagSeguroPaymentRequest object which, when autoloaded, will automatically call init. The examples distributed with the package also makes it look like this package was code that predated composer, and still uses many manual includes and requires.

Download a repository in Github along with composer packages

Without using Composer, is it possible to download a repository in Github along with it's defined composer packages?
For example: FluxBB 2 requires Laravel 4.
I was hoping to download FluxBB and at the same time the packages of Laravel 4 without using Composer.
Usually projects that use composer will ignore 3rd party components. In .gitignore you will see /vendor. This is the place where Composer downloads its dependencies.
This will find the latest version of monolog/monolog that matches the supplied version constraint and download it into the vendor directory. It's a convention to put third party code into a directory named vendor. In case of monolog it will put it into vendor/monolog/monolog.
Tip: If you are using git for your project, you probably want to add vendor into your .gitignore. You really don't want to add all of that code to your repository.
http://getcomposer.org/doc/01-basic-usage.md#installing-dependencies
Doing it manually is a bit of a hassle. Composer uses packagist to get its files (if you look at a package it has a source added to it Laravel https://packagist.org/packages/laravel/framework).
Composer auto loads the needed files automatically so its a big time saver.
For libraries that specify autoload information, Composer generates a vendor/autoload.php file. You can simply include this file and you will get autoloading for free.
require 'vendor/autoload.php';
This makes it really easy to use third party code. For example: If
your project depends on monolog, you can just start using classes from
it, and they will be autoloaded.
http://getcomposer.org/doc/01-basic-usage.md#autoloading

Categories