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 path with annotations routing

I have a single-module app with annotations-based routing (default routes enabled)...

//Router
$di->set('router', function() {
    $router = new \Phalcon\Mvc\Router\Annotations(true);
    $router->addResource('Controllers\Char', '/char');
    return $router;
});

...with one controller like this...

namespace Controllers;

use Evehub\Models\Character;
use Phalcon\Mvc\Controller;
use Phalcon\Mvc\Dispatcher;

/**
 * Class CharController
 *
 * @RoutePrefix('/char')
 */
class CharController extends Controller
{
    private function setCommonData()
    {
        //Common view variables for every future action in this controller
        $this->view->character = Character::findByName($this->dispatcher->getParam('charname'));
    }

    /**
     * @Get('/{charname}')
     */
    public function indexAction()
    {
        $this->setSharedData();
    }
}

...and one without annotations (default routes). When request follows default routes (for example, '/index/test') - testAction in IndexController matched and rendered view is 'views/index/test'. With annotations (for example, '/char/any-char-name') - indexAction in CharController matched and executed but 'views/char/index' view not rendered.

Is it expected behavior and I need to manually specify view in every action with $this->view->pick?



98.9k

Passing 'true' as first parameter means that you want to use the default routes, I recommend you not use the default routes together with the annotated routes.

Also, try changing the view name to: views/Char/index

"Also, try changing the view name to: views/Char/index" - thanks, that's it!

"Passing 'true' as first parameter means that you want to use the default routes" - yes, that was a goal - use default routes for simple controllers but define annotations for more complex ones. "I recommend you not use the default routes together with the annotated routes" - then, maybe, this should be noted in manual. In fact, it works but... Has some undocumented nuances like this topic :)



3.7k
edited Apr '14

$di->set('dispatcher', function () { $dispatcher = new Dispatcher(); $dispatcher->setDefaultNamespace('Unic\Controllers'); return $dispatcher; });

  1. $router->addGet("/register", array('controller' => 'home','action' => 'register'))->setName("register");

    this route is working correctly

  2. $router->add('/trainee/dispaly/show', array( 'namespace' => 'Unic\Controllers\Trainee', 'controller' => 'display', 'action'=>'show' ));

    this route showing project\Controllers\Trainee\DisplayController handler class cannot be loaded28/home/project/public_html/public/index.php2#0 [internal function]: Phalcon\Mvc\Dispatcher->_throwDispatchException('Unic\Controller...', 2) #1 [internal function]: Phalcon\Dispatcher->dispatch() #2 /home/unic/public_html/public/index.php(28): Phalcon\Mvc\Application->handle() #3 {main}

    please find my bug