Search code examples
phpcodeignitertemplatesnavigationcodeigniter-routing

Code Igniter: issue with nav & templating based on current controller/method


My controllers and methods map to sections and pages so I use that to highlight current navigation items and show page templates (via $this->router->class..). But I've struck a bit of a problem, I have the following structure for doctor test results:

website.com/doctor/results/

So a 'doctor' controller has a method 'results' that loads a page listing of results. All good.

But there is a link next to each result listed, i.e. 'view details' to show details of that test. Functionally, clicking this link takes you to a 'test' controller with it's own method 'view' to show a page of details for that particular test. But visibly you should still be in the 'doctor' section, 'results' subsection.

I'm not sure how to achieve that? without rethinking the navigation as whole (which up until now seemed pretty good).


Solution

  • The common way to do this is to use a route:

    In the file /application/config/routes.php, add this route:

    $route['doctor/test-results/(:any)'] = 'test/view/$1';
    

    Where test-results is whatever you want the url segment to be (the page you'll link to).

    This basically says:

    Any url that is doctor/test-results/something should really load test/view/something

    You can do quite a bit with routes. Read more on routing in the User Guide

    If you find $this->router->class/method is not working out, try experimenting with some functions from the URI Class