Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

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



4.3k

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



81.7k

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



4.3k

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?



4.3k
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 :)


4.3k
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.