Welcome Guest, Not a member yet? Register   Sign In
Codeigniter URL Issue
#1

[eluser]Nakul Khandelwal[/eluser]
Hello.

I have a logical issue to move on with a big project.

I have 4 user classes:
1. Dean
2. Faculty
3. Students
4. Administration

I an using cookies to store their data and user classes. My issue is basically with the URLs.

Problem:
My default controller is called 'Auth'. Whose index function is the login page. What I am doing right now is that I have separate controllers for dean, student, faculty and admin. When the login is successful the user is redirected to the specific controller and the URLs become:

localhost/dean, localhost/faculty etc...

What I wan't to do is to have the URL 'localhost' even after login. I think I can achieve it with the 'views' with if/else statements for all 4 user classes.

The main issue is: When a student wants to open his profile then the URL is localhost/student/profile. (Student controller, method is profile) and for faculty it is localhost/faculty/profile etc..

I wan't the URL's to be localhost/profile for every user class. This can be solved by using a single controller which will be default and having a big code in that controller only.


Is there a way I can implement the above using different controller for each user class? Maybe hiding the controller part from the URLs and showing just the method.
#2

[eluser]weboap[/eluser]
check the routing lib
http://ellislab.com/codeigniter/user-gui...uting.html
#3

[eluser]Nakul Khandelwal[/eluser]
Already did.

How can I route a single URI to different controller on the bases of cookie data? Can I use If/else and code for getting data from cookies in the routes? The examples are too basic.
#4

[eluser]weboap[/eluser]
suggestion:

2 controllers

home.php been your default controller
and profile.php

depending on the cookie or session var, or whatever you are using to pass data to the controller your show the proper view.

with the same logic

when the user click profile

use 2nd controller /profile

depending of the session info or cookie or ..... show content of dean, student or admin......
unifying the CRUD there will simplify it for you, forget just for the moment the labels "admin, students,...." and deal with them as db group members that need to be dealt with on permission based and specific views.

you keep: http://localhost/ for all classes
and http://localhost/profile


#5

[eluser]Nakul Khandelwal[/eluser]
Thats a great idea. Thanks.

I create a Profile Controller. That is fine for profile.

But I have like 8-10 methods for each controller viz. dean, admin, faculty and admin. Some of them are common and some are unique to a particular user class. Like the method "CreateCourse" is just for faculty at the moment and the URL is localhost/faculty/CreateCourse.

So by your suggestion I should create a controller for each unique method of the existing controllers?
#6

[eluser]weboap[/eluser]
i don't know much about your application, but it normally should be a method for every task including unique ones. unless somebody have a better idea.

students shouldn't have access as group to createcourse by permission

EDIT: Note that this is just a suggestion somebody may have a better idea on how to implement your system, ideally its better to split controllers for each class (dean,admin....) separate the models, views... and put the common or redundant method in one controller or MY_Controller... but its up to each devlopper to judge what will be better for his or her app.
#7

[eluser]Nakul Khandelwal[/eluser]
Right now it is like this:

Controllers:
1. Admin
2. Auth
3. Dean
4. Faculty
5. Students

Example Controller:
Code:
<?php
class Faculty extends CI_Controller {

public function __construct()
{
  parent::__construct();
  
  if (!$this->ion_auth->logged_in())
    {
     redirect('auth/login');
    }
  elseif(!$this->ion_auth->in_group('faculty'))
  {
   redirect('auth/acessdenied');
  }
}

public function index()
{
  ........
}

public function viewstudents()
{
  ........
}

public function viewcourse()
        {
  ........
}

public function editcourse()
        {
  ........
}

More functions follow... Same for every the other controllers which are dean, admin and students.

Following is a part of the Login method of my auth controller:

Code:
function login()
{
  $this->data['title'] = "Login";
  $data['group'] = 'Home';
  $data['nameli'] = 'Not Logged In !';
  $this->form_validation->set_rules('identity', 'Identity', 'required');
  $this->form_validation->set_rules('password', 'Password', 'required');

  if ($this->form_validation->run() == true)
  {
   $remember = (bool) $this->input->post('remember');

   if ($this->ion_auth->login($this->input->post('identity'), $this->input->post('password'), $remember))
   { //if the login is successful
    //redirect them specific controllers
    if ($this->ion_auth->in_group('dean'))
      {
       redirect('dean','refresh');
      }
    elseif ($this->ion_auth->in_group('members'))
      {
       redirect('students','refresh');
      }
      elseif ($this->ion_auth->in_group('faculty'))
        {
         redirect('faculty','refresh');
        }
        elseif ($this->ion_auth->in_group('admin'))
          {
           redirect('admin','refresh');
          }
  
   }
   else
   { //if the login was un-successful
    //redirect them back to the login page
    $this->session->set_flashdata('message', $this->ion_auth->errors());
    redirect('/');
   }
  }
  elseif........




Theme © iAndrew 2016 - Forum software by © MyBB