Using the object Silex/Application into a Symfony2 project - symfony

I need to use a third party API into a Symfony2 app, which is using composer to manage dependencies.
This API is built using Silex, so I need to use a Silex/Application object.
I have manage to get silex by adding
"silex/silex": "1.0.*"
into composer.json, but when I specify
use Silex/Application
in the controller and
$app = new Silex\Application();
in the action where I want to manage the third party API, the response is:
Fatal error: Class 'MyApp\MyBundle\MyController\Silex\Application' not found in path\to\my\controller\MyController.php on line 130
It seems like the Silex namespace haven't been loaded.
Any idea about how can I manage to use Silex into Symfony2?

If you exported it as use Silex\Application; then $app = new Application();
or use Silex\Application as SilexApp then $app = new SilexApp();

Related

Symfony 2 split application to more bundles

I want to split my application to two bundles.
Back-end bundle which will be something like CMS admin(reusable to another projects), and Front-end bundle.
I moved whole backend application including controllers, templates and model to new bundle, but I have a problem with 3rd-party bundles on which my project is dependent.
For example AsseticBundle. When I add assetic configuration to BackendBundle/config.yml i recieve a exception.
There is no extension able to load the configuration for "assetic"
Is there some good tutorial showing how to split application to two cooperating bundles with one config and both dependent to 3rd-party libraries?
Thanks for any advice.
If I understand you correctly, you're trying to add 3rd party bundle configuration to your own bundle config.
This can be achieved only in app/config/config.yml, which is loaded by default by Symfony. It's the only place where Symfony looks.
What you can do, is prepend configuration via own extension. That can be placed in your bundle:
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
class BackendBundleExtension extends Extension implements PrependExtensionInterface
{
public function prepend(ContainerBuilder $container)
{
$container->prependExtensionConfig('assetic', array(
'key' => 'value'
));
}
}
Let me know, if this is sufficient explanation.

Integrating Sylius packages into Laravel project

I am attempting to use some Sylius packages in a Laravel project I'm building. I have added the packages to my project with composer, and the classes seem to be autoloading fine.
Now I want to do something with it. Let's take a simple example from the Sylius docs:
public function myAction()
{
$repository = $this->container->get('sylius.repository.address');
$manager = $this->container->get('sylius.manager.address'); // Alias to the appropriate doctrine manager service.
$address = $repository->createNew();
$address
->setFirstname('John')
->setLastname('Doe')
;
$manager->persist($address);
$manager->flush(); // Save changes in database.
}
I would like to recreate something like this in Laravel. I've created a simple test route to attempt this, but my problems are:
How do I get a repository?
How do I get a Doctrine manager service?
So I also included the laravel-doctrine package in an attempt to get a manager service, and then I just manually created a Product object, so that now I have:
Route::get('test', function() {
$entityManager = new Doctrine;
$product = new \Sylius\Component\Core\Model\Product;
$product->setName('Bla');
Doctrine::persist($product);
Doctrine::flush();
dd($product);
});
This creates an error: Class "Sylius\Component\Core\Model\Product" sub class of "Sylius\Component\Product\Model\Product" is not a valid entity or mapped super class.
I'm not sure where to go next. My understanding of Doctrine is limited, but my understanding was that the Sylius components are decoupled from Symfony, and that I could just use them. But it seems that the annotations (or xml definitions) don't exist for Doctrine to operate properly.
Any direction would be appreciated!
Laravel and symfony are completely different. You can't use a symfony bundle in a laravel project. If you want to use Sylius, you have to use the Symfony framework. If you want to use laravel, you have to search for another ecommerce platform.

Silex + FOQElasticaBundle

My goal is to link the FOQElasticBundle with my Silex website.
The problem is that the documentationn of FOQElasticBundle states that I only have to put some basic stuff in my config.yml file.
See: https://github.com/Exercise/FOQElasticaBundle#declare-a-client
So far I can't find a config.yml file anywhere and I don't know if I have to create it and if so where to put it.
Did I made the wrong choice making my website with Silex instead of Symfony itself?
Or is it possible to load the FOQElasticBundle?
You cant use Symfony Bundles in Silex directly.
Silex integrates third party libraries via Service Providers, which act like some sort of adapters for third party libs.
If you want the convenience of bundles i suggest you better use Symfony.
Otherwise you could try to write a Service Provider for the Elastica library yourself
or integrate it directly as its done here:
https://github.com/4devs/demo-silex/blob/master/web/index.php
$app['elastica.host'] = "localhost";
$app['elastica.port'] = 9200;
$app['elastica'] = function ($app) {
return new \Elastica\Client(array(
'host' => $app['elastica.host'],
'port' => $app['elastica.port']
));
};
Unfortunatly there is no Elastica Service Provider listed here:
https://github.com/silexphp/Silex/wiki/Third-Party-ServiceProviders

Where is IHttpControllerFactory?

Microsoft website says there is a IHttpControllerFactory which can be used to generate controllers in custom ways.
However, the System.Web.Http DLL referenced from Visual Studio:
packages\Microsoft.AspNet.WebApi.Core.4.0.20710.0\lib\net40\System.Web.Http.dll
Doesn't have that interface:
So where is the interface now?
It's gone and replaced by a dependency resolver or more specifically the IDependencyResolver interface. Once you write your own dependency resolve you could plug it into the Web API:
config.DependencyResolver = new MyDependencyResolver();
Cannot find it as well.
Anyway you can use IHttpControllerActivator to create your custom controller factories in Web API.

How can I access Symfony2 services from an external legacy system?

We are slowly migrating our current system to Symfony2, but the majority of the codebase is still in an inhouse framework. I want to leverage some functionality built in Symfony2 from within classes in the old legacy framework. Is there an easy way to access a Symfony2 service from outside of the Symfony framework?
class MyOldClass extends SomethingOld
{
public function getSomethingViaSymfony()
{
$service = new SomeSymfonyService();
$results = $service->getResults();
}
}
My assumption is that this would be a failure, for the dependencies wouldn't be injected.
you would need to initialize symfony without dispatching any actions. this basicly means taking the code from the symfony front controller file in web/index.php, modifying it a bit and paste it to some initialization file of your legacy app.
// legacy/init.php
require_once 'PATH_TO_SYMFONY/app/bootstrap.php.cache';
require_once 'PATH_TO_SYMFONY/app/AppKernel.php';
$kernel = new AppKernel('dev', true);
$kernel->loadClassCache();
$GLOBALS['service_container'] = $kernel->getContainer();
note that this code is not tested but i'm pretty sure it'll work because symfony is great;)
also you could think about a different strategy in which you embed the legacy action into symfony and not the other way around.
you would have to implement a legacy controller and give it a catch all route at the end of the routing definition. this controller can initialize the legacy application and dispatch actions to it. afterwards you can successively define new routes at the top of the routing file and dispatch them with symfony.
this strategy is imho much better because you can leave the legacy application almost untouched and kill it piece by piece.

Resources