嵌套控制器
有没有办法拥有嵌套控制器或至少看起来像那样,例如在 codeigniter 中。
/admin/controller
其中 admin 是控制器文件夹内的目录。
/admin/users_controllers.php 你把所有东西都放在那里。
我知道我可以通过路线来实现这一点,但只是好奇我是否可以为应用程序的架构做到这一点。
Is there a way to have nested controllers or at least to looks like that,like in codeigniter eg.
/admin/controller
where admin is a dir inside Controllers forlder.
/admin/users_controllers.php where you put everything in there.
I know that I can achieve this with routes, but just curious if I can do this for architecture of app.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为以这种方式组织事物会混合控制器和身份验证逻辑。在 CakePHP 中,您的操作按控制器分组,而控制器通常与模型相对应。因此,您将拥有用户、帖子、产品或其他内容的控制器。
然后,对于每个控制器,某些操作可能仅对具有特定权限的用户(例如管理员)可用。您不应该创建单独的控制器来区分权限级别,而应该根据操作所针对的数据对操作进行分组。这是
Auth
组件的任务,可能与其他组件(例如Acl
)一起工作,以授予或拒绝对每个控制器中单个操作的访问。如您所知,您可以使用路由通过
admin
为需要管理员权限的操作添加前缀。这为操作提供了一个单独的入口点,但管理逻辑的控制器仍然保持不变。所以你的问题的答案是:你不应该。这不是控制器的用途;控制器用于对同一对象上的操作进行分组,而不是对需要不同访问级别的操作进行分组。
I think that organizing things this way mixes controller and authentication logic. In CakePHP your actions are grouped by controller, and controllers are usually in correspondence with models. So you will have controllers for users, posts, products or whatever.
Then, for each controller, some actions may only be available to users with certain privileges, for instance admins. You should not create a separate controller to distinguish the privilege level, but rather group your actions according to the data they act opon. It is the task of the
Auth
component, possibly working together with other components such asAcl
, to grant or deny access to the single actions in each controller.As you already know, you can then prefix actions which need admin privileges by
admin
using routing. This gives a separate entry point for the action, but the controller managing the logic reamins the same.So the answer to your question is: you shouldn't. This is not what controllers are for; controllers are for grouping actions on the same objects, not to group actions requiring different access levels.