How fix controller loads "home()" method everytime

Hey actually I am new to codeigniter so,I make a controller Admins in thats controller has severals methods http://192.168.0.103:8080/Concept/admins/order problem is when I request this page its load home() method instead of order method here’s some picture of my page where you can see in url I change method but thats still call home()

here my controller code:

<?php 
/**
 * 
 */
class Admins extends CI_Controller
{
	public function __construct()
	{	
		parent::__construct();
		if (!$this->session->userdata('id')) {
			redirect('logins/index');
		}
		$this->home();
		
	}
	public function home($page='main')
	{
		if (!file_exists(APPPATH.'views/admin/'.$page.'.php')) {
			show_404();
		}
		$data['title'] = ucfirst($page);
		$this->load->view('templates/header');
		$this->load->view('templates/sidebar');
		$this->load->view('admin/'.$page, $data);
		$this->load->view('templates/footer');
	}


	public function order()
	{
		
		$this->load->view('templates/header');
		$this->load->view('templates/sidebar');
	 	$this->load->view('admin/order-list');
		$this->load->view('templates/footer');

	}
	public function new()
	{
		$this->load->view('templates/header');
		$this->load->view('templates/sidebar');
		$this->load->view('admin/new-site');
		$this->load->view('templates/footer');
	}
	public function logout()
	{
		$data = $this->session->all_userdata();
		foreach ($data as $row => $rows_value) {
			$this->session->unset_userdata($row);
		}
		redirect('logins/index');
	}
	
}

$routes[‘admin’] = ‘admins/home’;
$route[’(:any)’] = ‘admins/home/$1’;
$routes[‘admin/order’] = ‘admins/order’;
$route[’(:any)’] = ‘admins/order/$1’;
$route[‘registers’] = ‘registers/index’;
$route[’(:any)’] = ‘registers/index/$1’;

$route[‘default_controller’] = ‘logins/index’;
$route[’(:any)’] = ‘logins /view/$1’;
$route[‘404_override’] = ‘’;
$route[‘translate_uri_dashes’] = FALSE;```
type or paste code here

public function __construct()
{	
	parent::__construct();
	if (!$this->session->userdata('id')) {
		redirect('logins/index');
	}
	$this->home();
	
}

So whenever you invoke this class, it calls its parent constructor, then it checks if the user is logged in and forces a redirect, otherwise it calls $this->home() with no parameter.

public function home($page='main')

The home function has an optional parameter, which if undefined is set to ‘main’.

Your constructor calls home with no parameter, so $page will always be ‘main’.

1 Like

oohh thanks :innocent: but one more thing I need to ask you as you saw the way of my work can you tell how should I redirect this home() method only first time when user log in ?

I supposed you’d do that in the Logins controller; on correct login redirect them to the home page.

look here’s is my login controller

<?php 
/**
 * Logins from  
 */
class Logins extends CI_Controller
{
	
	public function __construct()
	{
		parent::__construct();
		$this->load->library('form_validation');
		$this->load->library('encryption');
		$this->load->model('login');
		
	}
	
	public function index()
	{
		$this->load->view('index');
	}

	public function auth()
	{
		$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
		$this->form_validation->set_rules('password', 'Password', 'required');
		if ($this->form_validation->run()) {
		$result = $this->login->authenticate($this->input->post('email'),$this->input->post('password'));
		if ($result == '') {
			
			redirect('admins/home');
		}
		else{
			$this->session->set_flashdata('message', $result);
			redirect('admins/home');
		}
			
		} else {
			$this->index();
		}
	}
}

Seems it’s already doing the redirect you want.

(assuming that an empty string means you’re logged in, which seems questionable, but hey ho)

@rpkamp look but I don’t why it’s not work :thinking:

yes you’re right empty strings mean user is logged in still it doesn’t work

Is there something wrong in route.php ?

hey It works fine I forgot to remove redirection from Admins controller

1 Like

Thank to both you for taking some time for my query

1 Like