How to install or integrate bundles in Symfony2 - symfony

i have downloaded a sonata admin bundle, and have placed in /var/www/Symfony/vendor/symfony/src/Symfony/Bundle, and have made an entry in AppKernel.php as $bundles = array( ... new Symfony\Bundle\SonataAdminBundle\SonataAdminBundle(),), but throwing an error as
Fatal error: Class
'Symfony\Bundle\SonataAdminBundle\SonataAdminBundle' not found in
/var/www/Symfony/app/AppKernel.php on line 21 Call Stack: 0.0001
326332 1. {main}() /var/www/Symfony/web/app_dev.php:0 0.0122 1121592
2. Symfony\Component\HttpKernel\Kernel->handle()
please help me as i am very new to symfony 2. As a whole please give a link or detail like how to install/configure any bundle that is downloaded.
Thanks
Ravi.M

You need to move the bundle to
/var/www/Symfony/vendor/bundles
Then in AppKernel.php add
new Sonata\AdminBundle\SonataAdminBundle(),
in your $bundles array.
In autoload.php add
'Sonata' => __DIR__.'/../vendor/bundles',
to the $loader->registerNamespaces array

First off, SonataAdminBundle lives in Sonata namespace, not Symfony. So you'll have to rewrite the instantiation in app/AppKernel.php to:
new Sonata\AdminBundle\SonataAdminBundle()
You also have to register namespace in app/autoload.php:
$loader->registerNamespaces(array(
...
'Sonata' => __DIR__.'/path/to/parent/of/Sonata/folder'
...
));

Related

Prestashop 1.7 Override CmsController

In prestashop v1.7.4.2, with VisualComposer installed,
after I bought another module named: FormBuilder, can't enable and cause error like this:
Cannot enable module gformbuilderpro. Unable to install override: The
method initContent in the class CmsController is already overridden by
the module jscomposer version 4.4.7 at 2018-08-25 18:20:10 .
any knowledge or instruction to solve this conflict?
screenshot
You have 2 override:
1- jscomposer => override => cmsController => initContent
2- gformbuilderpro => override => cmsController => initContent
Second method has conflict with first method
Solution:
1- remove initContent method from gformbuilderpro module
2- merge content of both initContent methods on this path :
root/override/controllers/front/CmsController.php
3- install gformbuilderpro module and enjoy PrestaShop
!!!! It's better to put new merged initContent method on jscomposer for future changes

Example algolia and Symfony

I use this documentation for install algolia:
https://www.algolia.com/doc/api-client/symfony/setup/
this configuration is already done:
composer require algolia/algolia-search-bundle
And
$bundles = array(
new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
new Doctrine\Bundle\DoctrineBundle\DoctrineBundle(),
new Algolia\AlgoliaSearchBundle\AlgoliaAlgoliaSearchBundle(),
);
configuration.yml:
algolia:
application_id: YOUR_APP_ID
api_key: YOUR_API_KEY
but I don't understand , I need a example for implementation in my project.
Please your help
1-) you should install algolia-search-bundle with Composer;
composer require algolia/algolia-search-bundle
2-) add line this -> Algolia\AlgoliaSearchBundle\AlgoliaAlgoliaSearchBundle() at app/AppKernel.php file into $bundles array.
3-)you should add your api keys at app/config/config.yml file;
algolia:
application_id: YOUR_APP_ID
api_key: YOUR_API_KEY
That's enough.

Symfony 1.2 to 2.3 migration

I've got a pretty big Symfony 1.2 project to migrate.
First, I modified my .htaccess so I can have some pages handled by Symfony 2.
What I'd like to do, to make the migration smoother, is to be able to render some SF2 action/templates/methods/... inside SF1.
I added the autoloader to the SF1 app, so I can access to twig rendering methods and other stuff.
But how can I call a SF2 action ?
For example, if I want to migrate only the footer first, I would also need some php methods, not only rendering. That was previously in SF1 component, where should it be now ?
If you've got any suggestion about the way of migrating, don't hesitate !
EDIT 1 :
Apparently, the only way to do something like that is to render a full twig template, and/or in this template call some other partial twig templates with render(url, params).
Here is my SF1 code to be able to render twig templates :
public static function getTwig()
{
require_once __DIR__.'SF2_PATH/vendor/twig/extensions/lib/Twig/Extensions/Autoloader.php';
Twig_Autoloader::register();
$loader = new Twig_Loader_Filesystem( __DIR__.'SF2_PATH/sf2/src/VENDOR/BUNDLE/');
$twig = new Twig_Environment($loader, array(
'cache' => __DIR__.'SF2_PATH/sf2/app/cache/dev/twig',
));
return $twig;
}
And so :
$twig->loadTemplate('header.html.twig');
EDIT 2 :
That doesn't seem to work, if in a twig template I try to render an other one with {{render(controller('BUNDLE:CONTROLER:ACTION', {})) }} for example Twig_Error : The function "controller" does not exist. And if I try to render the url Unknown tag name "render".
I guess Symfony 2 twig functionalities are not loaded, how can I do that ?
EDIT 3 :
Ok, now I can do it, but I've got the following message...
Twig_Error_Runtime An exception has been thrown during the rendering
of a template ("Rendering a fragment can only be done when handling a
master Request.") in ...
EDIT : I solved it !
Here is my full bootstrap method to render a Twig template and be able to use some Symfony 2 functionalities, in Symfony 1.
$loader = require_once __DIR__.'/../../../sf2/app/bootstrap.php.cache';
Debug::enable();
require_once __DIR__.'/../../../sf2/app/AppKernel.php';
$kernel = new AppKernel('dev', true);
$kernel->loadClassCache();
Request::enableHttpMethodParameterOverride();
$request = Request::createFromGlobals();
$kernel->boot();
$kernel->getContainer()->enterScope('request');
$kernel->getContainer()->set('request', new \Symfony\Component\HttpFoundation\Request(), 'request');
$this->container = $kernel->getContainer()->get('twig');

PHP Fatal error: Class 'Application\Sonata\MediaBundle\ApplicationSonataMediaBundle' not found in /var/www/znata.com/app/AppKernel.php on line 47

i followed this doc to install SonataMediaBundle but i got this error:
PHP Fatal error: Class 'Application\Sonata\MediaBundle\ApplicationSonataMediaBundle' not found in /var/www/znata.com/app/AppKernel.php on line 47
After using the sonata command t generate the app:
php app/console sonata:easy-extends:generate SonataMediaBundle
new directory was generated under:
apps/Application/Sonata/MediaBundle
everything was done but when i registred the generated application in my AppKernel.php i got that error.
public function registerbundles()
{
return array(
...
new Application\Sonata\MediaBundle\ApplicationSonataMediaBundle(),
...
);
}
Have you any idea how to fix this issue ?
By default project root directory is not in the autoload path, only "src" dir.
You can use
php app/console sonata:easy-extends:generate --dest=src SonataMediaBundle
to generate bundle in the src or simple copy it to the src.
After debug this problem, i found that the namspace Application is not registred.
In SF2.0, the documentation said that we should register this namespace like:
<?php
$loader->registerNamespaces(array(
...
'Application' => __DIR__,
'Imagine' => __DIR__.'/../vendor/imagine/lib',
'Gaufrette' => __DIR__.'/../vendor/gaufrette/src',
'Buzz' => __DIR__.'/../vendor/buzz/lib',
...
));
but in SF2.1 they did talked about this.
So i registred the namespace Application in autoload.php and it works fine.
so, the autoload.php look like this:
<?php
// file: app/autoload.php
use Doctrine\Common\Annotations\AnnotationRegistry;
$loader = require __DIR__.'/../vendor/autoload.php';
//custom for Application
$loader->add("Application", __DIR__);
// intl
if (!function_exists('intl_get_error_code')) {
require_once __DIR__.'/../vendor/symfony/symfony/src/Symfony/Component/Locale/Resources/stubs/functions.php';
$loader->add('', __DIR__.'/../vendor/symfony/symfony/src/Symfony/Component/Locale/Resources/stubs');
}
AnnotationRegistry::registerLoader(array($loader, 'loadClass'));
return $loader;
With this new config everything is fine.But in SF2.0, they talked also about "Imagine", "Guffrette" and "Buzz" namespaces. So perhapes, when using them, we should register them also like Application namespace.
I hope that this helps you.
Using composer I did this in composer.json:
"autoload": {
"psr-0": {
"": "src/",
"Application": "app/"
}
},
I added the mapping "Application": "app/".
And then run
composer update
This generated extra autoloading needed.
inside your composer.json file, have:
"autoload": {
"psr-4": {
"AppBundle\\": "src/AppBundle",
"Application\\": "src/Application"
},
}
do a simple:
composer dump-autoload
to re-generate the autoload files.
new Application\Sonata\MediaBundle\MediaBundle(),
or
new Application\Sonata\MediaBundle\SonataMediaBundle(),
As skonsoft mentioned, you need to load the namespaces in autoload.php. I had the same issue with FOQ.Elastica and resolved it by adding the following.
$loader->add('FOQ', __DIR__.'/../vendor/bundles');
You can also use your app namespace prefix so the package falls under your namespace
bin/console sonata:easy-extends:generate --dest=src SonataMediaBundle --namespace_prefix=App

Symfony2: class MapClassLoader not found

I'm trying to use MapClassLoader in autoload.php but for some reason I keep getting errors saying
Class 'Symfony\Component\ClassLoader\MapClassLoader' not found in ...\autoload.php
autoload.php:
<?php
use Symfony\Component\ClassLoader\UniversalClassLoader;
use Symfony\Component\ClassLoader\MapClassLoader;
use Doctrine\Common\Annotations\AnnotationRegistry;
$loader = new UniversalClassLoader();
$loader->registerNamespaces(array(
//some values
));
$mapLoader = new MapClassLoader(array(
//some values
));
$mapLoader->register();
I double checked and MapClassLoader.php does exist in Symfony\Component\ClassLoader
Any idea why is it happening? :/
autoload.php is a file that configures autoloading for classes so autoloading isn't available in it and you need to include any files manually:
require_once __DIR__.'/../vendor/symfony/src/Symfony/ClassLoader/MapClassLoader.php';
Why is UniversalClassLoader available without require? Because symfony uses bootstrap file for system files to reduce file loading overhead.

Resources