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

Use Phalcon\Mvc\Url with Annotations

Hi,

lets say I have an application using the annotations router and I want to generate links using Phalcon\Mvc\Url.

Setup is:

<?php
try {
    //Register an autoloader
    $loader = new \Phalcon\Loader();
    $loader->registerDirs(array(
        '../app/controllers/',
    ))->register();
    //Create a DI
    $di = new Phalcon\DI\FactoryDefault();
    //Setting up the view component
    $di->set('view', function () {
        $view = new \Phalcon\Mvc\View();
        $view->setViewsDir('../app/views/');
        return $view;
    });
    $di->set('router', function () {
        $router = new \Phalcon\Mvc\Router\Annotations(false);
        $router->setUriSource(\Phalcon\Mvc\Router::URI_SOURCE_SERVER_REQUEST_URI);
        $router->removeExtraSlashes(true);
        $router->add("/", array('controller' => 'index','action' => 'index'));
        $router->notFound(array("controller" => "error", "action" => "notFound"));
        $router->addResource('Index', '/');
        $router->addResource('Authentication', '/auth');
        return $router;
    });
    $di->set('url', function() {
        $url = new \Phalcon\Mvc\Url();
        return $url;
    });
    //Handle the request
    $application = new \Phalcon\Mvc\Application($di);
    echo $application->handle()->getContent();
} catch (\Phalcon\Exception $e) {
    echo "PhalconException: ", $e->getMessage();
}

My IndexController has an action like:

<?php
/**
 * @RoutePrefix("")
 */
class IndexController extends \Phalcon\Mvc\Controller
{
    /**
     * @Get("/")
    */
    public function indexAction()
    {
        echo $this->url->get(array('for'=>'indexTestRoute','testParam'=>'routingTest','testId'=>12)); // This one works
        echo $this->url->get(array('for'=>'loginRoute')); // This one throws: PhalconException: Cannot obtain a route using the name "loginRoute"
    }

    /**
     * @Get("/test/{testParam:[a-zA-Z]+}/{testId:[0-9]+}", name="indexTestRoute")
     */
    public function testAction($testParam, $testId) {

    }
}

And my AuthenticationController:

<?php
/**
 * @RoutePrefix("/auth")
 */
class AuthenticationController extends \Phalcon\Mvc\Controller
{
    /**
     * @Get("/login", name="loginRoute")
     */
    public function loginAction() {
    }
}

So how can I tell Phalcon\Mvc\Url that the route "loginRoute" can be found in the AuthenticationController? Or is this what I'm doing super bad regarding the overall performance?

(The routes /auth/login and /test/routingTest/12 do work btw.)

Thank you, Toby



6.4k

Yeah, well, found the answer right after posting this: https://forum.phalcon.io/discussion/143/router-annotations-and-addresource Thanks anyway :)



98.9k

Also note that in production you must change the annotations adapter to Phalcon\Annotations\Adapter\Files or Phalcon\Annotations\Adapter\Apc to improve performance



6.4k

That's a very good point, thank you! Unfortunately I just started using Phalcon yesterday - and I couldn't find how to change the annotations adapter for the router, neither in the tutorials nor in the API. Do you have a hint for me? Do I have to implement my own RouterInterface or is there an easier solution?



6.4k
edited Oct '14

Ok next time I'll try a bit harder before posting :)

Solution:

  1. Overwrite annotations service in DI
$di['annotations'] = function() {
    return new \Phalcon\Annotations\Adapter\Apc();
};
  1. Stumble upon issue https://github.com/phalcon/cphalcon/issues/562

  2. Update to branch 1.2.0

  3. Face
....] Restarting PHP5 FastCGI Process Manager: php5-fpm<br />
<b>Warning</b>:  PHP Startup: Unable to load dynamic library '/usr/lib/php5/20100525/phalcon.so' - /usr/lib/php5/20100525/phalcon.so: undefined symbol: phalcon_get_uri in <b>Unknown</b> on line <b>0</b><br />
. ok
  1. Go to lunch and try to find a 1.2.0 revision that works later :)


6.4k
Accepted
answer
  1. Checkout revision 9911f5a2dea4536898e5804bdf50d01a78374929

  2. rebuild phalcon and restart php-fpm

  3. works with APC :)

I'll just leave this here in case someone else stumbles upon a similar problem.