如何使用 Symfony 1.4 避免大型操作类

发布于 2024-11-02 04:55:22 字数 516 浏览 1 评论 0原文

目前正在处理一个项目,但感觉代码的结构/解耦/可维护性不够好。

我认为主要问题是使用一个操作类来完成系统的主要工作。但还有什么选择呢?我的其他模块非常基本且独立。

如果模型包含更多我在控制器中拥有的业务逻辑(此时相关操作类的模型文件或多或少是空的,表模型文件也没有太多)。

我正在阅读 http://www.slideshare.net/nperriault/30-symfony-最佳实践有一些优点,其中之一是避免使用大型操作类,但并没有真正告诉您应该如何完成。

我是否应该分解我的动作类并在 /apps/frontend/module/myModule/lib/ 中有不同的文件。 ?

问题是我有另一个动作类,它必须重用上面提到的类中的一些功能......我真的不想有重复的代码,并且会陷入一些混乱。

一些一般性的指示会很好​​,谢谢

working on a project at the minute and don't feel the code is as well structured/decoupled/maintainable as it should be.

I think the main problem is using one actions class to do the brunt of the work of my system. But what are the alternatives? My other modules are pretty basic and separate.

Should the model contain more business logic that I have in my controller (at the minute the model file for the actions class in question is more or less empty, the Table model file doesn't have much either).

I was reading http://www.slideshare.net/nperriault/30-symfony-best-practices which has some good points, one of them being avoiding having large actions classes but doesn't really tell you how it should be done.

Should I break up my actions class and have different files in /apps/frontend/module/myModule/lib/. ?

The problem is I have another actions class that has to reuse some functionality from the class mentioned above... I really don't want to have duplicate code an am getting into a bit of a mess.

Some general pointers would be great, thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不醒的梦 2024-11-09 04:55:22

您可以将所有操作保存在一个文件中,也可以将每个操作放入其自己的文件中。

所以,或者

class mymoduleActions extends sfActions
{
  public function executeIndex($request)
  {
    // ...
  }

  public function executeList($request)
  {
    // ...
  }
}

或者

class indexAction extends sfAction
{
  public function execute($request)
  {
    // ...
  }
}

加上

class listAction extends sfAction
{
  public function execute($request)
  {
    // ...
  }
}

这一切都是 在文档中。然后,您应该将可重用代码放入两个操作类都可以扩展的父 XYZAction 类中。

You can either keep all actions in one file, or put each action in its own file.

So, either

class mymoduleActions extends sfActions
{
  public function executeIndex($request)
  {
    // ...
  }

  public function executeList($request)
  {
    // ...
  }
}

or

class indexAction extends sfAction
{
  public function execute($request)
  {
    // ...
  }
}

plus

class listAction extends sfAction
{
  public function execute($request)
  {
    // ...
  }
}

It's all in the documentation. You should then put reusable code in a parent XYZAction class that both action classes can extend.

街道布景 2024-11-09 04:55:22

您始终可以将代码放入 apps/frontend/lib 中。创建具有可重复使用的代码的类,您只需从操作中调用方法即可避免重复

You can always put code in apps/frontend/lib. Create classes that have the code that may be re-used and you can just call the methods from within your actions to avoid duplication

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文