问题
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
回答1:
Symfony actions can be declare in two flavors:
- A big actios.class.php file that inherit from sfActions
- 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.
回答2:
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.
回答3:
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.
回答4:
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.
来源:https://stackoverflow.com/questions/6843046/multiple-action-class-php