Symfony2 KendoUI实现

This is more a question of how to best import/integrate a JS library to use within a Symfony2 application ... I just happen to be using KendoUI as the test library.

I've already generated a bundle that converts and maps my database - including all associations.

 $ php app/console generate:bundle namespace=DB/DBImportBundle

this includes some other cli statements for all the mapping and converting (with annotations) ... we can assume that is all working fine. I have generated CRUD for all the tables that I need.

I will now want to make the KendoUI library available to all controllers ... and my first inclination was to ...

$ php app/console generate:bundle namespace=Kendo/KendoBundle

Next step was to move the Kendo's css/js files into the KendoBundle/Resources/public folders ... and then to declare the namespace in all the controllers where I intended to use it ...

use Kendo\KendoBundle;

This still leaves the question of where the php wrappers that come with the library should be placed. The tutorials in the Kendo docs only walk you through a straightforward static implementation of the library.

My question is more general in terms of how properly to integrate different libraries into a Symfony2 application.

I put them in the lib folder on the main project path. Inside that lib folder I put a folder called Kendoui, which has the kendo files in it (like DataSourceResult.php, FileBrowser.php, ImageBrowser.php, Kendo (folder), and SchedulerDataSourceResult.php.

Then in your composer.json file, change autoload to look like below. Notice the classmap reference for lib, this will tell it to look there for the library.

"autoload": {
        "psr-4": {
            "": "src/"
        },
        "classmap": [
            "app/AppKernel.php",
            "app/AppCache.php",
            "lib"
        ]
    },

Next run composer update and/or composer dump-autoload so that it remakes the autoload files. Now you can reference KendoUI for something like a datepicker control like this.

/**
 * @Route("/homepage", name="home")
 */
public function home(Request $request)
{

    $datePicker = new \Kendo\UI\DatePicker('datepicker');

    return new Response($datePicker->render());

}

Word of caution, I did notice composer give a warning for me that said...

Warning: Ambiguous class resolution, "Kendo\Dataviz\UI\DiagramConnectionDefaultsEndCap" was found in both "/home/distribution/public_html/distribution.tech/lib/Kendoui/Kendo/Dataviz/UI/DiagramConnectionDefaultsEndCap.stroke.php" and "/home/distribution/public_html/distribution.tech/lib/Kendoui/Kendo/Dataviz/UI/DiagramConnectionDefaultsEndCap.php", the first will be used.

However everything still seemed to work fine, but if anyone knows how to resolve this warning let me know and I'll revise my answer.

It has come to my attention that there is another way to do this that might be better than my previous answer.

In the app folder, there should be a file called autoload.php

Put the kendo 'lib' folder in the vendors folder where it should be.

Then have your autoload.php look like below.

<?php

use Doctrine\Common\Annotations\AnnotationRegistry;
use Composer\Autoload\ClassLoader;

/**
 * @var ClassLoader $loader
 */
$loader = require __DIR__.'/../vendor/autoload.php';
$loader->add(
    'Kend_', __DIR__.'/../vendor/lib'
);
AnnotationRegistry::registerLoader([$loader, 'loadClass']);

return $loader;

This will load the classes that make up Kendo up in a much nicer and easier way and no composer.