1

How do you make a simple navigation in Code Igniter? I keep tripping up on all these pseudo folders. Originally I was looking to make something that dug through a folder and grabbed all the file-names, then placed them in an array to order them into a nav bar.

Problem is, it seems that CI uses public functions for the pseudo folder in each page. Such as "public function aboutme()" would work out to xxx.xxx.xxx.xxx/index.php/[classname]/aboutme. This poses a problem for me, because when making links like test works out to xxx.xxx.xxx.xxx/[classname]/contactme.

Is there a better, dynamic way to make a nav bar without using a database? I CAN use a database, but I'm trying to learn CI and since I'm already well-versed in MySQL I wanted to find a way to do this some other way.

2
  • CodeIgniter provides a pseudo path scheme for each page. I found it convenient in developing my app as the paths I chose were [verb]/[object] such as edit/selection and view/user. You are not obligated to use it. You can easily redo or override that portion of CI. Commented Jul 25, 2012 at 18:08
  • Is there no way to get an array of classes and their respective paths? Commented Jul 25, 2012 at 18:16

1 Answer 1

1

Maybe a custom route would help you?

http://codeigniter.com/user_guide/general/routing.html

I'm not sure the structure of your application, but if you are using multiple controllers, it won't really help and there isn't really an easy way to do it "automagically". The best thing would be to come up with some way to manage the navigation items, such as a database (as you said).

If you're only using one class, you can use PHP's get_class_methods() on the class.

$pages = get_class_methods('classname');

foreach($pages as $page)
{
    echo anchor($page, ucfirst($page));
}

This is a very simplistic way to do it, and you'll have to filter out private methods and the __construct method as well.

Sign up to request clarification or add additional context in comments.

4 Comments

In this case, I have a header, body, and footer for each page. Each page is automatically generated by a public function defined in the class. I find it a little hard to believe that CI doesn't keep a cache of functions included in a class, along with each function's respective pseudo location - or if it doesn't keep record, that it doesn't know that multiple functions exist. But I may be wrong, I've only been doing this for a day.
So, are you only using one class?
Class [classname] extends CI_Controller, Then various public functions that create pages. I haven't yet found a reason to make multiple classes, just multiple functions within the class.
If you're only using the one class, you can use get_class_methods('classname') and filter out the ones you don't want in the navigation by removing them from the array with unset() or array_splice() (depending on if you want to normalize the array indices).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.