Phalcon Router and Loader for subfolder structure getting bigger. How to set up?

馋奶兔 提交于 2019-12-05 08:40:47

Ad. 1. You can change your namespaces to be more PSR-0 (in my opinion), so I would make:

  • app
    • controllers
      • Admin
        • Users
          • UsersController.php

The you can register one namespace Admin or any other. Then you need to register only the top most namespace to work (keep in mind that your UsersController must have namespace Admin\Users\UsersController; to work). Thena autoloader should have only:

 $loader
    ->registerDirs(
        array(
            // It's taken from my config so path may be different
            __DIR__ . '/../../app/controllers/'
            // other namespaces here (like for models)
        )
    );

I'm using registerDirs so I only point loader to the folder in which some namespace exists.

Ad. 2.

For this you can use groups of routes so you can pass a namespace as a parameter for config array of constructor and then do the repeative task in one place. Then just create new instances with different parameters;

$router->addGroup(new MahGroup(array('namespace' => 'Mah\\Controller'));

So inside MahGroup class could be:

class MahGroup extends Phalcon\Mvc\Router\Group {
    public function _construct($config = array()) {
       $this->setPrefix('/' . $config['perfix']);
       $router->add('/:controller/:action/', array(
          'namespace' => $config['namespace'],
          'controller' => 1,
          'action' => 2
       ));
       // etc...
    }
}

And then configuring routes:

$router->addGroup( new MahGroup(array('prefix' => 'mah-namespace', 'namespace' => 'Mah\\Namespace' )) );
$router->addGroup( new MahGroup(array('prefix' => 'mah-other-namespace', 'namespace' => 'Mah\\Other\\Namespace' )) );

But given examples for second question are just what could be done. I usually create Group class for each namespace and then declare some routes that my app uses since I'm not using english names for routes and I need rewriting polish urls to controllers which have also some namespaces.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!