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.

Jan Beeck
  • 303
  • 8
  • 24
  • Hope it helps http://stackoverflow.com/questions/28128117/using-classes-without-namespace-with-yii2 – SiZE Aug 21 '15 at 05:30

3 Answers3

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();
Sageth
  • 1,102
  • 1
  • 15
  • 30
  • 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. – Jan Beeck Aug 24 '15 at 20:35
  • 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. – Sageth Aug 25 '15 at 06:54
  • How do I add the all third-party and create its namespace? Thanks – Jan Beeck Aug 31 '15 at 23:58
  • 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. – Sageth Sep 01 '15 at 06:16
  • 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();....} – Jan Beeck Sep 01 '15 at 20:35
  • Have you add it in your config file as a component? look the step 3 in the guida I've posted – Sageth Sep 02 '15 at 06:41
  • I created a component Graphic just like the latter link, however Where do I set the path of the external library? lib\libchart\classes\libchart.php. Thanks. – Jan Beeck Sep 02 '15 at 19:52
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

Yonas Alemayehu
  • 111
  • 2
  • 8
  • You mean its not necessary to set the path where the external library is located in the project? Thanks. – Jan Beeck Sep 02 '15 at 21:46
  • What is the difference in this new Yii2 version? https://walterebert.com/blog/using-external-php-libraries-with-the-yii-framework/ – Jan Beeck Sep 08 '15 at 23:41
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?

Community
  • 1
  • 1
Jan Beeck
  • 303
  • 8
  • 24
  • While this may answer the question, [it would be preferable](http://meta.stackoverflow.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – IKavanagh Nov 04 '15 at 18:51