As you might have guessed I have a problem concerning the SRP
principle of OOP. I'm trying to build my own framework (just for the
sake of learning and maybe using it for my own website), I would like
to keep it simple but effective. For this I use the MVC pattern.
My classes work well, but it isn't perfectly yet. I do use single
entry (.htaccess -index.php) after which the router does reroute it
to the proper controller.
I have seperated the view and controller quite well, but I think my
controller isn't really perfect yet.
Now I wonder if there is anyone that can help me by showing how to use
the SRP principle properly.
What I have now (my user class) looks simplified like this:
if the uri is like this: example.com/user/action
The user class gets loaded, the constructor sees there is an action
and sets it in a variable within the object. The method index() is
called. And this handles the action:
<?php
// this is in the index() within a switch($action)
case 'login':
if(!$this->_action_block['login']){
if($this->check_action_allowed()){
if($_SESSION['signed_in'] == 0){
if($this->finalize){
$data = $this->get_data('login');
$checked_data = $this->check_input('login',$data);
if($checked_data === TRUE){
if($this->login($data)){
header('location: ' . $this->register-
>get('base_uri') . $data['location']);}else{
$this->view['html']-
>display_result('login_failed');}
}else{
// login data incorrect, log login-try to database
to be able to block ip adressess
$this->log_failed_login();
$this->view['html']->create_form('login',
$checked_data);
}
}else{
$this->view['html']->create_form('login');
}
}else{
$this->view['html']->display_result('logout_first');
}
}else{
$this->view['html']->display_result('unauthorised');
}
}else{
$this->view['html']->display_result('action_blocked');
}
break;
case 'logout':
//etc
break;
case 'create':
// etc.
break;
case 'edit':
// etc.
break;
case 'delete':
break;
?>
As you can see the user class handles the actions, however after
reading about SRP I see that this is not exactly perfect. Apparently
it is better for the user class to contain the user data and give it
back using methods like getUserName() or isLoggedIn().
In my controller I also have all the SQL which should be in a model,
according th what I read.
Now I wonder where do I actually handle all those actions like
login(), create(), edit(), etc.? I think this should be another class
but I don't really know how to do this perfectly by "seperation of
concerns".
Anywone who can explain me how this is done (or maybe show me a
model)?
Thanks