Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't load a Non-Laravel Composer package

It's the first time that I try to load a Composer package that doesn't use a Laravel service provider or a facade.

I am trying to install this package: https://github.com/mollie/mollie-api-php

I have followed the steps to install the package with Composer.

At the top of my controller I added:

require_once base_path('vendor/Mollie/API/Client.php');

I get the following error:

main(): Failed opening required '../vendor/Mollie/API/Client.php' (include_path='.:/Applications/MAMP/bin/php/php7.0.0/lib/php')

It can't find the file. Even though the path in the error is the path where the class is located. Are there more steps I should do?

Structure of the package:

enter image description here

composer.json from the package:

"autoload": {
    "psr-0": {
        "": "src/"
    }

Update: My controller has this namespace

namespace App\Http\Controllers;

So when i just try to new up my class it obviously can't find that class inside that namespace. So how do i ignore the namespace for 1 class

Because this will not work inside this controller:

$mollie = new Mollie_API_Client;
like image 471
Christophvh Avatar asked Apr 20 '16 13:04

Christophvh


People also ask

How install packages Laravel manually?

Via Download. Once Composer is installed, download the 4.2 version of the Laravel framework and extract its contents into a directory on your server. Next, in the root of your Laravel application, run the php composer. phar install (or composer install ) command to install all of the framework's dependencies.

How do I upload my package to composer?

Publishing Composer packages from a Space Git repository Make sure your PHP library is prepared for distribution – your PHP project has a valid composer. json file. The project is stored in a Space Git repository. In Space, open the repository page and click Submit Composer Packages.

How can I download Laravel without composer?

phar file and running commands on it. You do not need to run Composer on your server as well as locally, once you run composer install or composer update your project will have all its dependencies available and you can just upload it straight to your server. Hope it helps!!

Is Composer part of Laravel?

Composer in Laravel is used to manage their dependencies. Once Composer is installed, download the framework and extract its contents into a directory on your server. The primary task you should do after installing Laravel is to set your application key to a random string.


1 Answers

As noted in the comments, Composer handles the autoloading for you - manually re-requiring it isn't necessary and may in fact cause problems.

my controller has a namespace, so it tries to load that class from the namespace, how do i ignore it for 1 class?

You can reference the class with a leading \, i.e. new \Mollie_API_Client, which will tell PHP to look in the root instead of your controller's namespace.

You can also put use Mollie_API_Client; at the top of the file to accomplish a similar thing. You'll see use statements at the top of a lot of Laravel files for this reason.

like image 150
ceejayoz Avatar answered Oct 01 '22 22:10

ceejayoz