How to use Pagerfanta with Symfony2 and Doctrine2

Posted November 27th, 2011 in Tehnikalije by Metod

So let’s assume you want to use a pager with your Symfony2 application. If you are like me, you type it in google and get a link to KnpPaginatorBundle. Start using it and everything goes well, until you hit an error telling you something is wrong. That something is something in the lines of:

  1. SELECT SUM(S.STH) AS something FROM

Which Zend_Paginator does not support! Here is where Pagerfanta comes into the picture.

Download the PagerfantaBundle or get it through git. Also get Pagerfanta. I decided to break PagerfantaBundle into 2 more folders. So the structure of the bundle is now:

  1. vendor\bundles\WhiteOctober\PagerfantaBundle\…

For Pagerfanta, the structure is:

  1. vendor\pagerfanta\src\…

Now, add both to autoload:

  1. // app/autoload.php
  2. $loader->registerNamespaces(array(
  3.     // …
  4.     ‘WhiteOctober’     => __DIR__.‘/../vendor/bundles’,
  5.     ‘Pagerfanta’       => __DIR__.‘/../vendor/pagerfanta/src’,
  6.     // …
  7. ));

Register the bundle with your application kernel:

  1. // app/AppKernel.php
  2. public function registerBundles()
  3. {
  4.     return array(
  5.         // …
  6.         new WhiteOctober\PagerfantaBundle\WhiteOctoberPagerfantaBundle(),
  7.         // …
  8.     );
  9. }

Now what the official documentation does not tell you is what namespaces to include to use it.

For Doctrine2 the example Controller looks like this:

In the view (I assume you are using Twig) you just do:

  1. {{ pagerfanta(examples) }}

And you can be done!

However, there is much more to Pagerfanta. You can pass in many configuration options. Here is where so called Global Variables come in handy. You can define them in app/config/config.yml:

  1. twig:
  2.     globals:
  3.         pagerfanta_opts:
  4.             previous_message: ‘«’
  5.             next_message:     ‘»’
  6.             dots_message:     ‘ … ‘

Now you can access the ‘pagerfanta_opts’ in every template:

  1. {{ pagerfanta(examples, ‘default’, pagerfanta_opts) }}

Where ‘default’ is the name of the Pagerfanta view.

For those who want to learn more about it, read the documentation on GitHub for Pagerfanta.

Login event listener in Symfony2

Posted July 11th, 2011 in Tehnikalije by Metod

So you have a Symfony2 project under construction. Login works. Now you want to execute some code right after the user successfully logs in.

The solution: Make a custom event listener.

An event listener is actually a service with a proper tag associated with it. So first step is to register the listener. The configuration file shown corresponds to the listener in file Acme/UserBundle/Listener/LoginListener.php. You can read more about services here.

In services.yml:

Let’s go through the configuration.

class defines the listener class.

With arguments we inject services that this listener depends on. In this case the security.context service for retrieving the User object and doctrine service for EntityManager.

tags is important. Every listener must be tagged with kernel.event_listener and must specify it’s event. For successful login that event is security.interactive_login. You can also specify the optional method tag.

Ok, our listener is registered. Let’s create it.

The above code should be pretty self-explanatory. The only required method is onSecurityInteractiveLogin(Event $event). You do not need the other stuff, but you probably will if you want to modify the User object, or something else in the DB.

Multiple PHP versions with Apache 2, FastCGI, PHPFarm on Ubuntu

Posted December 2nd, 2010 in Tehnikalije by Metod

This tutorial is a part of the “Setting up your local development environment” series. It is based on this article while keeping in mind that in previous apache tutorial we compiled apache from source. So I’m definitely not taking all the credit for it. But there are some modifications.

Starting with PHPFarm, we need Git installed in order to check it out. If you don’t have it installed:

  1. sudo apt-get install git-core

Now create a directory in which PHP versions will reside. The directory of my liking is /usr/local/php, so I will use that. You can of course change that.

  1. cd /usr/local

Clone the phpfarm repository.

  1. git clone git://git.code.sf.net/p/phpfarm/code php

Now we have to edit the configuration options which phpfarm will use to compile php.

  1. cd php/src

We can use more general configuration options or very strict ones. Default configuration options.sh can be overridden by creating a file called custom-options.sh or a bit more specific like custom-options-5.2.sh or very strict like custom-options-5.3.9.sh. Since the configurations for 5.2 and 5.3 are different and I like to be a strict person, I will create two configurations:

  • custom-options-5.2.17.sh
  • custom-options-5.3.9.sh

custom-options-5.2.17.sh contents (these config values were also mentioned in previous tutorial):

  1. configoptions="
  2.         –enable-cli \
  3.         –with-pear \
  4.         –with-openssl=/usr \
  5.         –with-iconv \
  6.         –with-curl \
  7.         –with-mysql \
  8.         –with-mysqli \
  9.         –enable-mbstring \
  10.         –enable-exif \
  11.         –with-jpeg-dir=/usr \
  12.         –with-zlib \
  13.         –with-zlib-dir \
  14.         –with-png-dir=/usr \
  15.         –with-gd \
  16.         –with-gettext \
  17.         –enable-gd-native-ttf \
  18.         –with-mhash \
  19.         –enable-ftp \
  20.         –with-pspell \
  21.         –with-mcrypt \
  22.         –enable-bcmath \
  23.         –with-mime-magic \
  24.         –with-pdo-mysql \
  25.         –enable-sockets \
  26.         –enable-soap \
  27.        –enable-calendar \
  28.         –enable-fastcgi \
  29.         –enable-force-cgi-redirect \
  30. "

custom-options-5.3.9.sh contents:

  1. configoptions="
  2.         –with-mysql=mysqlnd \
  3.         –with-mysqli=mysqlnd \
  4.         –with-pdo-mysql=mysqlnd \
  5.         –enable-cli \
  6.         –with-pear \
  7.         –with-openssl=/usr \
  8.         –with-iconv \
  9.         –with-curl \
  10.         –enable-mbstring \
  11.         –enable-exif \
  12.         –with-zlib \
  13.         –with-zlib-dir \
  14.         –with-gd \
  15.         –with-gettext \
  16.         –enable-gd-native-ttf \
  17.         –with-mhash \
  18.         –enable-ftp \
  19.         –with-pspell \
  20.         –with-mcrypt \
  21.         –enable-bcmath \
  22.         –enable-sockets \
  23.         –enable-soap \
  24.         –enable-calendar \
  25.         –with-png-dir=/usr \
  26.         –with-jpeg-dir=/usr
  27. "

For PHP 5.3 we don’t need the cgi parameters, since these are already enabled by default.

On Ubuntu 11.04, the configuration might fail with error messages about libjpeg and/or libpng. What you can do is the following:

  1. sudo apt-get install libjpeg8-dev

Install the libjpeg8-dev package. This will solve the libjpeg error message.

  1. sudo ln -s /usr/lib/x86_64-linux-gnu/libpng.so /usr/lib/libpng.so

Create a symbolic link to the libpng.so in the /usr/lib directory. This will solve the libpng error message.

Next step – compiling PHP.

  1. sudo ./compile.sh 5.2.17
  2. sudo ./compile.sh 5.3.9

Where the number after the ./compile.sh is the PHP version you wish to compile.

Now that we have compiled PHP, we can move on to set up apache. If you followed the apache tutorial, first of we need to recompile it.

  1. ./configure \
  2.         –prefix=/usr/local/apache2.2.22 \
  3.         –with-included-apr \
  4.         –enable-mods-shared=all \
  5.         –enable-so \
  6.         –enable-mod-rewrite \
  7.         –enable-suexec \
  8.         –with-suexec-caller=daemon

These are just my configuration options. The options you have to pay attention to for this topic are:

  • –enable-so
  • –enable-suexec
  • –enable-suexec-caller=daemon – You can change this according to which user your apache runs under.
  1. make
  2. sudo make install

Apache should be up and running. Next stop – FastCGI module.

Download and extract the latest (current) source from http://www.fastcgi.com/dist/.

  1. tar xzf mod_fastcgi-current.tar.gz
  2. cd mod_fastcgi-2.4.6

Next thing to do is rename Makefile.AP2 to Makefile, so we can compile it for apache 2.

  1. cp Makefile.AP2 Makefile

Compile and install the module.

  1. make top_dir=/usr/local/apache2.2.22
  2. sudo make install top_dir=/usr/local/apache2.2.22

The top_dir parameter is optional. If you compiled apache to the more usual location like /usr/local/apache2, you do not need to set this option.

Now that we have mod_fastcgi module compiled, we can load it as a shared object. Add the following line to your httpd.conf:

  1. LoadModule fastcgi_module modules/mod_fastcgi.so

If you had previously ran PHP as an apache module, you must disable it. You can only run PHP either as CGI or an apache module. So in httpd.conf, comment out this line:

  1. # LoadModule php5_module modules/libphp5.so

Create a .conf file (you can call it what you want) – i.e. php-fcgi.conf and put the following content inside:

  1. <IfModule mod_fastcgi>
  2.     FastCgiWrapper /usr/lib/apache2.2.22/bin/suexec
  3.     FastCgiConfig -idle-timeout 110 -killInterval 120 -pass-header
  4.         HTTP_AUTHORIZATION -autoUpdate
  5.  
  6.     ScriptAlias /php-fcgi/ /var/www/cgi-bin/
  7. </IfModule>

And include it in your main httpd.conf:

  1. # Include FastCGI configuration
  2. Include conf/php-fcgi.conf

Now create a directory for CGI scripts.

  1. sudo mkdir /var/www/cgi-bin

And create scripts.

  1. sudo nano /var/www/cgi-bin/php-cgi-5.2.17
  2. sudo nano /var/www/cgi-bin/php-cgi-5.3.9

And put something like the following in. Just change the version of PHP.

  1. #!/bin/sh
  2. version="5.3.9"
  3.  
  4. PHPRC=/usr/local/php/phpfarm/inst/php-${version}/lib/php.ini
  5. export PHPRC
  6.  
  7. PHP_FCGI_CHILDREN=3
  8. export PHP_FCGI_CHILDREN
  9.  
  10. PHP_FCGI_MAX_REQUESTS=5000
  11. export PHP_FCGI_MAX_REQUESTS
  12.  
  13. # which php-cgi binary to execute
  14. exec /usr/local/php/phpfarm/inst/php-${version}/bin/php-cgi

These scripts should be executable. So we set permissions to 0755.

  1. cd /var/www/cgi-bin
  2. sudo chmod +x *

Now we configure apache virtualhosts. Each virtualhost will have it’s own subdomain and PHP version.

  1. NameVirtualHost localhost.53:80
  2. <VirtualHost localhost.53:80>
  3.   ServerName localhost.53
  4.   DocumentRoot /var/www
  5.   <Directory "/var/www">
  6.     AddHandler php-cgi .php
  7.     Action php-cgi /php-fcgi/php-cgi-5.3.9
  8.   </Directory>
  9. </VirtualHost>

Do the same for all versions of PHP.

We are almost there. Since these subdomains (asumingly) do not exist yet, we must point them to your localhost IP.

  1. sudo nano /etc/hosts

Just add your subdomains after the localhost definition. It should look something like this:

  1. 127.0.0.1 localhost localhost.52 localhost.53

Everything is set, so just restart the apache.

  1. sudo /usr/local/apache2.2.22/bin/apachectl -k restart

Put a file phpinfo.php in /var/www directory with the following line:

And test it. If everything went well, you should see different PHP versions on different subdomains.

  • http://localhost.52/phpinfo.php – PHP 5.2.17
  • http://localhost.53/phpinfo.php – PHP 5.3.9

Enjoy!

Post updated on February 1st, 2012th to reflect latest updates.

Doctrine connection error – symfony 1.4 – php 5.3

Posted November 20th, 2010 in Tehnikalije by Metod

I just got in the sandbox with symfony 1.4 & php 5.3.3. In the beginning everything went well, untill I tried to pull some data from a database. I got this error:

  1. PDO Connection Error: SQLSTATE[HY000] [2002] No such file or directory

Error log said:

  1. PHP Warning:  PDO::__construct() [<a href='pdo.--construct'>pdo.--construct</a>]: [2002] No such file or directory (trying to connect via unix:///tmp/mysql.sock)

It made it clear that mysql.sock was missing or I had some configuration problems. Turns out my php.ini file had to be changed a bit.

  1. pdo_mysql.default_socket=/var/run/mysqld/mysqld.sock

Solution?
Find out where mysqld.sock or mysql.sock is residing and change the default value in your php.ini. That should solve the problem.

Neat PHP trick for alternate background display

Posted September 27th, 2010 in Tehnikalije by Metod

Many is the time we have to use alternate background for displaying some data. The usual solution to this problem would look something like:

  1. <?php
  2.     $i = 0;
  3.    
  4.     foreach($somedata as $data) {
  5.         if($i % 2 == 0)
  6.             // alternate
  7.         else
  8.             // normal
  9.  
  10.         $i++;
  11.     }
  12. ?>

Which in my opinion is somewhat a dirty solution. So I did a little research and found a bit neater solution:

  1. <?php
  2.     $even = 0;
  3.    
  4.     foreach($somedata as $data) {
  5.         if($even)
  6.             // alternate
  7.         else
  8.             // normal
  9.        
  10.         $even = (int)($even xor 1);
  11.     }
  12. ?>

It might even be a bit faster but I have not done any benchmarking.

Setting up your local development environment – PHP

Posted July 3rd, 2010 in Tehnikalije by Metod

Last time we went through steps needed for setting up apache. Now we will set up PHP. I encourage you to read the article on multiple PHP versions, which takes use of PHPFarm that makes things a lot easier. Plus you get to have multiple PHP versions installed.

Download and prepare

First few steps are practically the same as for the apache. Download the source and check md5.

  1. md5sum php-5.3.9.tar.gz | grep ‘c79e374c61423beb64a69da1eb5526b7′

Extract the archive.

  1. tar xzf php-5.3.9.tar.gz

Go into the extracted directory.

  1. cd php-5.3.9

Configure for compiling

  1. ./configure \
  2. –prefix=/usr/local/php5.3.9 \
  3. –with-apxs2=/usr/local/apache2.2.22/bin/apxs \
  4. –with-config-file-path=/usr/local/php5.3.9/conf \
  5. –with-mysql=/usr/bin/mysql_config \
  6. –with-mysqli=mysqlnd \
  7. –with-pdo-mysql \
  8. –enable-cli \
  9. –with-pear \
  10. –with-openssl=/usr \
  11. –with-iconv \
  12. –with-curl \
  13. –enable-mbstring \
  14. –enable-exif \
  15. –with-zlib \
  16. –with-zlib-dir \
  17. –with-gd \
  18. –with-gettext \
  19. –enable-gd-native-ttf \
  20. –with-mhash \
  21. –enable-ftp \
  22. –with-pspell \
  23. –with-mcrypt \
  24. –enable-bcmath \
  25. –enable-sockets \
  26. –enable-soap \
  27. –enable-calendar \
  28. –with-png-dir=/usr \
  29. –with-jpeg-dir=/usr

You don’t need all of these extras, they just might be useful to you. Some of the more important ones are:

  • –prefix=… sets the location of the PHP installation.
  • –with-apxs2=… sets the location of the apxs binary of the apache. Adjust accordingly.
  • –with-config-file-path=… sets the location of the php.ini configuration file. I just set it so it is more clear where the .ini file will be.
  • –enable-cli whether or not you want to use php from the command line.

You can read more about the configuration options in the PHP manual.

On Ubuntu 11.04, the configuration might fail with error messages about libjpeg and/or libpng. What you can do is the following:

  1. sudo apt-get install libjpeg8-dev

Install the libjpeg8-dev package. This will solve the libjpeg error message.

  1. sudo ln -s /usr/lib/x86_64-linux-gnu/libpng.so /usr/lib/libpng.so

Create a symbolic link to the libpng.so in the /usr/lib directory. This will solve the libpng error message.

Compile

  1. make

If this error occures while doing “make”:

  1. /usr/bin/ld: cannot find -lltdl collect2: ld returned 1 exit  status

Just install the libltdl-dev package. Should be something similar for other linux distributions.

  1. sudo apt-get install libltdl-dev

It is a good practice to run tests and report any possible bugs by running this command. It however is not mandatory to do it.

  1. make test

This might take a while. After tests are through, there might be a fail or two, so you can decide if you want to report those or not.

Let’s go on and install it. This command must be run as super user.

  1. sudo make install

Configure

Now we have to copy the php.ini to the appropriate directory.

  1. sudo cp php.ini-recommended /usr/local/php5.3.9/conf/php.ini

Next, we configure apache to handle our PHP scripts.

  1. sudo gedit /usr/local/apache2.2.22/conf/httpd.conf

Find a line which starts with AddType and add this below.

  1. AddType application/x-httpd-php .php

Than in line which starts with DirectoryIndex add this.

  1. index.php

Save the file and restart apache.

  1. sudo /usr/local/apache2.2.22/bin/apachectl -k restart

Test

Go to your documents root directory. In previous tutorial we used /var/www directory for this.

In this directory create a file named test.php and put this in it:

  1. <?php phpinfo(); ?>

Now go to http://localhost/test.php

If you see a lot of data about your PHP installation, you have succeeded.

Enjoy!

Post updated on February 1st, 2012 to reflect newest updates.

Setting up your local web development environment – Apache

Posted June 25th, 2010 in Tehnikalije by Metod

This is the first on in the series of tutorials on how to set up your local web development environment on linux (I’m using Ubuntu). It is targeting those people, who don’t know their way around linux, but don’t want to use generic packages. It’s more fun. :)

Download and prepare

First, you go to the apache homepage. You download apache, check sha1, because it can get corrupted!

  1. sha1sum httpd-2.2.22.tar.gz | grep ‘bf3bbfda967ac900348e697f26fe86b25695efe9′

If a line is returned, the file is OK, we can proceed, else, return to step 1.

Extract the archive.

  1. tar xzf httpd-2.2.22.tar.gz

Move into the directory of the extracted archive.

  1. cd httpd-2.2.22

Configure for compiling

Next, we have to configure apache, before we compile it.

  1. ./configure –prefix=/usr/local/apache2 –enable-so –enable-mods-shared=all –enable-mod-rewrite

  • –prefix=/usr/local/apache2 tells apache where to install all the files needed. In this case we are installing apache to /usr/local/apache2 directory.
  • –enable-mods-shared=all tells apache to compile all modules as dynamic shared modules. We can then enable these modules in httpd.conf file, via the LoadModule directive.
  • –enable-mod-rewrite tells apache to enable mod rewrite.

Troubleshoot

If you get this error during the configuration:

  1. mod_deflate has been requested but can not be built due to prerequisite failures

This happened because we enabled all mods by default, one of which is the mod_deflate that uses the zlib library. You can simply install its headers on ubuntu.

  1. sudo apt-get install zlib1g-dev

The dependencies should pass.

Compile

  1. make

Next one must be executed as super user.

  1. sudo make install

Test it

Now, apache should be working. Let’s test it.

  1. sudo /usr/local/apache2/bin/apachectl -k start

Go to: http://localhost

You should see a “It works!” on your screen.

But when we started apache, it threw out an error:

  1. httpd: Could not reliably determine the server’s fully qualified domain name, using 127.0.1.1 for ServerName

Configure it

It’s time to configure it, so it won’t throw errors at us. You can use different text editor than gedit (vi, nano, mcedit … )

  1. sudo gedit /usr/local/apache2/conf/httpd.conf

ServerAdmin → change it to your email, just for a clean configuration file.

Uncomment ServerName directive (remove # in front of it) and change www.example.com to localhost. With this, we got rid of that error from before.

DocumentRoot → change to directory in which you want to have your html, php etc. files.

<Directory “/usr/local/apache2/htdocs”> → change if you changed the DocumentRoot in the previous step.

Here we must change the line AllowOverride None to AlloweOverride All if we want mod_rewrite to function properly.

Check if the syntax of the configuration file is OK.

  1. /usr/local/apache2/bin/apachectl -t

If everything is OK, restart apache.

  1. sudo /usr/local/apache2/bin/apachectl -k restart

You’re good to go!

Post updated on February 1st, 2012 to reflect latest apache version.

Annoying Eclipse message about JavaHL on Ubuntu

Posted June 21st, 2010 in Tehnikalije by Metod

How to get rid of it?

First of, we need to install some libraries:

  1. sudo apt-get install libsvn-java


Once this is done, we need to tell JVM (Java Virtual Machine) to include the path to our new libsvnjavahl-1.so file, when searching for extensions. We can do this in the eclipse configuration file. It is located inside the directory in which you installed Eclipse and is called eclipse.ini.

Find the line -vmargs and after it (in a new line) add this one:

  1. -Djava.library.path=/usr/lib/jni


Restart Eclipse and you’re done.

Tested on Eclipse Galileo (3.5.2) through all latest releases on Ubuntu 10.04 and onward.

How to pin My Computer to Windows 7 taskbar directly

Posted February 20th, 2010 in Tehnikalije by Metod

I always ask myself this when installing W7. And always have to google for it. So this one is more of a personal note and if someone find it useful, even better.

1. Right click on the map icon in the taskbar.

2. Right click on Windows Explorer.

3. Properties

4. In the target field enter:

  1. %SystemRoot%\explorer.exe /E,::{20D04FE0-3AEA-1069-A2D8-08002B30309D}

That’s it. :)