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

View related to action not picked

HI

I have a small problem (I can do what I want with another way, but it's weird) I have a SessionController

<?php

namespace App\Controllers;

class SessionController extends ControllerBase {

public function initialize()
{
    parent::initialize();
    $this->view->setTemplateBefore('session');
}

public function loginAction()
{

// Shows only the view related to the action $this->view->pick('session/login'); }

A route /login in my router

$router->add( '/login', [ 'controller' => 'Session', 'action' => 'login', ] );

And a 'login.phtml' view inside a 'session' folder. And the problem is, my login.phtml is not picked with $this->view->pick('session/login'); and I really can't know why.

Hi @etshy two things

one: $this->view->pick('session/login'); you are using "login" is the same action name

two: do you have set right the view service?



8.2k
edited Oct '17

I don't change the view services definition from the create-project

$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;

});

with the viewsDir

'viewsDir' => APP_PATH . '/views/',

So I have my view in APP_PATH/viewssession/login.phtml

So, normally, It take my main view in views/index.phtml, a templateBefore views/layouts/session.phtml and my action view in /views/session/login.phtml

but it doesn't works.

I know I pick the same action name as template, but that's the problem. The view is not picked if I don't do it explicitly.

Don't understant because it works great on my IndexController and my indexAction. in views/layout/index.phtml and views/index/index.html