We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

my function doesnt work in volt

I defined sef link function in app/config/services.php. yet i cant use it in volt healthy. those are my usages and results i got.

1 - i tried without space, it gave wanted results yet besides notice.

        <div>{{ sef_url('evrimağaci') }}</div>

THIS NOTICE :

Notice: Use of undefined constant evrimagaci - assumed 'evrimagaci' in /var/www/html/project/cache/_var_www_html_project_app_views_search_show.volt.php on line 43
evrimagaci

2- with space:

        <div>{{ sef_url('evrim ağaci') }}</div>

NOTICE :

Notice: Use of undefined constant evrim - assumed 'evrim' in /var/www/html/instagram/cache/_var_www_html_instagram_app_views_search_show.volt.php on line 43

Notice: Use of undefined constant agaci - assumed 'agaci' in /var/www/html/instagram/cache/_var_www_html_instagram_app_views_search_show.volt.php on line 43
0

here is my app/config/services.php file.

<?php

use Phalcon\Mvc\View;
use Phalcon\Mvc\View\Engine\Php as PhpEngine;
use Phalcon\Mvc\Url as UrlResolver;
use Phalcon\Mvc\View\Engine\Volt as VoltEngine;
use Phalcon\Mvc\Model\Metadata\Memory as MetaDataAdapter;
use Phalcon\Session\Adapter\Files as SessionAdapter;
use Phalcon\Flash\Direct as Flash;

/**
 * Shared configuration service
 */
$di->setShared('config', function () {
    return include APP_PATH . "/config/config.php";
});

/**
 * The URL component is used to generate all kind of urls in the application
 */
$di->setShared('url', function () {
    $config = $this->getConfig();

    $url = new UrlResolver();
    $url->setBaseUri($config->application->baseUri);

    return $url;
});

/**
 * Setting up the view component
 */
$di->setShared('view', function () {
    $config = $this->getConfig();

    $view = new View();
    $view->setDI($this);
    $view->setViewsDir($config->application->viewsDir);

    $view->registerEngines([
        '.volt' => function ($view) {
            $config = $this->getConfig();

            $volt = new VoltEngine($view, $this);

            $volt->setOptions([
                'compiledPath' => $config->application->cacheDir,
                'compiledSeparator' => '_'
            ]);

            $compiler = $volt->getCompiler();

            /* Function Start */
            $compiler->addFunction(
                'sef_url',
                function page($content_id,$char) {

                $content = $db->get_var("select content from pages where id = '$content_id'");
                $content = stripslashes(strip_tags($content));
                $content = mb_substr($content,0,$karakter,'UTF-8');

                return $content."...";
            }
            );
            /* Function End */

            return $volt;
        },
        '.phtml' => PhpEngine::class

    ]);

    return $view;
});

/**
 * Database connection is created based in the parameters defined in the configuration file
 */
$di->setShared('db', function () {
    $config = $this->getConfig();

    $class = 'Phalcon\Db\Adapter\Pdo\\' . $config->database->adapter;
    $params = [
        'host'     => $config->database->host,
        'username' => $config->database->username,
        'password' => $config->database->password,
        'dbname'   => $config->database->dbname,
        'charset'  => $config->database->charset
    ];

    if ($config->database->adapter == 'Postgresql') {
        unset($params['charset']);
    }

    $connection = new $class($params);

    return $connection;
});

/**
 * If the configuration specify the use of metadata adapter use it or use memory otherwise
 */
$di->setShared('modelsMetadata', function () {
    return new MetaDataAdapter();
});

/**
 * Register the session flash service with the Twitter Bootstrap classes
 */
$di->set('flash', function () {
    return new Flash([
        'error'   => 'alert alert-danger',
        'success' => 'alert alert-success',
        'notice'  => 'alert alert-info',
        'warning' => 'alert alert-warning'
    ]);
});

/**
 * Start the session the first time some component request the session service
 */
$di->setShared('session', function () {
    $session = new SessionAdapter();
    $session->start();

    return $session;
});


39.3k
Accepted
answer
edited Jul '19

An easier approach for you would be to create a component class that will give you what you need and add it to the DI container like this:

// Volt
$di->setShared(
    'view',
    function () {
        $config = $this->getConfig();

        $view = new View();
        $view->setDI($this);
        $view->setViewsDir($config->application->viewsDir);

        $view->registerEngines(
            [
                '.volt' => function ($view) {
                    $config = $this->getConfig();

                    $volt = new VoltEngine($view, $this);

                    $volt->setOptions(
                        [
                            'compiledPath' => $config->application->cacheDir,
                            'compiledSeparator' => '_'
                        ]
                    );

                    return $volt;
                },
                '.phtml' => PhpEngine::class

            ]
        );

        return $view;
    }
);

// Permalink
$di->setShared(
    'perma',
    function () {
        return new PermaLink();
    }
);

Class:

class PermaLink extends Phalcon\Mvc\Plugin
{
    public function url(string $content_id, string $char): string
    {
        $content_id = $this->filter->sanitize($content_id, 'int');
        $record     = Pages::findFirst(
            [
                'conditions' => 'id = :id:',
                'bind'       => [
                    'id' => $content_id,
                ]
            ]
        );

        $content = stripslashes(strip_tags($record->icerik));
        $content = mb_substr($content, 0, $char, 'UTF-8');

        return $content . "...";
    }
}

Volt:

{{ perma.url(''evrimağaci') }}