1. Overview
Previously, controllers (e.g., OrdersController) contained hundreds of actions and functions in a single file. This made the codebase difficult to read, maintain, and scale.
To improve structure, we have refactored the controller format.
Now, each controller:
Contains only core/major logic.
Delegates Actions and Functions to separate files inside their respective folders.
2. New Folder Structure
Controllers
│
├── OrdersController.php # Main controller file
│
├── Functions/
│ └── Orders/ # Folder for Orders-related Functions
│ ├── calculateInvoice.php
│ ├── validateOrder.php
│ └── …
│
└── Actions/
└── Orders/ # Folder for Orders-related Actions
├── createOrderAction.php
├── cancelOrderAction.php
└── …
2.1 Example Of File Code
Standard Code For Controller :
<?php
/**
* Sample custom action for Yii 1.x
*
* Usage:
* – Create file: protected/controllers/actions/Orders/TestAction.php
* – Access via URL: index.php?r=orders/test
*/
class TestAction extends CAction
{
/**
* This method is executed when the action is called.
*/
public function run()
{
echo “This is a test action running from ” . get_class($this->controller);
}
}
Sample Code For Function :
<?php
/**
* Sample function class for Yii 1.x
*
* Usage:
* – File: protected/controllers/functions/Orders/Test.php
* – Call: $this->runFunction(‘test’, [$model]);
*/
class Test
{
public $controller; // auto assigned by runFunction()
/**
* Executes custom logic
* @param mixed $model
*/
public function run($model)
{
echo “This is a test function running with model: ” . get_class($model);
}
}
3. Naming Conventions
To keep files consistent and easily identifiable:
Action Files → must end with Action
Example: createOrderAction.php, cancelOrderAction.php
Function Files → must use the exact function name only (without adding “Function” at the end).
Example:
-
calculateInvoice.php
(instead ofcalculateInvoiceFunction.php
) -
validateOrder.php
(instead ofvalidateOrderFunction.php
)
4. Responsibilities
4.1 Controller File (e.g., OrdersController.php)
Contains only the core logic of the controller.
Delegates execution to Actions and Functions.
Works as an entry point for handling the request.
4.2 Functions Folder
Path: /Controllers/Functions/{ControllerName}
Contains helper or utility functions used by actions.
Functions should be reusable across multiple actions.
Example:
calculateInvoice.php
validateOrder.php
4.3 Actions Folder
Path: /Controllers/Actions/{ControllerName}
Contains individual actions previously written inside the controller.
Each action is now in a separate file for better clarity and maintainability.
Example:
createOrderAction.php
cancelOrderAction.php
5. Benefits of New Format
✅ Readability – Each action/function is in its own file.
✅ Maintainability – Avoids controllers with hundreds of methods.
✅ Reusability – Functions can be reused across different actions.
✅ Scalability – Easy to add new features without bloating controllers.
✅ Clear Separation of Concerns –
Controller = Core logic
Actions = Request handling
Functions = Reusable helpers
6. Example Workflow
Request comes to OrdersController.
Controller decides which Action to trigger.
That Action file may call one or more Functions for processing.
Response is returned through the controller.
Example Flow:
User places an order → createOrderAction.php executes.
Inside action → calls validateOrderFunction.php and calculateInvoiceFunction.php.
Controller finalizes response.
📌 Important Note:
Whenever you create a new Action or Function, always follow the naming convention:
[ActionName]Action.php
[FunctionName].php
⚡ This is the standardized structure that must be followed across all controllers going forward