Friday, May 8, 2015

Phalcon Smarty Integration

So after a lot of late nights by one of my senior developers we now have Phalcon neatly integrated into our code base.

From the beginning of the project we have been making use of the Smarty templating engine and although Phalcon has its own (Volt) due to the team being used to Smarty and in my opinion Smarty being more powerful we have opted to continue with it.

In order to integrate Smarty into Phalcon we customised the class that incubator created it can be retrieved here  . This class extends the Engine class from Phalcon which later uses dependency injection to integrate Smarty into Phalcon,

So now that we have that class integrated we need to make use of it. In our public folder of Phalcon we setup a kernel file for each one of our sites.


try
{
    require dirname(__DIR__) . "/configuration.php";

    /**
     * Read the configuration
     */
    $config = include  WEBS_DIR . "/config/config.php";

    /**
     * Read auto-loader
     */
    include WEBS_DIR . "/config/loader.php";

    /**
     * Read services
     *
     * @var \Phalcon\Di $di
     */
    include WEBS_DIR . "/config/services.php";

    /**
     * Read services
     */
    include WEBS_DIR . "/config/routes.php";

    /**
     * Handle the request
     */
    $application = new \Phalcon\Mvc\Application($di);

    $application->router->setUriSource(\Phalcon\Mvc\Router::URI_SOURCE_SERVER_REQUEST_URI);

    // Register the installed modules
    $application->registerModules(
        array(
            'website'  => array(
                'className' => 'Axxess\Website\Module',
                'path'      => WEBS_DIR . 'class.Module.php',
            )
        )
    );


    echo $application->handle()->getContent();

}
catch (\Exception $e)
{
    echo $e->getMessage();
}


So now that we have setup our kernel we have to create the Smarty setup in the services.php in out site's folder.
Firstly instantiate the Phalcon dependency injector.

/**
 * The FactoryDefault Dependency Injector automatically register the right    services providing a full stack framework
 */
$di = new FactoryDefault();


then we inject the Smarty engine

$di->set('view', function () use ($config)
{
    $view = new View();

    $view->setViewsDir($config->application->viewsDir);

    $view->registerEngines(array(
            '.tpl' => function(View $view, $di)
            {

                $smarty = new SmartyEngine($view, $di);

                $smartyInstance = $smarty->getSmarty();
                $smartyInstance->setTemplateDir($view->getViewsDir())
                    ->setCompileDir(WEBS_DIR . 'viewsCompiled' . DS)
                    ->setCacheDir(WEBS_DIR . 'viewsCache' . DS)
                    ->setConfigDir(WEBS_DIR . 'viewsConfigs' . DS)
                    ->addPluginsDir(SMARTY_DIR . 'plugins' . DS . 'axxess' . DS . 'website');

                return $smarty;
            }
        )
    );

    return $view;
}, true);


This now allows us to access smarty like we would of Volt in the view variable in the Phalcon controllers.

No comments:

Post a Comment