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.

Is it possible to customize controller's name?

Hello,

Is it possible to customize controller names and make dispatcher understand these names? Here is the sample of what I'm looking for:

class App\Controller\Admin\Profile\Simple path app/controllers/Admin/Profile/Simple.php webpath /admin/profile/simple/action

App\Controller\Admin\Profile\Advanced path app/controllers/Admin/Profile/Advanced.php webpath /admin/profile/advanced/action

If yes - then how should I write custom routes and generate url using UrlResolver?

Thank you!



4.8k

Of course, we can also add Controller suffix to classnames for security reasons. class App\Controller\Admin\Profile\AdvancedController path app/controllers/Admin/Profile/Advanced.php webpath /admin/profile/advanced/action



83.4k

Class names must have the same name as their files: AdvancedController -> AdvancedController.php. Note that in Phalcon, controllers are loaded using auto-loaders, if the name of the class is different than the file, the autoloader wouldn't load the class. If you're using namespaces, you can remove the suffix: Advanced -> Advanced.php



4.8k

Thank you for answer. So, as far as I understand it is impossible to split controllers among folders, right?

You can, you just need to write proper Router. I use Router Annotation for this:

$router->addResource('Namespace\Controller\Folder\Homepage', '/homepage'); $router->addResource('Namespace\Controller\User\Settings', '/user/settings');



4.8k

Thank you, Tomasz. Never heard of annotations. What about performance when using annotations? What caching driver is the fastest one?



5.4k
Accepted
answer

addResource is pretty fast because second parameter is prefix which decide if parse controller for example

$router->addResource('Namespace\Controller\Folder\Homepage', '/homepage'); $router->addResource('Namespace\Controller\User\Settings', '/user/settings');

request /user/settings/password will parse only annotations in Namespace\Controller\User\Settings

request /homepage will parse only annotations in Namespace\Controller\Folder\Homepage

You can find more information on documentation about annotation router http://docs.phalconphp.com/en/latest/reference/routing.html#annotations-router

About caching.

Annotation Router use Annotation Parser from Dependency Injection Container. Default Annotations Adapter is Phalcon\Annotations\Adapter\Memory you can of course change it to Phalcon\Annotations\Adapter\Apc (of course you must have APC installed)



4.8k

Tomasz, thank you again for explanation. You're saying that when requesting "request /user/settings/password" only Namespace\Controller\User\Settings will be parsed for annotations - it is clear for me. But what about building URLs. If Namespace\Controller\User\Settings is parsed only, then Url service will know nothing about other routes and it is a problem.

For example, I want to build url from Namespace\Controller\User\Settings to some action in Namespace\Controller\Folder\Homepage. But Url know nothing about annotations in Namespace\Controller\Folder\Homepage and due to this fact it is impossible.

Thank you in advance.

Sorry but I'm not using named routes. My apps router is simple. Every time I need to created specific url for example for displaying Static Page I create method in Entity (Model)

class Page
{
  public function getUrl()
  {
      return sprintf('/category/%s/page/%s', $this->getCategory()->getSlug(), $this->getSlug());
  }
}

Annotations Router support named routes, so there should be way to use them. I hope someone else will help you.