Tag vite :

Using Pomm with Silex

 Posté par greg le 5 avril 2011 23:09 |  Les commentaires sont désactivés pour ce 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.

Liens récents 

 Crew - code review for Git projects.

Marqué par greg le 6 mai 2012 07:57..
Tags: geek computing git

Suivre le lien

 responsive grid system

Marqué par greg le 5 mai 2012 12:38..
Tags: geek computing dev

Suivre le lien

 Vim "improved

Marqué par greg le 8 mars 2012 09:26..
Tags: geek computing vim

Suivre le lien

 Highly scalable NoSql blog

Marqué par greg le 3 mars 2012 09:12..
Tags: geek computing dev

Suivre le lien

 Bruce Momjian Pg blog

Marqué par greg le 16 février 2012 13:51..
Tags: geek computing postgresql net blog

Suivre le lien

 Richard Miller's blog

Marqué par greg le 16 février 2012 09:57..
Tags: geek computing dev php symfony net blog

Suivre le lien

 Online SQL explain interface

Marqué par greg le 15 février 2012 09:58..
Tags: geek computing postgresql net

Suivre le lien

 Ace online code editor

Marqué par greg le 15 février 2012 08:57..
Tags: net geek computing dev javascript

Suivre le lien

 Coding horror

Marqué par greg le 14 février 2012 12:42..
Tags: geek computing dev net blog

Suivre le lien

 telegraphCQ - Stream oriented database

Marqué par greg le 13 février 2012 14:44..
Tags: geek computing postgresql

Suivre le lien