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

All action paths return to the controller index view

I've built a website with phalcon that has the following controller structure:

  • controllers
    • admin
      • TicketsController.php

and the following views structure

  • views
    • admin
      • tickets
        • case.volt
        • index.volt

This is my routes.php file. It routes the controllers within the Myapp\Controllers\Admin namespace to a url with /admin prefixed.

<?php
$router = new Phalcon\Mvc\Router();
$router->setDefaults([
    "controller" => "index",
    'namespace'  => 'Myapp\\Controllers',
]);
$router->add('/:controller/:action/:params', [
    'controller' => 1,
    'action'     => 2,
    'params'     => 3,
]);
$router->add('/:controller/:action', [
    'controller' => 1,
    'action'     => 2,
]);
$router->add('/:controller', [
    'controller' => 1,
]);

$router->add('/admin/:controller/:action/:params', [
    'namespace'  => 'Myapp\\Controllers\\Admin',
    'controller' => 1,
    'action'     => 2,
    'params'     => 3,
]);
$router->add('/admin/:controller/:action', [
    'namespace'  => 'Myapp\\Controllers\\Admin',
    'controller' => 1,
    'action'     => 2,
]);
$router->add('/admin/:controller', [
    'namespace'  => 'Myapp\\Controllers\\Admin',
    'controller' => 1,
]);
$router->add('/admin', [
    'namespace'  => 'Myapp\\Controllers\\Admin',
    'controller' => 'admindashboard'
]);
$router->removeExtraSlashes(true);
return $router;

Every controller gets a manual change for ViewsDir:

public function initialize(){
      $this->view->setViewsDir($this->view->getViewsDir() . "admin/" .  $this->dispatcher->getControllerName() . "/");
}

Question:

I always get the index view when I go to any action within the Tickets controller. For example: /admin/tickets/case/ returns the index.volt instead of the case.volt.

How can this issue be solved?



85.5k

public function initialize(){

try with

onConstruct()


43.9k
edited Jan '17

Hi,

did you had a look at https://github.com/phalcon/mvc#simple-subcontrollers ?

It looks similar to what you want to achieve. They use afterExecuteRoute() in that app, see here: https://github.com/phalcon/mvc/blob/master/simple-subcontrollers/app/controllers/admin/ControllerBase.php

Tip: To debug that kind of problem I make a heavy use of {{ dump($router) }} or any another kind of data that I pass to the view using the excellent phalcon lib see here all the methods beginning with "get" :-)



5.9k

public function initialize(){

try with

onConstruct()

The onConstruct() method instead of initialize() returns the same results :(



5.9k
edited Jan '17

Hi,

did you had a look at https://github.com/phalcon/mvc#simple-subcontrollers ?

It looks similar to what you want to achieve. They use afterExecuteRoute() in that app, see here: https://github.com/phalcon/mvc/blob/master/simple-subcontrollers/app/controllers/admin/ControllerBase.php

Tip: To debug that kind of problem I make a heavy use of {{ dump($router) }} or any another kind of data that I pass to the view using the excellent phalcon lib see here all the methods beginning with "get" :-)

I actually based my website on the simple-subcontrollers structure you linked. afterExecuteRoute() didn't fix my issue.

After debugging through the use of dumping in my case.volt view I've noticed it does actually return the correct action 'case' with the method getActionName().

I still can't find the issue why it just returns the index.volt page instead of the correct case.volt page which corresponds with the action.



5.9k
Accepted
answer

I've found the solution!

Changing the initialize method

from

$this->view->setViewsDir($this->view->getViewsDir() . "admin/" .  $this->dispatcher->getControllerName() . "/");

to

$this->view->setViewsDir($this->view->getViewsDir() . "admin/" );

Solved the issue.

However I do not know why this was an issue in the first place since it rendered the correct index.volt view from the correct folder.