Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple URL Routing not working with CodeIgniter

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?

like image 680
ErinSKabbash Avatar asked Mar 21 '13 18:03

ErinSKabbash


People also ask

How does route work in CodeIgniter?

Routing rules are defined in your application/config/routes. php file. In it you'll see an array called $route that permits you to specify your own routing criteria. Routes can either be specified using wildcards or Regular Expressions.

What is URL routing in CodeIgniter?

Routing matches the URL to the pre-defined routes. If no route match is found then, CodeIgniter throws a page not found an exception. Routes in CodeIgniter are defined using the below formula: example.com/Controller/Method/Parameter/ HERE, Controller -is mapped to the controller name that should respond to the URL.

How does URL routing work?

URL routing allows you to configure an application to accept request URLs that do not map to physical files. A request URL is simply the URL a user enters into their browser to find a page on your web site.

Which file the URL routing is specified?

Routing maps URL to physical file or class (controller class in MVC). Route contains URL pattern and handler information. URL pattern starts after the domain name. Routes can be configured in RouteConfig class.


1 Answers

$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

like image 83
stormdrain Avatar answered Sep 30 '22 10:09

stormdrain