Question

I have many models in my project. If I'm following phalcon default convention, I will have to create separate controllers for each models. So if I have 30 models and I want to build simple crud page, then I will have to create 30 controllers as well. I'm using the same crud views for each controller

I route the crud to : /project/admin/:controller/:action. The application is well and running but the problems are :

  1. I have to manage every single controllers
  2. Most of these controllers share same methods and attributes, just different table and few validation

So I want to combine them into a single controller AdminController. Here, I listed all the crud-related actions for each models. For example, accountListAction, cityEditAction, etc. Well yes indeed that the page will become much longer (and IDE will took more time to check) but the result is, shared methods and attributes are reusable between each actions.

Now there are only one controllers. I can access 'list' of model 'account' through /project/admin/accountList. To make the routing looks better, I add more route to specific actions so now, I can access it through project/admin/account/list. (had to define route for every single actions)

The question is : Am I doing it wrong ? Is there any better method to do this ?

What I wanted to achieve is : less controller files, less redundancy

Was it helpful?

Solution 2

I think your approach is good but you just need to organize it a bit diferent to make your life easier. Maybe something like this:

  1. As you already did, combine your CRUD operations in a single controller (AdminController)
  2. Instead of creating a action for every combination of CRUD/Model (like cityEditAction), create one action for each CRUD and any aditional action you want (exportToCSVAction for example)
  3. Then at every action you receive as first parameter the model type like list/city and if needed the id as second paremeter like list/city/2.
  4. After this every action can be implemented to work with any model type. Here's a example of a deleteAction():

public function deleteAction($modelType, $id)
{
  if($this->modelTypeExists($modelType))
  {
    //Check if the user has the required privilegies for this action
    //See http://docs.phalconphp.com/en/latest/reference/acl.html
    if($this->acl->isAllowed($currentUser->accessGroup, $modelType, "delete"))
    {
      //Get the model being manipulated
      $model = $this->getModelById($modelType, $id);
      //Attempt to issue the required action
      if ($model->delete() == false) 
      {
        //Something went wrong with the deletation
      }
      else 
      {
        //Horray!!
      }
    }
    else
    {
      //Denied
    }
  }
  else
  {
    //Error
  }
}

NOTE: modelTypeExists and getModelById aren't built-in functions, they're just illustrative functions for the answer.

OTHER TIPS

if you have some methods, what you need in many controllers, you can make BaseController

class ControllerBase extends Phalcon\Mvc\Controller
{
    public function something_what_you_need_in_many_controller(){
    }
}

and then use it while creating controllers:

class PeopleController extends ControllerBase
{
}

so you will have that function something_what_you_need_in_many_controller() in PeopleController.

You don't have to create controller for every model. These 2 are different layers of application, you have controllers - they tells in what view put what data and it can use many models at once. Models are just to handle the connection of data between controller and database.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top