Can't load a Non-Laravel Composer package

孤街浪徒 提交于 2019-12-22 05:22:36

问题


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:

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;

回答1:


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.



来源:https://stackoverflow.com/questions/36745626/cant-load-a-non-laravel-composer-package

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!