Skip to main content

Posts

Showing posts from 2011

Passing PHP data to Apache access logs

Recently, I worked on a project to collect performance data from an application at work and pass that data to Apache access logs as part of a performance improvement project. As it turns out, PHP has a built-in function called apache_note() that your can use for this purpose. For example, if you wish to pass memory usage information to Apache access logs, add the following code to at the end of your front controller file: if (php_sapi_name() != 'cli') { apache_note('PHPMemoryUsage', memory_get_peak_usage(true)); } Then, update your Apache LogFormat directive to include this new piece of information: LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %{PHPMemoryUsage}n" combined

Mapping embedded documents in Doctrine MongoDB ODM

If you have a MongoDB document that has another document embedded in it, you still have to create a separate XML mapping document for the embedded one. So if you have a parent document called Activity that has an embedded document called Coordinates , the configuration would look something like this: src/Acme/ActivityBundle/Resources/config/doctrine/Activity.mongodb.xml <doctrine-mongo-mapping xmlns="http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping.xsd"> <document name="Acme\ActivityBundle\Document\Activity" db="acme" collection="activity" customId="true"> <field fieldName="id" id="true" strategy="INCREMENT...

Creating a Symfony2 helper

While working on a Symfony2 project yesterday, I realized I needed a better way to format dates in my templates. The reason behind this was the fact that my MongoDB document entities contained date values that were either null or DateTime instances. This entailed using a bunch of if statements for extra checks that made template code unnecessarily verbose. My solution was to create a template helper in an application specific framework bundle that I had created previously to contain all common assets and other necessary common resources. Create a Helper directory in the framework bundle directory and create a DateHelper class: src/Acme/FrameworkBundle/Helper/DateHelper.php namespace Acme\FrameworkBundle\Helper; use Symfony\Component\Templating\Helper\Helper; class DateHelper extends Helper { protected $options; public function __construct(array $options) { $this->options = $options; } public function getName() { return ...

Adding post-login logic to FOSUserBundle

Having finally figured out how to use FOSUserBundle in my project, I decided to keep track of all logins next. The implementation turned out to be a breeze thanks to Symfony2's security listener mechanism. As usual, the first step is to create a MongoDB document for this purpose. This is a very simple document that contains a user's id, session id, IP address, and login date. src/Acme/UserBundle/Document/LoginHistory.php namespace Acme\UserBundle\Document; class LoginHistory { protected $id; protected $userId; protected $sessionId; protected $ip; protected $createdAt; /** * Get id * * @return custom_id $id */ public function getId() { return $this->id; } /** * Set userId * * @param int $userId */ public function setUserId($userId) { $this->userId = $userId; } /** * Get userId * * @return int $userId */ public function getUserId...

Symfony2 choice list type for MongoDB backed forms

A couple of days ago, I had to implement a select field representing a one-to-one MongoDB relationship in one of my Symfony2 forms. Having spent some time reading the documentation, I decided to use the entity form field type for this purpose. class AcmeFormType extends AbstractType { public function buildForm(FormBuilder $builder, array $options) { $builder ->add('parent', 'entity', array( 'class' => 'Acme\MyBundle\Document\MyDocument', 'property' => 'name', 'label' => 'Parent', 'empty_value' => 'Root' )); } } And I got presented with this exception: Class Acme\MyBundle\Document\MyDocument is not a valid entity or mapped super class. I realized Symfony2 was trying to load the necessary class using the default entity manager defined by the Doctrine ORM and failing utterly. Afte...

Integrating Memcached with Symfony2

Overview This is a short tutorial to add Memcached support to your Symfony 2 application. This is actually not a really good way to add caching support to your application as your API should support multiple caching mechanisms just like Zend_Cache does; however, all I need for my application is a way to access a Memcached singleton instance and this setup satisfies that requirement. Configuration Generate a new bundle in your application source directory using the command below. php app/console generate:bundle In your app/config.yml, create a section for your bundle: app/config/config.yml acme_memcached: servers: server1: host: localhost port: 11211 weight: 25 server2: host: localhost port: 11211 weight: 75 Note: You should also add a persistence flag to this configuration; however, I am going to omit this flag to keep this tutorial simple. Once we have a format that we like, we need t...

Symfony2 SecurityBundle and FOSUserBundle integration: How does it work?

Overview A couple of days ago, I realized I needed to add some new functionality to the login process. Specifically, I needed to track all previous login attempts. Not knowing anything about the new Symfony2 SecurityBundle, I had to go through the underlying code to understand what was going on. In the process, I think got a basic idea about how the new SecurityBundle interacts with FOSUserBundle. Configuration I have a basic security configuration as illustrated below. app/config/security.yml security: encoders: Symfony\Component\Security\Core\User\User: plaintext role_hierarchy: ROLE_ADMIN: ROLE_USER ROLE_SUPER_ADMIN: ROLE_ADMIN providers: fos_userbundle: id: fos_user.user_manager firewalls: main: pattern: .* form_login: provider: fos_userbundle check_path: /user/login_check login_path: /user/login lo...

Unexpected token "name" of value "if" ("end of statement block" expected) in "WebProfilerBundle:Collector:logger.html.twig"

Encountered this WebProfilerBundle error message when I ran the bin/vendors script to update my Symfony2 bundles. Make sure your deps file is up to date; you need to pay special attention to your version values. In this case, update your twig version to v1.2.0 as illustrated below: [twig] git=http://github.com/fabpot/Twig.git version=v1.2.0 Run the vendors script to update your bundle and the error message should disappear. You can get the most up to date deps file from the symfony-standard repository located at: https://github.com/symfony/symfony-standard/blob/master/deps

Symfony 2 + DoctrineMongoDBBundle + FOSUserBundle Tutorial

It's been a while I have not added an entry to my blog. I have been busy playing with Symfony 2, MongoDB, and FOSUserBundle for a while now so here is a tutorial to integrate Symfony 2, MongoDB, and FOSUserBundle. Objectives * Install DoctrineMongoDBBundle * Install FOSUserBundle * Create user model ** Utilize groups ** Add additional properties to user model * Create customized registration form and handler At this point I am going to assume that you have a running Symfony 2 and MongoDB installation in place already and a basic understanding of how to configure Symfony 2 services. I will also exclude view related steps from this tutorial. Setting Up DoctrineMongoDBBundle Add the following to your deps file: [doctrine-mongodb] git=http://github.com/doctrine/mongodb.git [doctrine-mongodb-odm] git=http://github.com/doctrine/mongodb-odm.git [DoctrineMongoDBBundle] git=http://github.com/symfony/DoctrineMongoDBBundle.git target=/bundles/Symfony/Bundle/Doctrin...

Exim posing as Sendmail: Fixing Sender and Return-Path headers

Exim If you are having problems changing the Sender and Return-Path headers, make sure that you are editing the right configuration file. On my CentOS 5.6: [root@server mail]# ll /usr/sbin/sendmail lrwxrwxrwx 1 root root 21 Oct 26 2009 /usr/sbin/sendmail -> /etc/alternatives/mta [root@server mail]# ll /etc/alternatives/mta lrwxrwxrwx 1 root root 23 Apr 9 07:48 /etc/alternatives/mta -> /usr/sbin/sendmail.exim [root@server mail]# ll /usr/sbin/sendmail.exim lrwxrwxrwx 1 root root 4 Apr 9 07:45 /usr/sbin/sendmail.exim -> exim I spent some time trying to figure out why my changes to the sendmail.mc file were being ignored. Naturally, Exim configuration is different than Sendmail. You need to edit the /etc/exim/exim.conf file: remote_smtp: driver = smtp return_path = bounce@domain.com headers_rewrite = apache@* info@domain.com s Don't forget the "s" at the end. See this page for more information: http://www.exim.org/exim-html-2.00/doc/html/spec_32.html...

PHP API for Solr

I finally had some time to finish my PHP client for Solr. It is still in alpha but I am planning to finalize testing for a beta release soon. The source code can be found at https://github.com/buraks78/Logic-Solr-API .