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

Very strange bug

Hello. Noticed on the logs is very strange. I have a controller IndexController:

class IndexController extends Controller { public function indexAction() { $this->tag->setTitle('Home');

      $this->view->cases = Cases::find();
  }

  public function testAction()
  {
      $this->view->disable();
  }

}

In the view index/index.volt also has function calls making requests to the database. So, when you click on the link /index/test reason are the database queries (which should be when visiting /index/index). What's the problem? P.S. If you remove indexAction() { ... } then DB queries no.



5.8k

Why feature, if you view from another activity?



5.8k

Also, there is a pluggable library Authorization, it is only when you refresh the page creates a 1 DB query. But for some reason 2 of the request. Moreover, if inside the function (where the request) to hang up the output of some text in the view, it will appear only 1 time, not 2. Where there are suspicions that in fact page connected 2 times, but the second is added to the view. And for some reason the script runs inside the view index/index.volt, although shown inside index/test.volt. What's the deal?



5.8k
edited Sep '16

Moreover, additional code is executed only from indexAction.

Ie, if you move $this->view->cases = Cases::find(); in another Action, it won't happen this prompt when opening site index/test

Please read docs before commenting i posted links which is answer to your problem.

This is just a feature, with indexAction as main action of each controller you can render controller template for each action, and then other actions can use it's content. It's nice feature which you can disable.



5.8k

You tell me about layouts for the controller?



5.8k
edited Sep '16

The fact of the matter is that even at the same time, still be queries from the view index/index.volt.

$this->view->setRenderLevel( View::LEVEL_NO_RENDER );

Although the display is generally disabled for this action.



5.8k

He not only performs Index/Index.volt but of course function indexAction(). That is not the case only in Phalcon\Mvc\View;

edited Sep '16

What you don't understand ? You need to disable LEVEL_LAYOUT level if you don't want this behaviour. Everything is in docs.

To render layout it's obviously need to execute this action to make sure he has all necessary variables for example.



5.8k
edited Sep '16

So? $this->view->disableLevel( [ View::LEVEL_LAYOUT => true ] );



5.8k
edited Sep '16

1) I Have basically no layouts. 2) When disabling layouts on view through disableLevel no change is observed.

$dependencyInjector->set('view', function() use ($config) { $view = new View(); $view->setViewsDir($config->engine->views); $view->registerEngines( [ '.volt' => function ($view) use ($config) { $volt = new Volt($view, $this); $volt->setOptions( [ 'compiledPath' => $config->engine->cache . 'views/', 'compiledSeparator' => '_' ] ); return $volt; } ] ); $view->disableLevel([View::LEVEL_LAYOUT => true]); return $view; }, true );

Then maybe it's some other level, im not using veiws so hard, but for sure it's because levels. You just need to disable certain level. I will check it when i home.



5.8k

Even in this view, is the indexAction(): $view->disableLevel( [ View::LEVEL_LAYOUT => true, View::CACHE_MODE_INVERSE => true, View::CACHE_MODE_NONE => true, View::LEVEL_ACTION_VIEW => true, View::LEVEL_AFTER_TEMPLATE => true, View::LEVEL_BEFORE_TEMPLATE => true, View::LEVEL_MAIN_LAYOUT => true ] );

Well. Can you show your disptacher ? Just add some event manager to dispatcher to log beforeRouteExecute, im guessing you have to have somewhere forward or something. I don't have issue like you have.



5.8k

I found that this only happens with index/indexAction(), i.e. only the Index controller. Or something I didn't notice...

$dependencyInjector->set('dispatcher',
function()
{
    $dispatcher = new Dispatcher();
    $dispatcher->setDefaultNamespace('Project\Controllers');
    return $dispatcher;
},
true
);
$di = new Phalcon\DI\FactoryDefault();

$di->setShared(
    'db',
    function () {
        $connection = new Phalcon\Db\Adapter\Pdo\Mysql(
            [
                'host' => 'localhost',
                'port' => 3306,
                'dbname' => 'phalcon_test',
                'username' => 'root',
                'charset' => 'UTF8',
            ]
        );

        return $connection;
    }
);
$di->set(
    'view',
    function () {
        $view = new \Phalcon\Mvc\View();
        $view->setViewsDir('views');
        $view->registerEngines(
            [
                '.volt' => function ($view, $di) {
                    $volt = new Volt($view, $di);
                    $options = [
                        'compiledPath' => 'cache',
                    ];
                    $options += ['compileAlways' => true];
                    $volt->setOptions($options);

                    return $volt;
                },
            ]
        );
        return $view;
    }
);
$di->set(
    'router',
    function () {
        $router = new \Phalcon\Mvc\Router();
        $router->add('/index', ['controller' => 'index', 'action' => 'index']);
        $router->add('/test', ['controller' => 'index', 'action' => 'test']);
        return $router;
    }
);

class Albums extends Model
{
}

class IndexController extends \Phalcon\Mvc\Controller
{
    public function indexAction()
    {
        $test = Albums::find();
        $this->view->setVar('albums', $test);
        var_dump($test);
    }

    public function testAction()
    {

    }
}

$app = new \Phalcon\Mvc\Application($di);
echo $app->handle()->getContent();

When im accessing /test I don't have issue like your, i don't have any result from index action. Post script please to reproduce a problem. Or just check logs as i wrote.



5.8k

Strangely, I have almost the same services are connected... do Not know.

Maybe it's about your nginx/apache config something ? Just check code i posted above, create some views. If it's still happening then check latest phalcon version(you need zephir to build it). What version you are using currently ? Also add some dispatcher listener to log all beforeExecuteRoute events to log. Just remove all unncessary services. Maybe you already have some dispatcher listener or you are doing forward somewhere ?