Search code examples
phpcakephpcakephp-2.3

Can't setup Highchart


I am trying to use highchart in cakephp and have followed the below tutorial and also the stackoverflow post on the subject.

I still get a highchart not found.

I downloaded highchartPHP and placed all the 4 files in Vendor/HighchartsPHP

In the layout, I add the lines with the actual js files in webroot/js

echo $this->Html->script('jquery-1.9.1.min'); // Include jQuery library
echo $this->Html->script('highcharts'); // Include jQuery library

This is my code

<?php
App::import('Vendor', 'HighchartsPHP/Highchart');

class ChartsController extends AppController
{
    public function index() {        
        $chart = new Highchart();  /////////////////Error: Class 'Highchart' not found 
        $chart->chart = array(
            'renderTo' => 'container', // div ID where to render chart
            'type' => 'line'
        );

        $chart->series[0]->name = 'Tokyo';
        $chart->series[0]->data = array(7.0, 6.9, 9.5);
        $this->set( compact( 'chart' ) );
    }

In view file

<?php $chart->printScripts(); ?>

<script type="text/javascript">
    <?php echo $chart->render("chart");?>
</script>

I can't find any more instructions about cakePHP setup with highcharts so I am stuck and I get a highchart not found error. I still have something missing. What has confused me is that highchartPHP doesn't explain how you install it for a MVC version with cakephp.

How to setup highchart so it works in cakephp ?

I got from the download zip button link so it must be v3 https://github.com/ghunti/HighchartsPHP

also Error: Class 'Highchart' not found from the controller as I outlined above


Solution

  • That's what happens when people don't mention version numbers...

    ...a year later nobody knows what they were talking about anymore. The tutorial and the question are most probably about version 1.x.

    https://github.com/ghunti/HighchartsPHP/tree/v1.0

    So a quick fix would be to use v1, but I'm not sure if that's a very good idea as it's probably not maintained anymore.

    Namespaces and Composer

    Look at the source code of version 2.x and 3.x, they are now using namespaces, and so the class cannot be found when not pointing into that namespace properly.

    As mentioned on the projects homepage the library should be installed via composer, and with pretty much all those libraries using composer the generated autoloader needs to be used, but this isn't really the place here to explain how to use composer, this is already extensively covered all over the net.

    https://getcomposer.org/doc/00-intro.md

    Be sure to check out the CakePHP docs on how to use the composer autoloader with CakePHP too:

    [...]

    If you’re installing any other libraries with Composer, you’ll need to setup the autoloader, and work around an issue in Composer’s autoloader. In your Config/bootstrap.php file add the following:

    // Load Composer autoload.
    require APP . 'Vendor/autoload.php';
    
    // Remove and re-prepend CakePHP's autoloader as Composer thinks it is the
    // most important.
    // See: http://goo.gl/kKVJO7
    spl_autoload_unregister(array('App', 'load'));
    spl_autoload_register(array('App', 'load'), true, true);
    

    http://book.cakephp.org/.../advanced-installation.html#installing-cakephp-with-composer