Adding a non-laravel package to a Laravel composer project

When using Laravel, I know the steps that should be followed to use a third-party library in my project using the composer:

  • Add the package to the .json composer:

    "require": { "zizaco/confide": "3.2.x" }

  • Run composer updateto install the package

  • Add to providers and alias arrays in config/app.php

I am trying to do the same with highchartsphp . Installation through the composer is quite simple, but there are no instructions on how to use this package with Laravel. How to load the correct file and how to instantiate the class as described in readme? Is this just a case of adding it to providers and aliases, and then doing it $chart = new HighChart();wherever I want?

+4
source share
1 answer

This is not a Laravel package, so you do not have a Service Provider or Alias ​​to configure, but it is a PHP package, and since you use Composer to install it, it is already loaded automatically, so you can simply:

Add the package to your composer.json:

{
    "require": {
        "ghunti/highcharts-php": "~2.0"
    }
}

Run

composer dumpautoload

And create an instance:

$chart = new Ghunti\HighchartsPHP\Highchart();

Or use it at the top of your 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.phpfor it if you prefer to use it as follows:

'Highchart' => 'Ghunti\HighchartsPHP\Highchart'

But you still have to create it

$chart = new Highchart();

You will not be able to use it, as in Laravel

Highchart::doWhatever();

If you yourself do not create a ServiceProvider,

+6
source

Source: https://habr.com/ru/post/1540666/


All Articles