I have a problem with the organization of views on CodeIgniter.
Project: Create a simple web application that permits to manage a library(bookcase).
I created 3 models : Member, Categories, Books with their respective controllers.
I implemented the Member model with its controller.
In the member_controller we have:
public function login(){
$this->load->helper('form');
$this->load->helper('email');
$this->load->library('form_validation');
$mail = $this->input->post('mail');
$pass = $this->input->post('pass');
$data['mail'] = $mail;
$data['pass'] = $pass;
$this->form_validation->set_rules('mail', 'mail', 'required');
$this->form_validation->set_rules('pass', 'pass', 'required');
if ($this->form_validation->run() === TRUE)
{
$result=$this->membre_model->login($mail,$pass);
if($result==TRUE){
$this->load->view('templates/header.php');
$this->load->view('membre/logged',$data);
$this->load->view('templates/footer.php');
}
Once user is logged, I want to show all existing categories he previously created(so get them from the database).
Finally, the thing I don't get is how the different controller communicate between them.
How can I do that?
You would have to add an exception when loading each page that will redirect user based on their login status. In other words, if user is logged in, bring him to the application. If user does not exist, redirect him to the registration page.
This can be done by verifying if the user is logged in based on stored session values. If this session is stored than you can let the user view the page. Here is a great tutorial explaning a simple Login system for Codeigniter.
http://www.codefactorycr.com/login-with-codeigniter-php.html
In my opinion, I usually use an Authentication library to simplify the Login system for my application. I would use Ion Auth, it has a great documentation explaning all the functions you can use.
Do I have to call a function of the category controller in the login function of the member controller?
You can simply use the category controller as you would normally. You would change the pages information or redirect the user out based on the session information stored in their browser.
Do I have to load the category view from the Login function?
You would load the category view from the category controller. The login controller would redirect the the user to the category controller after the login is performed.
Do I have to build the site from just one controller(the member controller)?
No, you can have as many controllers as you like.
How to build the webapp with differents views of differents controllers?
A controller would represent a section of your site. This controller would load multiple views for different things. Here is a great little tutorial explaning the MVC worklow. This will help you understand the process.
You can call one controller from another using the URI, so for example you might have an entry like this in your routes file:
$route['books/get_books_by_user/(:any)'] = "books/get_books_by_user/$1";
you could then call form your login controller:
redirect('books/get_books_by_user/'.$user_id)
you would then handle this in a get_books_by_user method in your books controller. For example using:
$user_id = $this->uri->segment(3);
$collection = $this->books_model->get_books_by_user($user_id);
The problem you have is that if you dont want anyone other than the user to see their own book list you have to check the user is logged in from you books controller. This is why most user auth scripts are presented as a library which can be accessed from any controller. If your auth is not laid out like this you could store user details as session data and check it from there, this would mean you wouldnt have to pass the username via the uri. Check out session docs here for details: http://ellislab.com/codeigniter/user-guide/libraries/sessions.html