子文件夹结构的Phalcon Router和Loader越来越大.如何设置? [英] Phalcon Router and Loader for subfolder structure getting bigger. How to set up?
问题描述
我有一个很大的项目需要编程.我曾经使用CodeIgniter进行编码,但是由于他们停止维护框架,因此我决定切换到另一个框架.我选择了Phalcon框架.接下来是我要实现的应用程序的文件夹结构:
I have a quite big project that I need to program. I used to code with CodeIgniter but since they stopped maintaining the framework, I decided to switch to another. I chose Phalcon framework. The folder structure of the application that I want to implement is next:
- 应用/
- 控制器/
- 管理员/
- 用户/
- UsersController.php
- UserGroupsController.php
- app/
- controllers/
- admin/
- users/
- UsersController.php
- UserGroupsController.php
- AnotherSubFolderController.php
- 设置/ SettingsController.php
- 仪表板/
- 流量/
- 另一个子文件夹/
- AnotherSubFolderController.php
- settings/ SettingsController.php
- dashboard/
- flow/
- another_subfolder/
- AnotherSubFolderController.php
- 客户/
- CustomersController.php
- CompaniesController.php
- customers/
- CustomersController.php
- CompaniesController.php
- sub_folder_structure///与控制器相同
这只是应用程序文件夹结构的示例.此项目中将有很多文件夹和子文件夹,以使其易于管理.
This is just example of the folder structure of the application. There will be quite a lot of folders and sub-folders in this project to make it manageable.
根据我的理解,我需要在加载器中为每个子文件夹注册所有名称空间,以便Phalcon知道在哪里搜索和加载类.
From my understanding I need to register all namespaces at the loader for each sub-folder, so that Phalcon knows where to search and load the class.
//Register an autoloader $loader = new \Phalcon\Loader(); $loader->registerNamespaces(array( // Main 'Controllers' =>'app/controllers/', 'Models' =>'app/models/', // Admin Routing 'Controllers\Admin'=>'app/controllers/admin', 'Models\Admin'=>'app/models/admin', 'Controllers\Admin'=>'app/controllers/admin', 'Models\Admin'=>'app/models/admin', 'Controllers\Admin\Users'=>'app/controllers/admin/users', 'Models\Admin\Users'=>'app/models/admin/users' ))->register();
加载程序看起来会很大.
The loader will look quite big.
然后,我必须设置路由器,以便将请求重定向到正确的控制器.现在我有下一个:
Then I have to set-up the router so that requests redirected to the right controller. Right now I have next:
// Setup Router $di->set('router', function(){ $router = new \Phalcon\Mvc\Router(false); $router->removeExtraSlashes(true); $router->add('/', array( 'namespace' => 'Controllers', 'controller' => "login" )); $router->add('/:controller/:action/', array( 'namespace' => 'Controllers', 'controller' => 1, 'action' =>2 )); $router->add('/admin/:controller/:action/', array( 'namespace' => 'Controllers\Admin', 'controller' => 1, 'action' =>2 )); $router->add('/admin/users/:controller/:action/', array( 'namespace' => 'Controllers\Admin\Users', 'controller' => 1, 'action' =>2 )); return $router; });
如果我需要为每个子文件夹和名称空间手动设置路由器,那也将非常大.
That will be also very big if I need to manually setup router for each sub-folder and namespace.
所以我的问题是:
- 有什么方法可以使装载机更漂亮,更小?随着它变得越来越大.
- 如何设置路由器,从而无需输入所有名称空间和子文件夹的组合?有什么办法可以缩小尺寸?可能是修改调度程序或其他任何类?还是有办法在路由器中将路径变量设置为控制器的位置?
我已经研究了Phalcon文档,但找不到解决方法.
I have researched Phalcon documentation but could not find the way to do that.
非常感谢您的帮助和建议.
Any help and suggestions are very much appreciated.
谢谢
推荐答案
Ad. 1.您可以将名称空间更改为更多PSR-0(在我看来),所以我会做:
Ad. 1. You can change your namespaces to be more PSR-0 (in my opinion), so I would make:
- 应用
- 控制器
- 管理员
- 用户
- UsersController.php
- app
- controllers
- Admin
- Users
- UsersController.php
您可以注册一个名称空间
Admin
或任何其他名称空间.然后,您只需要注册最顶层的命名空间即可工作(请注意,您的UsersController
必须具有namespace Admin\Users\UsersController;
才能工作). Thena自动装带器应仅具有: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 yourUsersController
must havenamespace 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) ) );
我正在使用
registerDirs
,所以我只将加载程序指向存在某些名称空间的文件夹.I'm using
registerDirs
so I only point loader to the folder in which some namespace exists.广告. 2.
为此,您可以使用路由组,因此您可以将名称空间作为构造函数的config数组的参数传递,然后在一个位置执行重复性任务.然后只需创建具有不同参数的新实例;
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'));
因此在MahGroup类中可能是:
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... } }
然后配置路由:
$router->addGroup( new MahGroup(array('prefix' => 'mah-namespace', 'namespace' => 'Mah\\Namespace' )) ); $router->addGroup( new MahGroup(array('prefix' => 'mah-other-namespace', 'namespace' => 'Mah\\Other\\Namespace' )) );
但是给出第二个问题的例子就是可以做什么.我通常为每个命名空间创建
Group
类,然后声明我的应用程序使用的一些路由,因为我没有为路由使用英文名称,我需要将波兰语URL重写为也有一些命名空间的控制器.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.这篇关于子文件夹结构的Phalcon Router和Loader越来越大.如何设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- Users
- Admin
- controllers
- 用户
- 管理员
- 控制器
- users/
- admin/
- controllers/
- 用户/
- 管理员/
- 控制器/