I have some dilemma about controller usage. Like usualy, router (dispatcher or front controller) calls some static method in controller class, eq PageController::showIndexPage(). My controllers usualy have several lines of code and that is usualy model loading, getting data and passing it to view.
My question is – because only one method is executed per http request, should I avoid classes and just create a single function for controller? Sometimes, I realy have several methods per controller, but every time only one method is in usage. I know that is not big deal, but maybe I can get better system? Here is example: My AuthController have methods like showLoginPage(), doLogin(), doLogout()… So, maybe is better idea to avoid class, and put write this controller as several functions in separated files, eq auth/show_login_page.php, auth/do_login.php, and so on? Does this concept have some pros/cons?
Update: Because some users bloat my usage of static method, I’m must defence 🙂 I do not create instance of controller, because there is no need for that. In 99% of cases controller is only used to pass data from model to view. And, there is no need for instance creation for only one method call.That is reason why method is static. Here is example of one of my controllers:
class ArticlesController {
static function showArticle($article_id) {
$article = ArticlesModel::getArticleById($article_id);
View::getInstance()->assignByRef("article", $article);
View::getInstance()->display("articles/one.tpl");
}
static function showAllArticles() {
$articles = ArticlesModel::getAllArticles();
View::getInstance()->assignByRef("articles", $articles);
View::getInstance()->display("articles/all.tpl");
}
}
Static class functions are just pretty like global functions, so no idea in specific why you are using static class functions for your controllers (which is pretty akward), so either change them to standard object methods or global functions altogether.
I suggest you branch your whole app into one branch that replaces all controllers with global functions and the other branch with non-static class functions. Then you can better compare both concepts.
If you don’t wanna play around and you’re asking for pointers, convert all static class functions into non-static functions, then go on until you run into the next problem.