Simple URL Routing not working with CodeIgniter

36,000
$route['home'] = 'theWorksPlumbingController/index/home';
$route['about'] = 'theWorksPlumbingController/index/about';
$route['services'] = 'theWorksPlumbingController/index/services'; 

Might do it.. although I've never tried it with a setup like that. Typically in CI you make a method for each page like so:

class TheWorksPlumbingController extends CI_Controller {

    public function home(){

        $this->load->view('templates/header');
        $this->load->view('home');
        $this->load->view('templates/footer');
    }

    public function about(){

        $this->load->view('templates/header');
        $this->load->view('about');
        $this->load->view('templates/footer');
    }

    public function services(){

        $this->load->view('templates/header');
        $this->load->view('services');
        $this->load->view('templates/footer');
    }
}

which are reachable via

http://www.example.com/index.php/theWorksPlumbingController/home
http://www.example.com/index.php/theWorksPlumbingController/about
http://www.example.com/index.php/theWorksPlumbingController/services

And routable via

$route['home'] = 'theWorksPlumbingController/home';
$route['about'] = 'theWorksPlumbingController/about';
$route['services'] = 'theWorksPlumbingController/services';

https://www.codeigniter.com/user_guide/general/routing.html

Share:
36,000
ErinSKabbash
Author by

ErinSKabbash

Updated on February 12, 2021

Comments

  • ErinSKabbash
    ErinSKabbash over 3 years

    I am fairly new to CodeIgniter and finishing up my first project. However, before I put it up on my hosting site I would like to clean up the URL's using the routes.php file that CodeIgniter provides in the config folder.

    My site will load using the following urls:

    http://fakehost:8888/TheWorksPlumbing/index.php/theWorksPlumbingController/index/home
    http://fakehost:8888/TheWorksPlumbing/index.php/theWorksPlumbingController/index/about
    http://fakehost:8888/TheWorksPlumbing/index.php/theWorksPlumbingController/index/services
    

    and it will also load the homepage by using the default controller url of: http://fakehost:8888/TheWorksPlumbing/

    However, I want to have a url for each page of the site but cannot get it to work. For example I would like to have:

    http://fakehost:8888/TheWorksPlumbing/home
    http://fakehost:8888/TheWorksPlumbing/about
    http://fakehost:8888/TheWorksPlumbing/services
    

    Here is the code for the theWorksPlumbingController Controller file:

    class TheWorksPlumbingController extends CI_Controller {
    
        public function index($page = 'home'){
    
            if ( !file_exists('application/views/'.$page.'.php') ) {
                show_404();
            }
    
            $this->load->view('templates/header');
            $this->load->view($page);
            $this->load->view('templates/footer');
        }
    }
    

    Here is the code in my routes.php file that is not working:

    $route['default_controller'] = "theWorksPlumbingController";
    $route['404_override'] = '';
    $route['(:any)'] = "index.php/theWorksPlumbingController/index";
    

    What do I need to add or change to get the site to just load /home or /about or /services?

  • ErinSKabbash
    ErinSKabbash about 11 years
    I tried to add your routes to my routes.php.. but it did not work. Then I kept them in the routes.php and tried to add the functions you suggested into the controller file theWorksPlumbingController.php but that didn't work either? Am I missing something that I need to autoload? or maybe something in the .htaccess file?
  • stormdrain
    stormdrain about 11 years
    make sure to get rid of the $route['(:any)'] = "index.php/theWorksPlumbingController/index"; route. .htaccess shouldn't affect the routes at all; .htaccess is used for things like hiding index.php form the url.
  • ErinSKabbash
    ErinSKabbash about 11 years
    Ahhh, ok. I added the index.php to the url and it worked. So now I have localhost:8888/TheWorksPlumbing/index.php/home but how to I hide index.php?
  • stormdrain
    stormdrain about 11 years
    ellislab.com/forums/viewthread/155801 Or just search for 'codeigniter hide index.php'. That's one of the most frequently asked questions and there are thousands of answers out there.