Tag finder:

Using Pomm with Silex

 Posted by greg on April 5, 2011 11:09 PM |  Comments are deactivated for this post
Tags : geek  computing  postgresql  dev  php  silex  pomm 

Pomm and Silex: FAST !

I must say I am quite impressed by Fabien’s microframework known as Silex . I needed to give it a try as I have lot of demands for small but efficient websites. I naturally wrote an extension for Pomm:https://github.com/chanmix51/Pomm and, using the code of the PommBundle Tutorial I had a blog working in 10 minutes with Twig as template engine.

Installing Silex and PommExtension

Silex installation cannot be more simple. Download the PHAR file and edit an index.php:

<? #index.php

require "silex.phar";

use SilexApplication;

$app = new Application();
$app->get('/', function() { echo "Hello world"; });
$app->run();

Well, we have a web application that prints “Hello world”. I let you have a look in the concise and well done Silex’s documentation if you are not familiar with Silex yet. Installing the PommExtension should not be a hassle, everything is described in the README. Of course, you want to use twig, just clone the twig’s git repository in your vendor directory and register the module. At the end, your index.php should look like this:

<? #index.php

require "silex.phar";

use SilexApplication;
use SilexExtensionTwigExtension;
use SymfonyComponentHttpFoundationResponse;

#Pomm 
use ExtensionPommExtensionPommExtension;

$app = new Application();
$app['autoloader']->registerNamespace('Extension', __DIR__);
$app['autoloader']->registerNamespace('Model', __DIR__);

$app->register(
    New ExtensionPommExtensionPommExtension(), 
        array(
            'pomm.class_path' => __DIR__.'/vendor/pomm', 
            'pomm.connections' => array(
                'default' => array(
                  'dsn' => 'pgsql://nss_user:nss_password@localhost/nss_db'
                  )))
            );
$app->register(new TwigExtension(), array(
    'twig.path'       => __DIR__.'/views',
    'twig.class_path' => __DIR__.'/vendor/twig/lib',
));

Managing the model

Ok I want to use the database described in PommBundle’s tutorial but I still wanted to generate my map files properly for this project. I set up this little PHP script to scan the nss_blog schema:

<? #generate_mapfiles.php

require __DIR__.'/vendor/pomm/test/autoload.php';

PommPomm::setDatabase('default', array('dsn' => 'pgsql://nss_user:nss_password@localhost/nss_db'));

$scan = new PommToolsScanSchemaTool(array(
    'dir'=> __DIR__.'/Model/Pomm/Map',
    'schema' => 'nss_blog',
    'connection' => PommPomm::getDatabase('default'),
    'namespace' => 'ModelPommMap'
  ));
$scan->execute();

This means model Map files will be under the Model/Pomm/Map directory, so I copied the model files in the Model/Pomm directory from the PommBundle tutorial. Just change the namespace for the autoloader to find your model files and here you go:

<? #index.php

[...]

$app->get('/', function() use ($app) {
    $articles = $app['pomm']->getConnection()
      ->getMapFor('ModelPommPublishedArticle')
      ->findAll();

    return $app['twig']
      ->render('index.twig', array('articles' => $articles));
})->bind('homepage');

$app->run();

We have a MVC structure with routing, model methods, encapsulated controllers and awsome template engine. Just add a Yaml parser for config files and you probably have the fastest PHP MVC framwork ever… You can separate processing in different controller files if needed, create modules, create controllers in Extensions … I think I am in love.

Recent Links 

 Bruce Momjian Pg blog

Bookmarked by greg on February 16, 2012 1:51 PM..
Tags: geek computing postgresql net blog

Follow link

 Richard Miller's blog

Bookmarked by greg on February 16, 2012 9:57 AM..
Tags: geek computing dev php symfony net blog

Follow link

 Online SQL explain interface

Bookmarked by greg on February 15, 2012 9:58 AM..
Tags: geek computing postgresql net

Follow link

 Ace online code editor

Bookmarked by greg on February 15, 2012 8:57 AM..
Tags: net geek computing dev javascript

Follow link

 Coding horror

Bookmarked by greg on February 14, 2012 12:42 PM..
Tags: geek computing dev net blog

Follow link

 telegraphCQ - Stream oriented database

Bookmarked by greg on February 13, 2012 2:44 PM..
Tags: geek computing postgresql

Follow link

 Relational Database Technologies

Bookmarked by greg on February 13, 2012 1:28 PM..
Tags: geek computing postgresql net blog

Follow link

 J.Davis blog «Ideas on Databases»

Bookmarked by greg on February 12, 2012 8:59 PM..
Tags: geek computing postgresql net blog

Follow link

 Flotr2, javascript graph library

Bookmarked by greg on February 6, 2012 8:17 AM..
Tags: geek computing dev javascript

Follow link

 Node.js toolbox

Bookmarked by greg on January 31, 2012 10:09 AM..
Tags: geek computing dev javascript nodejs

Follow link