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

Controller and layout name in views directory

Is it a bug or a feature? Or I've just missed something in documentation.

For example I have an IndexController::indexAction().

If I define routes in a class extended from \Phalcon\Mvc\Router\Group then view rendering from views/index/index.phtml. Routes are mounting using Phalcon\Mvc\Router\Annotations::mount() function.

But if I use \Phalcon\Mvc\Router\Annotations::addModuleResource() to add module-related routings then views are looking in different folder. Instead of views/index/index.phtml they should be placed in views/Index/index.phtml. Controller name is capitalized.

Layout acts the same way. It should be in views/layouts/index.phtml or in views/layouts/Index.phtml, respectively.

Unfortunately, I want to use both way to define routes, as annotation and mount them to router. Is it possible?



1.0k

Some code expamles.

My controller class:

namespace Frontend\Controller;

/**
 * @RouterPrefix("/")
 */
class IndexController extends \Phalcon\Mvc\Controller
{
    /**
     * @Get("home", name="home")
     */
    public function indexAction()
    {
    }
}

Routes in class

class Routes extends \Phalcon\Mvc\Router\Group
{
    public function initialize()
    {
        $this->setPaths(array(
            'module' => 'frontend',
            'namespace' => 'Frontend\Controller',
        ));

        $this->setPrefix('/');

        // Route to homepage
        $this->add('home', [
            'controller' => 'index',
            'action' => 'index',
        ]);
    }
}

Application bootstrap:

$router = new \Phalcon\Mvc\Router\Annotations(false);

$router->addModuleResource('frontend', 'Frontend\Index'); // *1*
$router->mount(new Routes()); // *2*

For line 1 beforeExecuteRoute $dispatcher->getControllerName() === 'Index'

For line 2 $dispatcher->getControllerName() === 'index'



1.0k

Have fixed issue with the following code in dispatcher:

        $di->set('dispatcher', function () {
            //Create an EventsManager
            $eventsManager = new EventsManager();

            //Camelize controller
            $eventsManager->attach(
                'dispatch:beforeDispatchLoop',
                function ($event, $dispatcher) {
                    $controller = Text::camelize($dispatcher->getControllerName());
                    $dispatcher->setControllerName($controller);
                }
            );

            $dispatcher = new Dispatcher();
            $dispatcher->setEventsManager($eventsManager);
            $dispatcher->setDefaultNamespace('Frontend\Controller');

            return $dispatcher;
        });

So, I simply capitalize every controller name.