CodeIgniter Forums
second controller not accessible - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: second controller not accessible (/showthread.php?tid=59570)

Pages: 1 2 3


second controller not accessible - El Forum - 10-21-2013

[eluser]webdevguy32[/eluser]
I have main controller. In main, a method (login) calls administer controller. The URL says, mysite/main/login = works fine. Once it logs in, it is redirected to mysite/administer/member. There is a controller in the controller directory called administer and it does have a method called member. The error for that address is:
Quote:404 Page Not Found
The page you requested was not found.
I can echo out some test code in the main controller just before that redirect so I know the code is getting there. The code is:
Code:
echo "*****************";exit;
            // validation passes so redirect user to logged in page
            redirect('administer/member');
It echos out the "********". When I remove the line, including exit, so it's only the redirect code,
Code:
redirect('administer/member');
it errors 404.
My base url and index file in config are blank.
My router are:
Code:
$route['default_controller'] = "main";
$route['404_override'] = '';
Here's my .htaccess:
Code:
Options +FollowSymLinks
    RewriteEngine On

RewriteRule ^(image|css)($|/) - [L]

    RewriteCond %{REQUEST_URI} !-f
    RewriteCond %{REQUEST_URI} !-d
    RewriteCond %{REQUEST_URI} !-l
    RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
I tried sub-folders, per the instructions on the site and could not get them to work either.

Any ideas why it is not able to display anything from the administer controller and how to fix it?

Thanks.


second controller not accessible - El Forum - 10-21-2013

[eluser]Tpojka[/eluser]
Does it work without .htaccess file and with index.php in URL?

Edit:
Have
Code:
$this->load->helper('url');
?


second controller not accessible - El Forum - 10-21-2013

[eluser]webdevguy32[/eluser]
I removed the contents of .htaccess, restarted apache, threw up this address
mysite/index.php/admin/admin/members
and it 404 errors out

When I initially added the index.php, I had to log in again and the address was
/index.php/main/login_validation
When I logged in, it sent me to
mysite/main/login_validation (without the index.php)
and errors out with "Object not found"


second controller not accessible - El Forum - 10-21-2013

[eluser]Tpojka[/eluser]
In first post you talked about administer/member, and now you are talking about admin/admin/members?


second controller not accessible - El Forum - 10-21-2013

[eluser]webdevguy32[/eluser]
You're right, I apo0logize. I changed the names on my end to see if the error was caused by any confusion in my system with the controller name and the folder it's contained in were the same. So I changed the name. The error still exists whatever the name is. Let's continue with it being, admin/admin/members

Thanks.


second controller not accessible - El Forum - 10-21-2013

[eluser]Tpojka[/eluser]
Is that application/controllers/admin/admin.php with method public function members(){}?
Can you post code so far?


second controller not accessible - El Forum - 10-21-2013

[eluser]webdevguy32[/eluser]
No, that url does not post - I have removed the htaccess and tried it with /index.php/controlerfolder/controller/method and it does not work either. I have restarted apache, cleared all browser cache, done everything I know to do. CI works only more than one controller (because I see in example that it mentions that it does, and in subfolders), so it must be something I am doing. I just don't know what I am doing wrong.
Thanks.


second controller not accessible - El Forum - 10-21-2013

[eluser]Tpojka[/eluser]
This is impossible to do this way. Let's try from beginning:

1. Remove .htaccess file (no need of restart server)

//config.php file
2. $config['base_url'] = 'http://localhost/codeigniterdirectory/'; //or $config['base_url'] = 'http://yourwebsite.tld/';
3. $config['index_page'] = 'index.php';
4. $config['uri_protocol'] = 'AUTO';

5. Write here your controller code and point where is it located in file system.

Maybe than, we could make something


second controller not accessible - El Forum - 10-21-2013

[eluser]webdevguy32[/eluser]
Okay. The structure is simple:
/controller/main.php
/controller/admin/administer.php - this has the method "members"

these are the urls I've tried and error out 404, with and without the htaccess rewrite on:
/index.php/admin/administer/members
/admin/administer/members

I changed the config file as you specified, still erroring out.

Code:
<?php

if (!defined('BASEPATH'))  exit('No direct script access allowed');

class Main extends CI_Controller {

    public function index() {
        $this->login();
    }

    public function login() {
        $this->load->view('login');
    }

    public function members() {
        if ($this->session->userdata('is_logged_in')) {
            $this->load->view('admin/header');
            $this->load->view('admin/members');
            $this->load->view('admin/footer');
        } else {
            redirect('main/restricted');
        }
    }

    public function restricted() {
        $this->load->view('restricted');
    }

    public function login_validation() {
        $this->load->library('session');

        $this->load->library('form_validation');
        $this->form_validation->set_rules('userID', 'Username', 'required|trim|xss_clean|callback_validate_credentials');
        $this->form_validation->set_rules('password', 'Password', 'required|md5|trim');

        if ($this->form_validation->run()) {
            //  Get the active user's email address to email them
            $query = $this->db->query("SELECT * FROM table_name WHERE id = '" . $this->input->post('id') . "'");
            $row = $query->first_row('array');
//            print_r($row);exit;
            //set session data
            $data = array(
...
.            //set session data
...
         );
            $this->session->set_userdata($data);

            // validation passes so redirect user to logged in page
            redirect('admin/administer/members');
        } else {
            // validation passes
            $this->load->view('login');
        }

        echo $this->input->post('username');
    }

    public function validate_credentials() {

        $this->load->model('model_users');

        if ($this->model_users->can_log_in()) {
            return true;
        } else {
            $this->form_validation->set_message('validate_credentials', 'Incorrect username/password');
            return false;
        }
    }

    public function logout() {
        $this->session->sess_destroy();
        redirect('main/login');
    }

}


Code:
<?php

if (!defined('BASEPATH'))  exit('No direct script access allowed');

class Main extends CI_Controller {

// doesn't work - this is for troubleshotting
//    public function __construct() {
//        parent::__construct();
//  //      $this->members();
//        // Your own constructor code
//    }

    public function verify_login() {
        if ($this->session->userdata('authorized') == 1) {
            return true;
        } else {
            $this->session->sess_destroy();
            redirect('main/login');
        }
    }

    public function members() {
        echo "*****************";exit; //this is for troubleshooting. it never gets to this point where this displays
        if ($this->verify_login()) {
            $this->load->view('admin/header');
            $this->load->view('admin/members');
            $this->load->view('admin/footer');
        }
    }

//some other enclosed methods here

}

?>



second controller not accessible - El Forum - 10-21-2013

[eluser]Tpojka[/eluser]
If code bellow one is administer.php file, It need to have

Code:
class Administer extends CI_Controller { /*code*/ }