I am creating a shopping cart in a MVC file structure and I have a shopping cart controller which has a group of functions for the frontend and backend.
Class ShoppingCartController{
//frontend function
public function viewCart(){
//do something
require 'view/viewCart.php';
}
//backend function
public function viewOrders(){
//do something
require 'view/viewOrders.php';
}
}
$controll = new ShoppingCartController();
if(isset($_GET['action']) && in_array($_GET['action'], get_class_methods($controll))){
call_user_func(array($controll, $_GET['action']));
}
What I want to do is guard against anyone from the frontend being able to call a backend function. So I thought I would set the functions to protected and write two extended classes to regulate permissions. Like so
Class ShoppingCartController{
//frontend function
protected function viewCart(){
//do something
require 'view/viewCart.php';
}
//backend function
protected function viewOrders(){
//do something
require 'view/viewOrders.php';
}
}
Class ShoppingCartFrontendController Extends ShoppingCartController{
//frontend function
public function viewCartExtended(){
//do something
$this->viewCart();
}
}
Class ShoppingCartBackendController Extends ShoppingCartController{
//backend function
public function viewOrdersExtended(){
//do something
$this->viewOrders();
}
}
Is that the way everyone else would do it or is there a better way?