I know there is a lot of debate about which PHP framework is the best and this isn't a thread about this ;). This thread is about how do developers handle the separation of concerns belonging to a controller.
In MVC-ish framework, the controller will serve pages (get) and accept form submission (post). However, I found that even trivial controller can be hard to understand.
With the following CodeIgniter example:
class TrivialController extends MY_CONTROLLER{
public function login(){
//Load a view containing a login form
}
public function login_p(){
//Load user model + login
//Success / Faillure view
}
}
or
class TrivialController extends MY_CONTROLLER{
public function login(){
if(GET){
//Load a view containing a login form
} else if (POST) {
//Load user model + login
//Success / Faillure view
}
}
}
I found both option to be equally cumbersome:
if-else
on the request type. It'll make the method harder to read.So, here's the question. How do you handle this in practice?
The GET/POST are two different, yet functionally cohesive things.
This is a very common workflow in web applications:
I consider these to be different enough to split the into different methods, but similar enough to keep in the same controller.
The problem is the naming convention. You should concisely distinguish between command and query actions, and make use of object-verb
names that describe the subject what you are working with, and what you are doing with it.
class TrivialController extends MY_CONTROLLER{
public function askCredentials(){
// Show login form
}
public function verifyCredentials(){
// Login
}
}