Multiple action.class.php
Asked Answered
I

4

5

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

Investigate answered 27/7, 2011 at 10:46 Comment(1)
Are you following the MVC pattern? Would be odd to end up with too much code if you are. Refactor rather than splitting across multiple class files like this.Jelsma
A
7

Symfony actions can be declare in two flavors:

  1. A big actios.class.php file that inherit from sfActions
  2. Multiple xxxAction.class.php file that inherit from sfAction

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.

Aracelis answered 27/7, 2011 at 13:4 Comment(1)
Your link points to the docs of the outdated 1.0 version. Here's the link for symfony 1.4: symfony-project.org/gentle-introduction/1_4/en/…Heteropterous
U
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.

Uncinus answered 27/7, 2011 at 11:58 Comment(0)
P
1

PHP doesn't support 'partial classes' like C#. So you have two options:

  • Create an inheritance chain, by creating a baseActions class which inherits from sfAction. Then create your MainActions class which inherits from baseActions. 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.

Phocaea answered 27/7, 2011 at 10:51 Comment(0)
D
1

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 an article module for the CRUD behaviors (generated), article_support for additional behaviors like moderation, activation, etc. and perhaps article_ajax for async requests.

Dieppe answered 27/7, 2011 at 15:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.