0

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.

1

3 Answers 3

0

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();
Sign up to request clarification or add additional context in comments.

7 Comments

I added the former 'components' statement in config\web.php and in config\console.php ('libchart' => [ 'class' => 'lib\libchart\classes\libchart.php', ], ) And I get this message: "Class lib\libchart\classes\libchart.php does not exist" What is wrong? Thanks.
Check if the library extends the interface Component of Yii and has a valid namespace. If not, add it, it should not change how library works.
How do I add the all third-party and create its namespace? Thanks
copy it in your project folder, you can create a special foler base/libchart for example. Then open the class file and add the namespace namespace app\libchart. Use the guide i've post to do it properly.
I apreciate your help. I am getting something wrong in the logic (I get this error: Namespace missing?). I created a folder base/libchart. Then I created a file libchart.php with class libchart extends Component {public function welcome()... Then at the model's layer I call public function insert(){Yii::$app->libchart->welcome();....}
|
0

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

2 Comments

You mean its not necessary to set the path where the external library is located in the project? Thanks.
What is the difference in this new Yii2 version? walterebert.com/blog/…
0

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?

1 Comment

While this may answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.