Jan Beeck
Jan Beeck

Reputation: 315

How to call variables and functions from an external library in Yii2?

I call to an external library in a fileForm in the models layer

namespace app\models;
use Yii;
use yii\base\Model;
include "../lib/libchart/classes/libchart.php"; //external library

However Yii2 does not recognize a variable from that external library, the error is: "PHP Fatal Error – yii\base\ErrorException - Class 'app\models\LineChart' not found"

I have the following function in the fileForm:

public function insertGrafic() {
$chart = new LineChart(); ... }

What's missing? Thanks.

Upvotes: 0

Views: 1323

Answers (3)

Jan Beeck
Jan Beeck

Reputation: 315

I have solved the issue by using another wraper (https://github.com/miloschuman/yii2-highcharts). (1) I put the following lines in the require section of the composer.json file: "yiisoft/yii2-jui": "*", "miloschuman/yii2-highcharts-widget": "dev-master" (2) And then run in the console the command: composer update

This is the link of the other thread: How to add google-chart correctly in Yii2?

Upvotes: 0

Yonas Alemayehu
Yonas Alemayehu

Reputation: 111

yii2 is fully namespaced. instead of using includes you have to use namespace. based on the error message it looks the external library has a namespace as a result you can try as below

use app\models\LineChart;

remove the include and try

Upvotes: 0

Sageth
Sageth

Reputation: 1112

Just try to add the library as a Component in your config file.

Write & use a custom Component in Yii2.0

'components' => [
    'libchart' => [
        'class' => 'class Path',
    ],

Then use your component method like this:

Yii::$app->libchart->method();

Upvotes: 0

Related Questions