Tag Archives: symfony2

Load bundle without composer.json in Symfony2.3

This is how I loaded a legacy budle without composer.json in a custom location for my Symfony2.3project with composer.

In one of my Symfony2.3 projects I needed to use an old external bundle. This legacy bundle (from Symfony2.0) hasn’t a composer.json file and in my case I can not add one.

Anyway, I wanted to use composer and its autoloader also for this bundle in order to keep things simple for me.

Let’s assume I wanted to add TwitterClientBundle and its namespace is: Razvan\TwitterClientBundle

For this I added in my composer.json the following:


After that I ran:

and the bundle was added in /path/to/yourproject/vendor/razvan/twitter-client-bundle/

eg:

After that I enabled the bundle in AppKernel.php

and this was all.

Disclaimer:

  • Maybe there are better solution, but I did not find them yet.
  • The bundle RazvanTwitterBundle doesn’t exist on Github.

P.S. Somebody else wrote a similar post in the past.

How to use AWS SDK for PHP 2 and Symfony Console

Few days ago I wanted to write a small PHP script to backup (upload) some files in Amazon S3. And because Amazon just released last month a new version of their SDK, AWS SDK for PHP 2 I decided to use this one.
I also choose to use Console component from Symfony2 and Composer to install all the dependencies.

In the root folder of my project I downloaded and installed composer, like this:

In composer.json file I added this dependencies:

and after this I ran:

After runing this command, you will see in the project folder, a directory called vendor where are installed all the dependencies.

After that I created on the same level with vendor a folder called src and inside this one another folder called Razvan.

inside the Razvan folder I put my BackupCommand.php

Below is a part of the code:

And now we have to call this command. For this, in the root folder of the project I created another small script called backup.php with this content:

Now, we can run it from the console, with:

Based on this skeleton, I’m working for a more complex syncronization script between Amazon S3 and a local folder. You can find the code on GitHub.

Disclaimer:

It is possible the tutorial above to not be very complete and accurate.

The code it’s not optimized at all (eg: if you have a big amount of files or very big files).

How to create a command (task) in Symfony 2

This tutorial is deprecated. It was written when Symfony2 was in beta. 

In Symfony 1.x a task is a command line tool to help perform tasks around the application. The same is in Symfony 2. Unfortunately in Symfony2 there is no tool to auto-generate some code for these. To create a Symfony2 Command you must to have or to create in your Bundle a folder named Command. Here, you must create a file named MytaskCommand.php with the following code:

 

N

ow, you can open a console and go to app folder. If you write:

./console

you will see something like

You can execute this Command with: ./console tudorica:razvan