Logo F2FInterview

Codelgnitor Interview Questions

Q   |   QA

Short answer - you probably want to extend the core Controller, so do a search in the forums for ‘MY_Controller’, or check out the section covering this on the different Approaches Guide page. This works well when you want to seamlessly introduce data into the CI superobject ($this-> ...).

Less short answer - in very general terms, if you want to call a function that doesn’t require access to the CI superobject, then you probably want a Helper (and you probably want to autoload it). If you want some functions that needs database access to a small number of tables, and is likely to be used through many controllers, you might be best with an autoloaded Model.

Longer answer - this is also covered by the next section (on nested templates, view partials etc) which in turn contains many links into the forums - this will provide a wealth of alternative approaches.

It is also asked frequently (hence its presence here - duh!) on the forums—about once every 36 hours on average, usually prefaced with the lie ‘I couldn’t find anything about how to do this…’. You’re advised (read strongly encouraged) to use the forum search feature and Search in Titles Only for the words ‘header’ and ‘footer’.

Asking this question anew within the forums is likely to imbue a certain degree of chagrin.

Check out the Different Approaches Guide to this problem.

There are several other described ways to deal with this problem:

Version 1.6 (Released January 31, 2008) has support for multiple views. See the User Guide for more details. That change to the core makes some of these approaches “old school” (as it were). This forum thread covers some good ground: http://codeigniter.com/forums/viewthread/87346/

First, basic information on views and the optional third ‘return’ parameter: http://www.codeigniter.com/wiki/Displaying_Multiple_Views/ and also, The CI Loader class documentation page

Coolfactor’s View Library is one solution to nesting views in a tidy way. http://codeigniter.com/forums/viewthread/49910/

Gyorgy Fekete’s View library similar to Coolfactor’s with the differences spelled out in the thread.

Another place to get advice if you don’t want to add a new library is Rick Ellis’ original thread on the subject (Embedding Views within Views). http://codeigniter.com/forums/viewthread/44916/

Also, teamhurting has implemented a Ruby On Rails style Yield approach that uses CI’s hooks system:
Yield using hooks - http://codeigniter.com/forums/viewthread/57902/

Another similar approach using a basic class rather than a class as a hook: http://codeigniter.com/wiki/layout_library/

A thread where esra lists a few threads on this topic: http://codeigniter.com/forums/viewthread/57965/

A Rails-Style ActionView library and helper called Ocular has a web page wiki here: http://codeigniter.com/wiki/Ocular_Layout_Library/ and a thread here: http://codeigniter.com/forums/viewthread/65050/

A thread for a View library (author tested with Matchbox) http://codeigniter.com/forums/viewthread/67028/

The obvious problem is that a second parameter on the URL will be interpreted as the method name.

Short answer - use a URL like this: /controller/index/param1/param2

Long answer - using _remap() you can work some magic. This code is supplied courtesy of Colin Williams

function _remap ( $method ) {
  $param_offset = 2;

  // Default to index
  if ( ! method_exists($this, $method))
    // We need one more param
    $param_offset = 1;
    $method = 'index';

  // Since all we get is $method, load up everything else in the URI
  $params = array_slice($this->uri->rsegment_array(), $param_offset);

  // Call the determined method with all params
  call_user_func_array(array($this, $method), $params);

Short answer: You don’t

Wrong answer: Use the $CI = get_instance() trick.

Long answer: You shouldn’t actually be trying to do this. It implies that your design isn’t quite right, but on the upside it’s quite easy to re-engineer the relevant bits when you first discover this problem.

It’s likely that the method you want to call should simply be relocated - this might mean moving it into a helper, library, model or your MY_Controller - where it can be accessed by any number of controllers. You’re encouraged to consult the user guide regarding helpers v. libraries, and elsewhere on this page and wiki for information about MY_Controller.

Short answer: You don’t

Long answer: You shouldn’t actually be trying to do this. It implies that your design isn’t quite right. (Your design may be right - I’m just saying that this implies that it’s not (also, if you have to ask this question, this also suggests you probably shouldn’t be doing this.)) Consider moving things around such that MY_Model or a library or helper contains the functionality you’re trying to utilise in this way.

If you absolutely positively have to do this (and remember - you shouldn’t) you can do the old $CI = get_instance() trick. Phil explains it in this thread but loosely it is simply this:

$ci =& get_instance();

In order to link this F2FInterview's page as Reference on your website or Blog, click on below text area and pres (CTRL-C) to copy the code in clipboard or right click then copy the following lines after that paste into your website or Blog.

Get Reference Link To This Page: (copy below code by (CTRL-C) and paste into your website or Blog)
HTML Rendering of above code: