使用 composer 将非 laravel 包添加到我的 Laravel 项目中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23675320/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding a non-laravel package to my Laravel project with composer
提问by harryg
When using Laravel I am aware of the steps to follow to use a 3rd party library in my project using composer:
使用 Laravel 时,我知道在我的项目中使用 Composer 使用 3rd 方库要遵循的步骤:
Add the package to composer.json:
"require": { "zizaco/confide": "3.2.x" }
Run
composer update
to install the package- Add to the providers and alias arrays in
config/app.php
将包添加到 composer.json:
"require": { "zizaco/confide": "3.2.x" }
运行
composer update
安装包- 添加到提供者和别名数组中
config/app.php
I am trying to do the same with highchartsphp. Installing via composer is simple enough but there are no instructions on how to use this package with Laravel. How to load the correct file and how to I instantiate the class as described in the readme? Is it just a case of adding it to the providers and aliases and then doing $chart = new HighChart();
wherever I want?
我正在尝试对highchartsphp做同样的事情。通过 composer 安装很简单,但是没有关于如何在 Laravel 中使用这个包的说明。如何加载正确的文件以及如何按照自述文件中的描述实例化类?这只是将它添加到提供者和别名然后$chart = new HighChart();
随心所欲的情况吗?
回答by Antonio Carlos Ribeiro
This is not a Laravel package, so you don't have Service Provider nor Alias to setup, but this is a PHP package and since you use Composer to install it, it is already autoloaded, so you can just:
这不是 Laravel 包,因此您无需设置 Service Provider 或 Alias,但这是一个 PHP 包,并且由于您使用 Composer 安装它,因此它已经自动加载,因此您只需:
Add the package to your composer.json:
将包添加到您的 composer.json:
{
"require": {
"ghunti/highcharts-php": "~2.0"
}
}
Run
跑
composer dumpautoload
And instantiate it:
并实例化它:
$chart = new Ghunti\HighchartsPHP\Highchart();
Or use it in the top of your php:
或者在你的 php 顶部使用它:
use Ghunti\HighchartsPHP\Highchart;
And you should be able to:
你应该能够:
$chart = new Highchart(Highchart::HIGHSTOCK);
Anywhere in your project and it should work.
在您的项目中的任何地方,它都应该可以工作。
You can create an alias in app/config/app.php
for it if you prefer to use it this way:
app/config/app.php
如果您更喜欢以这种方式使用它,您可以为其创建别名:
'Highchart' => 'Ghunti\HighchartsPHP\Highchart'
But you will still have to instantiate it
但是你仍然需要实例化它
$chart = new Highchart();
You won't able to use it as in Laravel
您将无法像在 Laravel 中那样使用它
Highchart::doWhatever();
Unless you create a ServiceProvider yourself, of course.
当然,除非您自己创建 ServiceProvider。