Well in my admin control panel controller classes there are methods such as index(), add(), edit(), delete(), settings(), info() and other methods dealing with various admin actions, but there is a design issue that I've been wondering what the best way is to resolve. Methods such as add(), edit(), delete() have various actions depending on whether a form has been submitted by an admin(since apparently, an admin can add, edit, delete contents). The traditional php approach is to write if..else block to check whether a form has been submitted, like this below:
class ACPUserController extends ACPController{
// properties...
public function __construct(){
// constructor code.
}
public function index(){
// show index action for user controller.
}
public function add(){
$input = Registry::get("input");
if($input->post("submit")){
// Code to handle admin form submission, updating data.
}
else{
// default code without form submission.
}
}
public function edit(){
$input = Registry::get("input");
if($input->post("submit")){
// Code to handle admin form submission, updating data.
}
else{
// default code without form submission.
}
}
public function delete(){
$input = Registry::get("input");
if($input->post("submit")){
// Code to handle admin form submission, updating data.
}
else{
// default code without form submission
}
}
public function settings(){
// code inside
}
public function info(){
// code inside
}
}
The problem is that I will have to test form submission condition over and over again, which is a bit cumbersome. Moreover, it makes each method of controller class overcrowded, bad OOP design. In some programming languages such as Java and C# with inner/anonymous class syntax, it is possible to define an inner class to handle form submission separately. Nonetheless, PHP does not support inner/anonymous classes so there's no way to accomplish this. Alternatively I could write a helper method called execute($action) that handles each action, but it can also get either quite big or inefficient.
What will you do with a problem like this? Do you use PHP's traditional approach though inefficient? Or do you have better ideas? Please lemme know if you have any comments or suggestions to make, I'd be really glad to hear and learn.