Skip to content

Refactoring

Adham Saad edited this page Apr 19, 2016 · 4 revisions

Fat Controllers

Almost all of our controllers are getting fat , either a lot of code existing in each action function or there are functions that are not actions inside the controller .

To get over this , we will introduce a new type of models/services in each module called "Workflow" . Workflow class will have 3 main functions

1- init($params) init function will be responsible of initializing the workflow class with required parameters

2- process process function will be responsible of making the main functionalities required by this action ( dealing with the database , sending email ... )

3- prepare prepare function will be responsible of preparing the main data that will be sent to the view . prepare function will be optional as a contorller action can redirect to another action and not display anything .

So , a normal controller action can follow the following example

$workflow = $this->getServiceLocator()->get('ModuleName\Workflow\WorkflowName');

$workflow->init($options)

$workflow->process()

return $workflow->prepare();

Mustache Helpers

Adding helpers to mustache configuration needs to be changed . This is happening now in CustomMustache\Service\RendererFactory::createService .

  • For each helper , a new class will be created following similar to this class ( Translation\Helper\TranslatorHelper )
  • We will add the available helpers ( classes ) in a config array in module.config.php file in CustomMustache . The array will be key to value , while the key is the mustache key and the value is the class name