Question

I encounter problems when I call the method of a controller. By the way, this controller is routed.

Routes

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

Controller

class Company extends CI_controller {

    public function __construct() {
        parent::__construct();
        session_start();

        /** Check if user is logged in */
        if ($this->session->userdata('user') != "") {

            $this->load->model('my_model');
            if ( $this->uri->segment(1) != "admin" ) {
              redirect('admin/company/'.$this->uri->segment(2));
            }

        } else redirect('/');
    }
        public function index() { Some coding here............  }
        public function addnew() { Some coding here...........}
        public function process() { Some coding here...... }
    }

When I call "localhost/company", it works fine and redirects me to "localhost/admin/company which is great. But, when I try to call the method of it, it displays a 404 error message.

Example: When I go to link: localhost/admin/company/addnew

Did lack something in routes? or in controller? or anything else?

Thanks, James

Was it helpful?

Solution 2

You will have to add a route for each function in your controller.

$route['admin/company/addNew'] ='company/addNew';
$route['admin/company/process'] ='company/process';

It's very annoying. Better, create a folder "admin" inside your "controllers" folder. Put the controller on the folder. Thus you can access your controller with the URL "localhost/admin/company" and all the methods without rerouting.

If it doesn't work at first, create a controller inside "admin" folder with the same name you'll find in your routes file (default_controller).

OTHER TIPS

If appropriate for all use cases, use a simple catch-all rule in routes.php:

$route['admin/company/(.+)$'] = "company/$1";

create a admin directory and add $route["company"]="admin/company"

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top