Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I include a composer package into plain php?

Good afternoon, I will start off by saying that I've never included a composer project in my projects unless it was a composer package for the Laravel framework. Where you "require" it and add it to the providers array and aliases if needed.

Now, the problem. I have a composer package I'm trying to play around with https://github.com/WHAnonymous/Chat-API the problem is i dont know how to include it into myu project since its not really made "for" laravel. So I'm trying to use plain php without a framework but I have no idea how to "load" the package in, tried googling and only found information regarding building a package.

To clarify: I can install the package fine, its the php part of "loading" the package in my index.php file that im struggling with, pretend the index.php file is empty.

Can someone please help me?

like image 804
icetimux Avatar asked Sep 18 '15 02:09

icetimux


Video Answer


1 Answers

After installing the package with composer, the composer has generated an autoloader that you can include with:

require_once 'vendor/autoload.php';

Then you can use the classes of the package without further includes. In your example this might be:

// Create an instance of WhatsProt.
$w = new WhatsProt($username, $nickname, $debug);

(taken from https://github.com/WHAnonymous/Chat-API/blob/master/examples/exampleRegister.php)

Note that this line from the example is not necessary, when you use the composer autoloader:

require_once('../src/whatsprot.class.php');
like image 129
Fabian Schmengler Avatar answered Oct 12 '22 23:10

Fabian Schmengler