无法加载Non-Laravel Composer软件包 [英] Can't load a Non-Laravel Composer package
问题描述
这是我第一次尝试加载不使用Laravel服务提供商或Facade的Composer软件包.
It's the first time that I try to load a Composer package that doesn't use a Laravel service provider or a facade.
我正在尝试安装此软件包: https://github.com/mollie/mollie-api-php
I am trying to install this package: https://github.com/mollie/mollie-api-php
我已按照步骤使用Composer安装软件包.
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');
我收到以下错误:
main():无法打开所需的'../vendor/Mollie/API/Client.php' (include_path ='.::/Applications/MAMP/bin/php/php7.0.0/lib/php')
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?
软件包的结构:
composer.json
:
"autoload": {
"psr-0": {
"": "src/"
}
更新: 我的控制器具有此名称空间
Update: My controller has this namespace
namespace App\Http\Controllers;
因此,当我尝试更新班级时,显然无法在该名称空间中找到该班级.那么我如何忽略1个类的命名空间
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;
推荐答案
如注释中所述,Composer会为您处理自动加载-手动重新要求它是不必要的,实际上可能会导致问题.
As noted in the comments, Composer handles the autoloading for you - manually re-requiring it isn't necessary and may in fact cause problems.
我的控制器有一个名称空间,因此它尝试从名称空间中加载该类,我如何在1个类中忽略它?
my controller has a namespace, so it tries to load that class from the namespace, how do i ignore it for 1 class?
您可以使用前导\引用该类,即new \Mollie_API_Client
,它将告诉PHP在根目录中查找而不是在控制器的命名空间中.
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.
您也可以将use Mollie_API_Client;
放在文件的顶部以完成类似的操作.因此,您会在许多Laravel文件的顶部看到use
语句.
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.
这篇关于无法加载Non-Laravel Composer软件包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!