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:
- As you already did, combine your CRUD operations in a single controller (AdminController)
- 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) - Then at every action you receive as first parameter the model type like
list/city
and if needed the id as second paremeter likelist/city/2
. - 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.