2

I have got a problem with my codeigniter library. To transform database data into a navigation and have active elements and such like I build a library class CI_Navigation(). It works perfectly fine, I just have one problem.

Often on webpages one has corresponding but separated navigations e.g. a main navigation at the top and a subnavigation on the side. To get around that I can initialize my class with a parameter telling it which levels this navigation is to hold. I would for example do something like this.

$this->load->library('Navigation');

$main = $this->navigation->build( array('levels'=>array(0)) );
$sub = $this->navigation->build( array('levels'=>array(1,2)) );

As you might expect it does not work, because the data in the class stays the way it was assigned by the first call of build.

Sadly enough in CodeIgniter Libraries are singletons (right? that’s what I read). So I can not initialize it twice like:

$this->load->library('Navigation','','main');
$this->load->library('Navigation','',sub);

Do you have any idea how I can overcome this problem.

It should work if I would use arrays for the variables used in the class right? E.g. for the options instead of using $this->option I would have to dynamically create $this->$option[0], $this->$option[1].

Does this work? I can not test it at the moment but will do so tonight. But this is not a really elegant way, so is there a better way to solve this? Is there any way i could initialize the library multiple times?

Thanks in advance guys.

2 Answers 2

7

If you want to stick the strict CI library implementation, then I would suggest that you make your class parameters an array of configurations; and then specify the configuration you want to use in your 'build' function.

$options = array
(
  'main' => array('level' => 0),
  'sub' =>array('level' => 1)
);

$this->load->library('navigation', $options);

$main_nav = $this->navigation->build('main');
$sub_nav = $this->navigation->build('sub');

But I often revert to standard objects for this sort of thing:

$a_navigator = new Navigation($options_a);
$b_navigator = new Navigation($options_b);

$a_tree = $a_navigator->build();
$b_tree = $b_navigator->build();

unset($a_navigator);
unset($b_navigator);
Sign up to request clarification or add additional context in comments.

2 Comments

Okey, thank you, the first solution is about what I had in mind, so I will try that and let you know whether it works. Thanks for your answer anyway.
Yes, it works. Perfect, thanks so very much. I am actually talking about the first version, using the options array. I did not try the other method.
0

Alternatively you can do stuff like this:

$this->load->library('navigation');
$this->another_navigation = clone($this->navigation);

Comments

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.