Example algolia and Symfony - 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.

Related

How to use symfony2 twig extension without symfony2

I want to use some of the builtin symfony2 extensions(e.g:humanize,yaml_dump) for twig for a website not developed in symfony but using a twig engine.how can I do that?
The symfony/twig-bridge package provides the symfony-specific twig extensions.
These include i.e. the YamlExtension that provides the yaml_dump filter and the FormExtension that provides the humanize filter.
The extensions can be found in the Extension folder.
I strongly advise you to install the package via composer to get the package's dependencies automatically.
composer require symfony/twig-bridge:~2.3
Further composer will automatically register the classes in the autoloader (vendor/autoload.php) for you.
Now you just need to add the extensions to twig as described in the documentation.
$twig->addExtension(new \Symfony\Bridge\Twig\Extension\YamlExtension());
// ...
A complete example, with an extension class and a quick extension (a new filter) :
<?php
require_once("vendor/autoload.php");
$loader = new Twig_Loader_String();
$twig = new Twig_Environment($loader);
// here we add the extension class (taken from #nifr answer)
$twig->addExtension(new \Symfony\Bridge\Twig\Extension\YamlExtension());
// here we add a new filter quickly
$filter = new Twig_SimpleFilter('paragraph', function ($argument) {
return "<p>{$argument}</p>";
}, array('pre_escape' => 'html', 'is_safe' => array('html')));
$twig->addFilter($filter);
// demo
echo $twig->render('{{ "hello" | paragraph }}');

FOSUserBundle not found in appkernel

I am using symfony on Windows and I tried to configure FOSUserBundle as described in the official documentation.
I get this error when try to update the schema:
Class 'FOS\UserBundle\FOSUserBundle' not found in app/AppKernel.php line 20;
searched for the problem and find this solution:
adding this to autoload.php
$loader->registerNamespaces(array(
//all the rest
'FOS' => $vendor_dir . '/bundles',
));
but it returns another error which says
call to undefined method ...\ClassLoader::RegisterNamespace() in ...\autoload.php on line 13
can anybody plz tell me what should i do?:|
and this is my appkernel.php file:
<?php
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Config\Loader\LoaderInterface;
class AppKernel extends Kernel
{
public function registerBundles()
{
$bundles = array(
new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
new Symfony\Bundle\SecurityBundle\SecurityBundle(),
new Symfony\Bundle\TwigBundle\TwigBundle(),
new Symfony\Bundle\MonologBundle\MonologBundle(),
new Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle(),
new Symfony\Bundle\AsseticBundle\AsseticBundle(),
new Doctrine\Bundle\DoctrineBundle\DoctrineBundle(),
new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(),
new Sad\Bundle\WarehouseBundle\SadWarehouseBundle(),
new FOS\UserBundle\FOSUserBundle(),
);
if (in_array($this->getEnvironment(), array('dev', 'test'))) {
$bundles[] = new Acme\DemoBundle\AcmeDemoBundle();
$bundles[] = new Symfony\Bundle\WebProfilerBundle\WebProfilerBundle();
$bundles[] = new Sensio\Bundle\DistributionBundle\SensioDistributionBundle();
$bundles[] = new Sensio\Bundle\GeneratorBundle\SensioGeneratorBundle();
}
return $bundles;
}
public function registerContainerConfiguration(LoaderInterface $loader)
{
$loader->load(__DIR__.'/config/config_'.$this->getEnvironment().'.yml');
}
}
I had the same problem. I found out that the FOSUserBundle was not properly installed. You should delete your /vendor/friendsofsymfony/ directory and then update the bundle using:
php composer.phar update friendsofsymfony/user-bundle
It worked for me. I hope it helps someone else having the same issue.
Alright, there seems to be nothing wrong with your code.
Before we can get on the workarounds, let's try to reinstall your bundle, through the following steps:
Remove that $loader->registerNamespaces(...) thing you added to autoloader.php.
Run php composer.phar self-update to update composer.
Remove the line use FOS\UserBundle\FOSUserBundle(), from AppKernel.php.
Run php composer.phar update to update all your bundles.
Clear your cache, running php app/console cache:clear.
Add the line use FOS\UserBundle\FOSUserBundle(), to AppKernel.php again.
Those should do it. If you still can't use the bundle and you need the workaround (which I wouldn't advice), this is the way to go:
Open app/autoload.php. Right after $loader = require __DIR__ . '/../vendor/autoload.php. add the following:
//Loads FOSUserBundle
$loader->add('FOS', __DIR__.'/../vendor/friendsofsymfony/user-bundle/FOS');
Again, this should fix the issue and yet is not the correct way to do things. Your bundle should be working.
To fix this error in Symfony 3.x.
If composer can not be executed and you can only transfer the files by FTP, it's necessary to update the file:
vendor/composer
and maybe
vendor/symfony (if the version is updated)
The same solution applies to this error
Fatal error: Class 'FOS \ JsRoutingBundle \ FOSJsRoutingBundle' not
found

Symfony 2.1.7 and doctrine migrations bundle error

I am following this tutorial: http://tutorial.symblog.co.uk/docs/extending-the-model-blog-comments.html#doctrine-2-migrations
1) Installing Doctrine migrations bundle
1.1) - adding
"doctrine/migrations": "dev-master",
"doctrine/doctrine-migrations-bundle": "dev-master"
to composer.json
1.2) running
php composer.phar update
2) adding
new Doctrine\Bundle\MigrationsBundle\DoctrineMigrationsBundle(),
in AppKernel.php
3) Running
php app/console doctrine:migrations:diff
this should run the command and find the differences between the current entities and the database, yes?
But I get an error instead:
Fatal error: Class 'Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle' not found in D:\xampp\htdocs\symblog.dev\app\AppKernel.php on line 23
This is exactly the line of (2.)
Can you help me out? Any advice is welcome!
I think the bundle was renamed in the meanwhile. Try: (updated question):
// app/AppKernel.php
public function registerBundles()
{
$bundles = array(
//...
new Doctrine\Bundle\MigrationsBundle\DoctrineMigrationsBundle(),
);
}
See DoctrineMigrationsBundle documentation and DoctrineMigrationsBundle class.

How to install or integrate bundles in Symfony2

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'
...
));

How to use PHPExcel correctly with Symfony 2

I need to use PHPExcel with a Symfony2 project. Anyone know how to set up the project correctly to use the library? Should i put it in the vendor directory? What should be changed in the configuration files etc?
Actually, to do it right you need to follow next steps:
Edit your deps file and add dependency from the PHPExcel
[PHPExcel]
git=http://github.com/PHPOffice/PHPExcel.git
target=/phpexcel
version=origin/master
Run php bin/vendors install in order to install all missing dependencies (PHPExcel in our case)
Update prefixes section in app/autoload.php:
$loader->registerPrefixes(array(
// ...
'PHPExcel' => __DIR__.'/../vendor/phpexcel/Classes',
));
Done. Now, you can use it in your bundle's controller (code based on PHPExcel example from Tests/01simple-download-xls.php):
<?php
namespace Demo\MyBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use PHPExcel;
use PHPExcel_IOFactory;
class DemoController extends Controller
{
public function demoAction()
{
$response = new Response();
// Create new PHPExcel object
$objPHPExcel = new PHPExcel();
// Set document properties
$objPHPExcel->getProperties()->setCreator("Me")
->setLastModifiedBy("Someone")
->setTitle("My first demo")
->setSubject("Demo Document");
// Add some data
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', 'Hello')
->setCellValue('B2', 'world!')
->setCellValue('C1', 'Hello')
->setCellValue('D2', 'world!');
// Set active sheet index to the first sheet
$objPHPExcel->setActiveSheetIndex(0);
// Redirect output to a client’s web browser (Excel5)
$response->headers->set('Content-Type', 'application/vnd.ms-excel');
$response->headers->set('Content-Disposition', 'attachment;filename="demo.xls"');
$response->headers->set('Cache-Control', 'max-age=0');
$response->prepare();
$response->sendHeaders();
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit();
}
}
Copy the library to your vendors directory.
Configure autoloader in your bootstrap file:
$loader->registerPrefixes(array(
// Swift, Twig etc.
'PHPExcel' => __DIR__ . '/../vendor/phpexcel/lib/PHPExcel'
));
That's all.
actually the best solution is to use https://github.com/liuggio/ExcelBundle.
I tried to use #Crozin's solution but I was still getting an error about IOFactory::createWriter.
Hope this helps,
Simone
As of Symfony 2.3, you can now do this:
...
"require": {
...
"phpoffice/phpexcel": "dev-master"
...
},
...
Then just run composer update and dependencies will resolve automatically.
Or you can do composer require phpoffice/phpexcel:dev-master if you don't want to mess with the composer.json file.
If you are using composer to manage your project, you can just change the composer.json file:
"autoload": {
"psr-4": {
"": "src/",
"": "vendor/phpoffice/phpexcel/Classes/"
},
"classmap": [
"app/AppKernel.php",
"app/AppCache.php"
]
},
Then add
use PHPExcel;
use PHPExcel_IOFactory;
to your controller file, and you can use the PHPExcel like this:
$objPHPExcel = new PHPExcel();
Hope it helps.
With composer (since Symfony2.1) it's really easy, you only have to modify the composer.json.
You don't need to register the namespace anymore!
Only two things, to notice:
refer to github tags, I only found a soltion with the package type
when changing something in the composer.json related to the class autoloading stuff, you have to remove the whole directory in the vendor dir
Here is the related link: use PHPExcel with composer and Symfony2.2

Resources