0
votes

I am calling a logout function of my controller from a view. Just for heads up, I have made the modular structure of my project and when I go to http://my-local-project.com/admin, it loads the index function of my admin controller. But when i go to http://my-local-project.com/admin/logout, it shows me 404 page my directory structure is

  • application/
    • Controllers/
      • admin/
        • admin.php

Controller:

    <?php

    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */

    /**
     * Description of TestController
     *
     * @author Ibm
     */
    class Admin extends CI_Controller {
        function __construct() {


            parent::__construct(); //call to parent constructor
            $this->data = "";
            $this->header = $this->load->view('admin/header', $this->data, TRUE);
            $this->template = $this->load->view('admin/template', $this->data, TRUE);
            $this->footer = $this->load->view('admin/footer', $this->data, TRUE);
            $this->load->helper('url');
            // $this->loginModel    =   $this->load->model('admin/loginModel');
            session_start();
        }
        public function index() {

            echo "all is well";
        }
public function logout() {
        $userSessionData = array(
            'user_id' => '',
            'username' => '',
            'email' => ''
        );
        $this->session->unset_userdata($userSessionData);
        $this->session->sess_destroy();
        session_destroy();
        redirect(base_url('admin/login'));
        exit;
    }
            }

    ?>

and here i want to call this function like this

 <a href="<?php echo site_url()?>admin/logout">Sign Out</a>

EDIT my routes.php is

$route['default_controller'] = "welcome";
$route['admin(/:any)'] = "admin/admin$1";
4
do you have htaccess in your project?? - Jenz
yes i have it. should i share it too? - baig772
R u removind index.php in it?? - Jenz
yeah I am removing index.php in it - baig772
This is because there is no any view page corresponding to logout. - Jenz

4 Answers

0
votes

Your app directory have 2 admin segment. 1. Folder(/admin/), 2. File (admin.php)

URL should be like http://my-local-project.com/admin/admin/logout

If you dont want like this, you have to set routes:

$route['admin']        = "admin/admin/index";
$route['admin/(:any)'] = "admin/admin/$1";

Or use CodeIgniter Modular Extensions - HMVC:

https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc

0
votes

You have to redirect to a page or have to load a view page in the function you are trying to call from url. So try like this

public function logout()
{
    $userSessionData = array(
        'user_id' => '',
        'username' => '',
        'email' => ''
    );
    $this->session->unset_userdata($userSessionData);// unset your sessions
    $this->session->sess_destroy();   
    redirect('admin/index');     // redirect to admin index page
}
0
votes

Just destroy the session and redirect ...

$this->session->sess_destroy();
redirect('controller/method');
0
votes

In routes make it:

$route['logout'] = "admin/admin/logout";

$route['admin'] = "admin/admin";

and call the logout as <a href="<?php echo base_url()?>logout">Sign Out</a>

In this way you will be calling the logout function present in the admin module-->admin class-->logout function

and In logout destroy the session as u did and use redirect('admin') which will redirect you to the controller index (since it is defined in the routes)