Organize web application with codeIgniter - php

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).
How can I do that?
Do I have to call a function of the category controller in the login function of the member controller?
Do I have to load the category view from the Login function?
Do I have to build the site from just one controller(the member controller)?
How to build the webapp with differents views of differents controllers?
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

Related

CakePHP: How to use a function with a model in every controller?

I have a project which includes admin and user section. Both section use the same controllers, just different functions and templates (ex: viewAdmin() and viewUser()). In function beforeRender() of every controllers, I set variable $admin as true for admin functions and false for user functions.
For authentication, I use Shibboleth. Shibboleth uses data from LDAP, while user types were saved in SQL-Database, that means while it can check if the login and password are false, it can't check if the user is admin or not. An user can go to ADMIN section as long as they use the right action (ex: go to the link http://example.com/tool/viewAdmin).
To prevent this, I will have to:
Load model Users
Compare the environment variable uid (login name) with the "login" columns in Users table in my SQL-Database
See the "type" column in Users table to know if user is admin or not.
Compare the result with value of $admin and redirect to an error page when necessary.
The problem is: I don't want to repeat those steps for EVERY controllers.
Currently I have 2 ideas:
Write a function in UsersController, and use it in every controllers.
Create a component and load it in every controllers.
Both methods require me changing code in all controllers. I would like to hear a better way with less work, perhaps by changing app.php or bootstrap.php.
Any suggestion is appreciated.
To share methods in CakePHP controllers you can do:
Create component and include in controller
Or create method in AppController and use it in child controllers
Or PHP way create Trait.
But when you authorize users, then all user data is stored in session, incl. is user roles (example admin, regular, member,.. )
Use the official CakePHP authentication plugin and extend the LDAP adapter with the additional code check you need. This is very easy to do and also a very clean way of solving the problem. Disclaimer: I'm one of the authors of the authentication plugin. https://github.com/cakephp/authentication
Or if you want to stay agnostic to any framework, use my library that is based on the authentication plugin and was decoupled from any framework but still works just nice with Cake https://github.com/Phauthentic/authentication.

How to make two different login systems in yii

I am new to yii. I just need two login page. One for Super-Admin and another for Sub-Admin. I created Super-Admin login page and it is successful. My Super-Admin path is 127.0.0.1/ticketing/adminLogin. My Sub-Admin path is 127.0.0.1/ticketing/login. I had created crud app for adminLogin page. Now i am using default login page for Sub-Admin. But i gave all the proper model name and actions. But my Sub-Admin login page is checking values from Super-Admin table and not from Sub-Admin table. Please help to fix this issue.
Hi as you said you have created a complete new crud for admin and sub admin then you can also create a new SubadminUserIdentity in components which extend CUserIdentity and in login model of sub admin use SubadminUserIdentityin authenticate and login function
Hi thanks for your reply. I got an answer. I made a silly mistake. I called the same authenticate function for Super-Admin and Sub-Admin. Now i make two authenticate functions and it works.

codeigniter - Two Controllers

I made a simple login system, with help of codeigniter. I have created a user section where user can see concern data(coming from DB) after login.
I made another controller and module for this but now I am confused How to use two controllers to manage this or if I can make only one model?
so how can I call this model on my home page where user gets redirected after login, please suggest your ideas on this confusion.
for login system i have these files
Controller (a) login.php (b) home.php (c) verfiylogin.php
Models (a) user.php
Views (a) home_view.php (b) login_view.php
For member section
Controller (a) newsletter.php
Models (a) newsletter_model.php
Views (a) home_view.php same home_view.php call via php
why do you make 2 controllers for this? login controller and verfiylogin?
in my opinion the best way to do it is to look at ion auth for codeingiter,create a model and fetch your members list after login in the user or the admin.
PS: LOL Your method is confusing....:))

COdeIgniter structure for login & registration

I am creating a login & registration system using CodeIgniter.
Currently I have a Model, View and Controller for login, with functions to validate,
check username, etc and an registration model, view and controller,
that does the registration.
I have chosen to separate the login and registration as a principle.
So right now i need to include functions to edit profile, and to check if logged in or not, and to check the user's role, and I would like to know how can i best do this, i have planned creating a user model and controller(no view), the main user controller would have the methods call to model's, but however the methods(updateprofile,islogin,etc) would be in different models, for example in the login model.
So is this design good/bad? How can it be done better. I would appreciate your suggestion's.
I really find no problem with your application structure. Its how you write your code and how will it easily be to update it in the future. For managing your models try using an ORM. PHPActiveRecord is a good start. With this, you no longer be creating alot of individual functions for your database transactions. Reference
You can create a User_Model and expand it as needed. You can see this CI auth lib for example as how build login & registration structure in CodeIgniter.

Codeigniter - Authentication Library or Custom Controller?

I'm wondering what is best in my case. I'm building a site using CodeIgniter with two main sections:
the public part avalaible to everyone
the private one only for registered users
In each page of the public area (one controller) I want to put a sign in form and a sign up link and if the users is logged in he has to be redirected to the private area or a link to it may be shown.
Now I have two choices:
A user controller is the first thing I thought of but in each page of the site I need to control if the user is logged and this is impossible or very bad since I'm using another Controller
So I started working on a library but I'm not sure how implement it (for example form validation should be achieved by the controller or by the library itself?, what about database connection since I haven't a model?)
What do you think is the best? Why? and how would you implement it?
(and yes I like reinventing the wheel and not using an existing library mainly because i want to learn how to do it)
Super Controller
=>assign user data,settings,configs etc
|-----private controller extends super controller
=>check user credentials
|-----admin controller extends super controller
=>check user && admin credentials
Your super controller is your public controller as long as you only do assignments, no checking...
Anything you want public just extends super controller
Anything you want private extends private controller
Form validations and query jobs should be carried out with the controller itself. The library act like a tool no need to implement these things in them but as a need you can use queries in them to check some data but it's better to be worked in the controllers.
The idea for having a log flag is to:
When user is signed in, create a session for it to show the access.
Check every time the session for the private parts.

Categories