PHP 5.4 Install php-intl extension on centos 5 with latest ICU data

When i moved my Symfony project to VPS, the problem is missing php-intl because i have installed PHP from Remi repository (php 5.4.11). I will show you how can we fix it.

Install ICU version 4+ to meet symfony’s requirements by compile it from source

I choose ICU version 4.8.1.1

wget http://download.icu-project.org/files/icu4c/4.8.1.1/icu4c-4_8_1_1-src.tgz
tar -xvf icu4c-4_8_1_1-src.tgz
cd icu/source/
./runConfigureICU Linux
make
make install

If Everything is OK, you are ready to next step

Compile php-intl extension from source
Let’s choose the same php version with your self, my case is PHP 5.4.11

wget http://php.net/distributions/php-5.4.11.tar.bz2
tar -xvf php-5.4.11.tar.bz2
cd php-5.4.11/ext/intl
phpize
./configure --enable-intl
make
make install

Everything is OK, we have intl.so at /usr/lib64/php/modules/intl.so

Edit php.ini to load extension

extension=intl.so

Restart php-fpm

/etc/init.d/php-fpm restart

Symfony 2: Injecting service as dependency into doctrine repository

To do that, you must declare repository as a service and inject services which you need into it.

cities_repository:
        class: MY\EntityBundle\Entity\CitiesRepository
        arguments: [MYEntityBundle:Cities]
        factory_service: doctrine.orm.entity_manager
        factory_method: getRepository
        calls:
            - [ setCanonicalizer, [ @util.canonicalizer ] ]

The repository class:

namespace MY\EntityBundle\Entity;

use Doctrine\ORM\EntityRepository;
use FOS\UserBundle\Util\CanonicalizerInterface;

/**
 * CitiesRepository
 */
class CitiesRepository extends EntityRepository
{
    /**
     * @var CanonicalizerInterface $canonicalizer
     */
    protected $canonicalizer;

    public function setCanonicalizer(CanonicalizerInterface $canonicalizer)
    {
        $this->canonicalizer = $canonicalizer;
    }

    public function getCanonicalizer()
    {
        return $this->canonicalizer;
    }

    public function saveFromString($cityName, Countries $country)
    {
        $city = $this->findOneBy(
            array('name' => $this->canonicalizer->canonicalize($cityName))
        );
        if (!$city) {
            $city = new Cities();
            $city->setName($cityName);
            $city->setCountry($country);
            $this->_em->persist($city);
            $this->_em->flush();
        }

        return $city;
    }
}

Usage

$city = $this->get('cities_repository')->saveFromString(
            'Ho Chi Minh',
            new Countries('VN', 'Vietnam')
        );

Symfony 2 redefine form_row for horizontal form in bootstrap support validation style

Override form_row block in your twig template as follow:

{% form_theme signup _self %}
{% block form_row %}
    {% spaceless %}
        <div class="control-group{% if errors|length > 0 %} error{% endif %}">
            {{ form_label(form, '', { 'label_attr': {'class': 'control-label'} }) }}
            <div class="controls">
                {{ form_widget(form) }}
                {% if form.vars.help is defined %}
                    <span class="help-block">{{ form.vars.help }}</span>
                {% endif %}
            </div>
            {{ form_errors(form) }}
        </div>
    {% endspaceless %}
{% endblock form_row %}
{% block form_errors %}
    {% spaceless %}
        {% if errors|length > 0 %}
            <div class="controls">
                {% for error in errors %}
                    <span class="help-block">{{ error.messagePluralization is null
                    ? error.messageTemplate|trans(error.messageParameters, 'validators')
                    : error.messageTemplate|transchoice(error.messagePluralization, error.messageParameters, 'validators') }}</span>
                {% endfor %}
            </div>
        {% endif %}
    {% endspaceless %}
{% endblock form_errors %}

Usage

{{ form_widget(signup) }}

Use Memcache for Session handler on Symfony 2.1

#parameters.yml

session_memcache_host: 127.0.0.1
session_memcache_port: 11211
session_memcache_prefix: sess
session_memcache_expire: 3600

#config.yml

services:
    session.memcache:
        class: Memcache
        calls:
             - [ addServer, [ %session_memcache_host%, %session_memcache_port% ]]

    session.handler.memcache:
        class: Symfony\Component\HttpFoundation\Session\Storage\Handler\MemcacheSessionHandler
        arguments: [ @session.memcache, { prefix: %session_memcache_prefix%, expiretime: %session_memcache_expire% } ]

framework:
    session:
        handler_id: session.handler.memcache

[Ubuntu] PHP 5.4.x and gearman troubleshooting

When upgraded to PHP 5.4.x, you may have an error:
PHP Warning:  PHP Startup: Unable to load dynamic library ‘/usr/lib/php5/20100525/gearman.so’ – /usr/lib/php5/20100525/gearman.so: undefined symbol: gearman_job_error in Unknown on line 0

[Resolve] Update your gearmand server to the latest version

Remove current gearman from packages manager
sudo apt-get remove gearman gearman-job-server

Install lastest version (now gearmand-1.1.1.tar.gz )
sudo apt-get install libboost-program-options-dev libboost-all-dev libevent-dev cloog-ppl
tar -xvf gearmand-1.1.1.tar.gz && cd gearmand-1.1.1/
./configure
make
sudo make install

Run gearman
gearmand -d

Check version
gearadmin --server-version

Start gearman at boot up: init script

Centos 5.8 install your own git server

Install dependencies

yum -y install zlib-devel openssl-devel cpio expat-devel gettext-devel

Install from source

wget https://git-core.googlecode.com/files/git-1.7.11.1.tar.gz
tar -xzvf git-1.7.11.1.tar.gz 
cd git-1.7.11.1
./configure 
make
make install

Check install:

git --version

Create new Test repo:

useradd git
cd /home/git
mkdir test-repo
cd test-repo
git init
cd ../
git clone --bare ./test-repo/ test-repo.git

Add your public key to authorize

cd /home/git
mkdir .ssh
touch .ssh/authorized_keys
# import your public key into authorized_keys
chown -R git.git ./*

Now you can clone your test-repo and then pull, push

git link: git@server_ip:test-repo.git

Codeigniter hook: combine master page and auto load view

Get source at: https://github.com/phthviet/codeigniter_hook_master_autoview

How to use?

When use this hook. you don’t need to load view manually by hand, just doing what are in your controller codes

Old way:
public function index()
{
// your code
$this->load->view('welcome_message');
}

New way:
public function index()
{
// your code
}

The view of action will auto located at path /application/views/{controller}/{method}.php if exists

Note: the view only will be locate if there is no output from controller ($this->output->set_output() won’t be call in controller codes)

will be continue…

Centos 6.2: Compile PHP 5.4.2 from source

Before installation start, make sure you have already installed web server such as Apache, Nginx… and Mysql server

Install command:

[dependecies]

yum -y install libmcrypt libmhash openssl-devel bzip2-devel libxml2-devel libjpeg-devel libpng-devel libmcrypt libmcrypt-devel pcre pcre-devel curl-devel freetype-devel gmp-devel mysql-devel ncurses-devel pspell-devel net-snmp-devel

[mhash]

wget http://nchc.dl.sourceforge.net/project/mhash/mhash/0.9.9.9/mhash-0.9.9.9.tar.bz2

tar -xvf mhash-0.9.9.9.tar.bz2 && cd mhash-0.9.9.9 && ./configure && make && make install  && cd ../

[mcrypt]

wget http://nchc.dl.sourceforge.net/project/mcrypt/MCrypt/2.6.8/mcrypt-2.6.8.tar.gz

tar -xvf mcrypt-2.6.8.tar.gz && cd mcrypt-2.6.8 && ./configure && make && make install && cd ../

Download php source from php.net, extract it

[Compile]

cd php-5.4.x

./configure \

--with-apxs2=/usr/sbin/apxs \

--with-config-file-path=/etc \

--enable-intl \

--with-mysql \

--with-mysqli \

--with-pdo-mysql \

--enable-sockets \

--with-zlib \

--with-bz2 \

--with-gd \

--with-jpeg-dir \

--with-png-dir \

--enable-ftp \

--enable-mbstring  \

--with-mcrypt \

--with-mhash \

--with-pear \

--with-curl \

--with-openssl \

--with-libxml-dir \

--enable-exif \

--with-freetype-dir \

--enable-soap \

--with-tidy \

--disable-cgi