Phalcon Integration Guide

How to register Tplix as a shared service in Phalcon 5's FactoryDefault DI container.

Phalcon · v5

Tplix + Phalcon

Register Tplix as a shared service in Phalcon's DI container and access it from any controller via $this->tplix.

composer require tplix/tplix
1

Register in Services

app/config/services.php
<?php
use Phalcon\Di\FactoryDefault;
use Tplix\Tplix;

$di = new FactoryDefault();

$di->setShared('tplix', function () {
    $tplix = Tplix::getInstance();
    $tplix->setTemplateDir(APP_PATH . '/views/tplix/');
    $tplix->setCompileDir(APP_PATH . '/cache/tplix_c/');
    $tplix->setCacheDir(APP_PATH  . '/cache/tplix/');
    return $tplix;
});

return $di;
2

Use in a Controller

app/controllers/IndexController.php
<?php
use Phalcon\Mvc\Controller;

class IndexController extends Controller
{
    public function indexAction(): string
    {
        $this->tplix->assign('title', 'Home');
        $this->tplix->assign('user',  $this->session->get('user'));
        return $this->tplix->fetch('index/index.tpl');
    }
}