Part of the EllisLab Network
x
 
Create New Page
 View Previous Changes    ( Last updated by {author} )

Revision: FAQ

Revision from: 06:37, 10 Oct 2009

Table of Contents
(Absent an automagic ToC feature in the CI-wiki, this is here for convenience only.  Do NOT assume all the FAQs on this page are listed in this table of contents—please read through this whole page, just in case.  Ctrl-F is your friend ;)

Database
* How do I see the actual raw SQL query generated by CI’s Active Record
* How do I find out the ID of the row I just inserted?

Design
* I keep repeating lumps of code - things like login-dialogs, stylesheets, headers, footers, or menus - is there some place I can define them just once?
* How do I embed views within views? Nested templates? Header, Main, Footer (Blocks) design? Partials? Page Fragments? Ruby on Rails style Yield functionality?

Images
* When I load an image in my code, it doesn’t appear on the rendered page?

Site migrations, relocations and platform differences
* After moving from development to production, my site stopped working
* How do I migrate my existing ‘normal’ PHP site to CodeIgniter?

CodeIgniter and Ellis Labs and Expression Engine
* When is the next version of Code Igniter coming out?  What can I expect from the next version of Code Igniter?
* I’ve got cool new code for CI. What is the process for getting it included in the official CI distribution?

Plain old frequently asked questions
* How do I find the name of the current controller and/or method?
* Can I cache only certain parts of a page?
* How do I call methods in one controller via another controller?

 

 

Database

Checking under the hood of queries, that kind of thing

How do I see the actual raw SQL query generated by CI’s Active Record

Answer 1
Turn on profiling (part of the benchmarking class) - this will show the full detail of all the SQL queries for the page.

Answer 2
You can do this before your query:

$this->db->_compile_select(); 

.. and then this, once you’ve run the query:

$this->db->last_query(); 

 

How do I find out the ID of the row I just inserted?

(Just so that people searching for this will be more likely to find it, we’ll mention that this is comparable to the native PHP mysql_insert_id() function.)

This is covered in the Query Helper Functions section of the CI User Guide - the function you’re looking for is:

$foo $this->db->insert_id(); 

 

 

Design

Architecture of your application, what goes where, design patterns, that kind of thing

I keep repeating lumps of code - things like login-dialogs, stylesheets, headers, footers, or menus - is there some place I can define them just once?

This is covered in the Different Approaches Guide part of this wiki.

It 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.

 

How do I embed views within views? Nested templates? Header, Main, Footer (Blocks) design? Partials? Page Fragments? Ruby on Rails style Yield functionality?

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.
http://codeigniter.com/forums/viewthread/62521/

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 wiki page 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/

 

 

 

Errors

Frequently Seen Error messages, that kind of thing

Headers already sent ...

9 times out of 10, this error is caused by one of:

* whitespace after a closing ?> tag,
* whitespace before a <?php opening tag at the start of one of your files
* residual debugging echo (print_r, var_dump, etc) in your non-view files

The solution(s) are easy - do not use closing ?> tags anywhere, ever, in your libraries, models, controllers and helpers.  Check those files for any whitespace at the start of the file.  Check for echo/print_r/var_dump/etc functions - usually you’ll see the output of those on-screen before the error message, in any case.

 

Images

Embedding graphic images, that kind of thing

When I load an image in my code, it doesn’t appear on the rendered page?

This is a hugely common problem, and 99 times out of ten it’s because you are confused about paths.  This is very much a PHP / HTML problem, not a CodeIgniter one, but the following hints might help you resolve this.

First - look at the page source in your browser - this is the most important and most useful thing you can examine.  It’ll give you very strong hints immediately as to what’s going wrong.  You can cut-n-paste the img src component into another browser, and then start modifying it until you find a path that works.

Second - utilise the CI helpers, if you’re not already.  For example, if you keep your images at /assets/images then you can pull foo.jpg in with this code (modify to taste) in your view:

<?php
    $foovar 
"foo.jpg";
    
$img_array = array ("src"=> "/assets/images/"$foovar"border"=>"0");
    echo 
anchor (img($img_array));
?> 

 

 

Site migrations, relocations and platform differences

Relocating from GNU/Linux to Windows, changing databases engines, that kind of thing

After moving from development to production, my site stopped working

There are several sub-categories to this problem, depending what you mean by ‘doesn’t work’

We assume that you have done ALLthe following easy steps:
o  used phpinfo.php to determine if mod_rewrite is actually running
o  checked the contents of your .htaccess for a specific reference to your dev box
o  changed the $config[‘uri_protocol’] - trying ALL of the FIVE choices there

Subcategory - my index.php isn’t being hidden anymore

If your .htaccess mod_rewrite is no longer working—check that you have AllowOverride All in your host’s config file for the public web directory of your website.

How do I migrate my existing ‘normal’ PHP site to CodeIgniter?

Answer 1 - provided by n0xie
How we migrated a rather big site:

Put the whole site in a subfolder of your root web folder, and use .htaccess to rewrite all requests to the subfolder (we named it ‘oldsite’ but any name will do). Be wary of absolute file paths (do a find on the entire codebase to look for any file that uses the filesystem). Make sure everything works before you continue any further.

Now setup your CI site in the root of the webfolder. Make sure the .htaccess still routes all traffic to the old site.

Now overwrite the Router class with a MY_Router. Let CI check if there is a controller present which matches against the url (the way it normally does). If not, redirect to the oldsite folder. Remove the .htaccess rule that redirects all traffic to the old site.

Now every request will go through the CI index.php. The Router Class will test if a controller exists for the requested url. (just like it would normally so you can user your routes in your config folder). If it won’ t find one, normally CI would return a 404. Instead it now redirects the request to the old site.

Now you can slowly migrate parts of the website. Since the CI controllers take precedent over the old website, you can slowly replace parts of the old websites, and add new features as you would a normal CI site.

One word of caution. This will seriously impact any pagerank google has given to your website, since google doesn’t like redirects. To counter this you can use your .htaccess to rewrite old urls to the ‘oldsite’ subfolder, although the list could become quite large.

Answer 2
Read through the Site Migration page for an alternative approach.

 

CodeIgniter and Ellis Labs and Expression Engine

Release schedules, contributions, that kind of thing

When is the next version of Code Igniter coming out?  What can I expect from the next version of Code Igniter?

Code Igniter is a product of EllisLab.  New releases and new features in the framework are made available when it is possible to release them, and may always be a surprise:
http://codeigniter.com/forums/viewthread/53619/P15/#262381

You can glean some insight from reading the Changelog, which shows the historical frequency of releases and the types and scale of changes.

I’ve got cool new code for CI. What is the process for getting it included in the official CI distribution?

New code is welcome, but not all code can be included.  To maximize the chances of your code making it into CI, test (PHP 4 and 5) and document the code. All decisions about incorporating it fall to EllisLab. Basically, if you want to satisfy your lust for abiding fame you have to make an effort to promulgate your code to as many CI developers as possible who put it through an informal ‘vetting’ process. As part of this process you should create a ‘manual page’ to help people learn.

 

 

 

Plain old frequently asked questions

Almost a ‘misc’ category, but categorically not a dumping ground - these are simply uncategorised questions ... for things that aren’t any obvious or particular kind of thing yet

How do I find the name of the current controller and/or method?

There are standard PHP function that’s good for this kind of thing, and you’re encouraged to consult the PHP User Guide.

get_class() will return the current class’s name.

get_class_methods() will return the full list of methods for a given class.


Alternatively, consult the URI Class in the user manual, paying particular attention to the rsegment() function - as you may be happy with pulling segment(1) for your class, and segment(2) for your method.


Alternatively, you can query the CI Routing class directly, using these functions:

$this->Router->class;
$this->Router->method

 

Can I cache only certain parts of a page?

A. This is related to the question above about nested templates and partials. Basically, CI cache library (1.5.4) only supports full page caching - it’s all or nothing. There are several contributions that can help.

The Sparks library is one approach. (NOTE: the Sparks object caching library is currently an orphan (as of 20070925) as the developer has moved on to Zend Framework - anyone want to step up and carry it on?)

In case you have questions to ask on the forum, please review this general information on caches. There are many levels of caching and they can be broken down into a few categories and approaches:

PHP code itself: php opcode of some kind

The following can be classed as session-specific or global caches depending on the approach:
DB cache: db query, db object serialization
HTML output cache: partial or full page caching

Browser cache is always (by definition) session-specific:
Browser cache: using headers to control cache, JS and CSS architecture to optimize browser cache-ability

 

How do I call methods in one controller via another controller?

Often this question disguises a requirement for some shared methods - consult the earlier question on View Partials and Header/Footer/Menu common views first, and confirm your question isn’t better answered there.

Modular Extensions (ME) aka the HMVC library allows you to do this. ME/HMVC<strike>You don’t. See http://codeigniter.com/forums/viewthread/55212/</strike> for more discussion.

 

 

Category:Help

Categories: