多个action.class.php
我确实有一个模块,例如帐户。当然,您会在 acount/actions/action.class.php 中找到一个名为的文件。
PHP 文件action.class.php 变得越来越大。可以延长吗?
举个例子:
/account/action/action.class.php
/account/action/action2.class.php
如果可能的话,action.class.php和action2.class.php中的代码是什么样的?和/或我应该在配置 ymal 文件中的哪里输入内容?
提前致谢!
克拉猎手
I do have an module e.g. account. Of course you will find a file called in acount/actions/action.class.php.
The PHP-file action.class.php is getting big. Is it possible to extend it?
for an example:
/account/action/action.class.php
/account/action/action2.class.php
If it is possible, how does the code look like in action.class.php and in action2.class.php? And/or where should I enter something in a config-ymal-file?
Thanks in advance!
Craphunter
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Symfony 操作可以以两种方式声明:
两者基本相同,但不能混合使用(您必须决定是否想要123123 个文件,每个操作 1 个,或一个大文件)。
这是有关此问题的 symfony 参考: Symfony 前端控制器 - 操作 检查操作部分。
Symfony actions can be declare in two flavors:
The are both basically the same but these cant be mixed (you must decide if you want 123123 files, 1 per action, or one big fat file).
Here its the symfony reference on this matters: Symfony Front Controller - Actions check the Action section.
听起来动作类变得太大了,因为里面的东西太多了。我建议将其分解为多个模块或将逻辑代码的相关部分移动到您的模型中。
为 action2 文件添加 include 并不是 Symfony 1.4 的预期用途,而且很可能会导致各种其他令人头疼的问题。
It sounds like the action class is getting too big because you have too much stuff in it. I would suggest breaking it into multiple modules or moving relevant parts of the logic code into your models.
Adding an include for an action2 file is not how Symfony 1.4 is intended to be used and will likely just lead to all kinds of other headache.
PHP 不支持像 C# 那样的“部分类”。因此,您有两个选择:
通过创建继承自
sfAction
的baseActions
类来创建继承链。然后创建继承自baseActions
的MainActions
类。您可以添加不同的继承层。将函数分组到不同的类中。覆盖操作文件中的
execute()
函数,并手动调用所需类上的函数。PHP doesn't support 'partial classes' like C#. So you have two options:
Create an inheritance chain, by creating a
baseActions
class which inherits fromsfAction
. Then create yourMainActions
class which inherits frombaseActions
. You can add different layers of inheritance.Group functions in seperate classes. Override the
execute()
function in your actions file, and manually call the function on the class you need.还要考虑您在操作(控制器)中实现的某些行为是否属于模型(或视图)。如果确实属于该操作,只需遵循 guiman 的建议并为每个继承
sfAction
的类创建一个操作。如果我有一个基于模型(由 CLI 生成)的 CRUD 模块,并且我必须为该模型定义其他行为,我倾向于创建另一个模块来包含这些行为。例如,考虑一个模型
文章
。我可以为 CRUD 行为(生成)创建一个article
module,为审核、激活等其他行为创建article_support
,也许还有article_ajax
用于异步请求。Also consider if some of the behavior you're implementing in the action (controller) belongs to the model (or the view) instead. If the really belongs to the action just follow guiman's advice and create an action per class inheriting
sfAction
.If I have a CRUD module based on a model (as generated by the CLI), and I have to define additional behaviors for that model, I tend to create another module to contain that behaviours. E.g., considering a model
Article
. I could create anarticle
module for the CRUD behaviors (generated),article_support
for additional behaviors like moderation, activation, etc. and perhapsarticle_ajax
for async requests.