A
A
Alexander Ivanov2017-05-25 15:40:39
PHP
Alexander Ivanov, 2017-05-25 15:40:39

How to fix bug in addFunction volt function?

I found a good example of creating a widget in phalcon
docs.phalconphp.ru/ru/1.2.0/reference/forms.html
read the article:
https://docs.phalconphp.com/en/3.0.0/reference/vol...
In general, I can’t understand why after the widget
is displayed {{ widget('Form',['2','4','5']) }} stops working and no error is displayed

// файл лежит в public/index.php

  // Register Volt as a service
  $di->set(
    "voltService",
    function ($view, $di) use ($config){
      $volt = new Volt($view, $di);

      $volt->setOptions(
        [
          "compiledSeparator" => "_",
          "compiledPath"      => "../cache/",
          "compiledExtension" => ".compiled",
        ]
      );

      $compiler = $volt->getCompiler();
//      $compiler->addFunction( //пробовал самотстоятельно функции добавлять смотрел как работает
//        "widget", function ($resolvedArgs, $exprArgs) {
////          return "Library\\Widgets::widget(" . $resolvedArgs . ")";
//        }
//      );

//уверен ошибка где-то здесь:
      $compiler->addFunction('widget', function($resolvedArgs, $exprArgs) use($compiler) {
        $class = trim($compiler->expression($exprArgs[0]['expr']), "'");
        $params = empty($exprArgs[1]['expr']) ? null : $compiler->expression($exprArgs[1]['expr']);
        $result = "\\Widgets\\$class::widget($params)";
        echo var_dump($result);
        return $result;
      });
      return $volt;
    }
  );

    // Регистрация сервиса widgetView
    $widget = $di->set('widgetView', [
        'className' => '\Phalcon\Mvc\View',
        'calls' => [
            ['method' => 'setViewsDir', 'arguments' => [
                ['type' => 'parameter', 'value' =>  '../app/widgets/'],
            ]],
            ['method' => 'registerEngines', 'arguments' => [
                ['type' => 'parameter', 'value' => [
                    ".volt" => "voltService",
                ]],
            ]],
        ],
    ]);

//думал что так он мне больше ошибок выведет но ни ошибок не предупреждений нет:

} catch (Exception $e) {
  echo get_class($e), ": ", $e->getMessage(), "\n";
  echo " File=", $e->getFile(), "\n";
  echo " Line=", $e->getLine(), "\n";
  echo $e->getTraceAsString();
}


//результат вардампа:
string(43) "\Widgets\Form::widget(array('2', '4', '5'))"

structure
app/Widgets/Form.php
app/Widgets/View/formView.volt
app/Library/Widget.php
public/index.php - head file

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
jasonOk, 2017-05-25
@jasonOk

Hello.
You just need to create (let's say in the directory /app/library/widget/)
a Widget class of this type and life will immediately become easy:

namespace Library\Widget;

use Phalcon\Mvc\User\Component;

class Widget extends Component
{
  ...

  public function output ($widget) { // к примеру
  ...

Then in the view everything is easier than ever:
Форма:

{{ widget.output('Form') }}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question