ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Expression Engine + CodeIgniter -- Tips and Tricks to Get Started

December 31, 2009 3:40pm

Subscribe [20]
  • #1 / Dec 31, 2009 3:40pm

    vanquish

    48 posts

    Since everyone has been so eager to jump into using CodeIgniter in Expression Engine 2.0, this thread has been created as a sort of informal discussion area for people to post and discuss helpful tips and tricks they have learned in using CodeIgniter inside Expression Engine.

    CodeIgniter can now be used in the following components of EE:

    MODULES (most recommended)
    —Full-featured components, including Control Panel functions and front-end functions.  Managing templates, database, etc.  This is recommended if you need a lot of features and would like to follow the traditional CI model/view/controller style setup. Easy to call from EE templates. Example: {exp:module_name:function_name}

    PLUGINS
    —Focusing on modifying template code and front-end work

    EXTENSIONS
    —Tweaking the functionality of the Control Panel and hooks in the system

    From what I have seen, the most flexible implementation of CodeIgniter will occur in modules.

    ————————————————————————————————————————————————————————————————————-
    NOTE: All third-party add-ons must be placed inside the folder of /system/expressionengine/third_party
    ————————————————————————————————————————————————————————————————————-

    Modules:

    Modules will enable you to follow the traditional CodeIgniter MODEL-VIEW-CONTROLLER style setup, and offer the greatest flexibility.

    With minor amount of work, you should be able to convert a basic CI application to EE by making it a module and following the instructions below.  You can then call your CodeIgniter methods directly from your templates!

    To load CodeIgniter helpers/libraries into your module, you simply do the following inside your function:

    $this->CI =& get_instance(); 
    $this->CI->load->helper('url');

    The module folder must be named as module_name.

    Modules require several files to operate properly.  Make sure you have created the following files in your module_name folder:

    /mcp.module_name.php - Your back end control panel functions for submission and/or management of content

    /mod.module_name.php - This is the core module file, and will contain the methods that your EE tags will be calling. It is used for front-end stuff like form processing and the displaying of content from the database. (example: {exp:module_name:function_name}

    /upd.module_name.php - Update file for installing/uninstalling/updating your module from the EE database, and also for installing/uninstalling/updating your own custom tables

    /language/english/lang.module_name.php - Language definitions for your application to use.

    Once you have your module set up, it basically acts as a mini CodeIgniter “application” folder similar to a regular CodeIgniter installation.  You can create a /view folder to hold your view files, and a /models folder to hold your models.  Note: The view files will be used in the Control Panel *only*.  Your EE templates act as your front-end “views”.

    Several of these files require special code to be detected inside Expression Engine.  Please refer to the Expression Engine 2.0 module development documentation here for details on all the requirements:
    http://expressionengine.com/public_beta/docs/development/tutorial.html

    ————————————————————————————————————————————————————————————————————-

    Plugins:
    To use CodeIgniter inside a Plugin, you can create a normal style controller class/function structure.  To load CodeIgniter helpers/libraries into your plugin, you simply do the following inside your function:

    $this->CI =& get_instance(); 
    $this->CI->load->helper('url');

    Plugins need to be named in the format of pi.plugin_name.php.  Folder name should be as plugin_name. You will need to add the appropriate EE plugin code for it to be detected inside EE.

    <?php
    $plugin_info       = array(
       'pi_name'        => 'Test\'s Plugin',
       'pi_version'     => '1.0',
       'pi_author'      => 'Author Name',
       'pi_author_url'  => 'http://www.google.com',
       'pi_description' => 'This is a test to see how the plugin system works.',
       'pi_usage'       => Test_plugin::usage()
       );
    
    class Test_plugin
    {
        var $return_data  = "";
        
        function Test_plugin()
        {
        
        }
        
        // This function holds the usage documentation
        function usage()
        {
            ob_start(); 
            ?>
            This is where our simplified documentation will go.
            <?php
            $buffer = ob_get_contents();
            ob_end_clean(); 
            return $buffer;
        }
    
    }

    It would be helpful if everyone could share a few of their own tips on how they were able to implement CodeIgniter software in Expression Engine.

  • #2 / Jan 03, 2010 8:44am

    Scott Boyde

    208 posts

    vanquish have you any ideas where you would begin.

  • #3 / Jan 04, 2010 2:26pm

    Scott Boyde

    208 posts

    I have been able to run my CI app alongside EE2 with using their own system folders until I figure out how the CI app to use EE2.

    My structure is as follows.

    nifootball/
    ee_system
    system - CI system

    public_html/
    application - CI app folder
    index.php - EE2 index
    stats.php - CI index

    For the CI parts pf the site I now have

    The stats.php is need to route the CI controllers etc properly but is there anyway I can hide this or rewrite

    http://www.nifootball.co.uk/stats.php/league/viewleague/nafl.html
    to
    http://www.nifootball.co.uk/league/viewleague/nafl.html

  • #4 / Jan 04, 2010 4:35pm

    Rick Jolly

    729 posts

    Scott, post your .htaccess file.

  • #5 / Jan 04, 2010 4:54pm

    Scott Boyde

    208 posts

    RewriteEngine On
    RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L]
  • #6 / Jan 04, 2010 5:35pm

    Rick Jolly

    729 posts

    There must be something in your url so we can distinguish between EE and CI. If you can use “league”, then you are set. In that case, with a modified .htaccess, the following urls will go through CI: Urls that aren’t an image or an existing directory/file and
    start with “league” and are optionally followed by a slash or a slash and anything else.

    RewriteEngine On
    
    # EE
    RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond $1 !^(league|league/.*)$ [NC]
    RewriteRule ^(.*)$ index.php/$1 [L]
    
    # CI
    RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond $1 ^(league|league/.*)$ [NC]
    RewriteRule ^(.*)$ stats.php/$1 [L]

    Edit: Remember to set your “index_page” config variable to an empty string in the CI config.php file.
    Edit: As a simple alternative, have you considered using subdirectory for CI or EE?

  • #7 / Jan 04, 2010 6:21pm

    Scott Boyde

    208 posts

    Thats the job, cheers.

  • #8 / Jan 04, 2010 6:48pm

    Scott Boyde

    208 posts

    Rick

    Just seen the

    As a simple alternative, have you considered using subdirectory for CI or EE?

    What do you mean.

  • #9 / Jan 04, 2010 7:02pm

    Rick Jolly

    729 posts

    For example, say you put the CI index page in a subdirectory called “ci”. Your urls would include the subdirectory like this:

    http://www.nifootball.co.uk/ci/league/viewleague/nafl.html

    You could add an .htaccess file to your CI subdirectory that is almost exactly the same as your original EE .htaccess. You wouldn’t have to rename index.php to stats.php. In your CI .htaccess, just add the following line under “RewriteEngine On”:

    RewriteBase /ci

    That transparently just prepends all your rewritten urls with “ci”.

  • #10 / Jan 04, 2010 7:15pm

    Scott Boyde

    208 posts

    I want to use EE as my starting point and as my current site uses CI I also wanted to try and keep the original urls.

  • #11 / Jan 04, 2010 7:33pm

    Rick Jolly

    729 posts

    Ok, you could stick with the solution above, or put EE in the subdirectory. Since you want EE as your starting point, you could redirect to EE from your index method in your default CI controller. Alternatively, in your CI .htaccess, if the url just has the domain (your “starting point”) you could rewrite to the EE subdirectory. CI or EE routes aren’t really an option here since at that point it would be too late - you’d already be in either the EE or CI application.

  • #12 / Jan 11, 2010 1:42pm

    vanquish

    48 posts

    Hey everyone, I updated my posting with some more of my findings after developing some more with EE and CI together.

    You should able to convert a basic CI application to EE by making it a module and following the instructions I have outlined.  You can then call your CI application directly from your templates!

  • #13 / Jan 11, 2010 2:22pm

    Scott Boyde

    208 posts

    Hey everyone, I updated my posting with some more of my findings after developing some more with EE and CI together.

    You should able to convert a basic CI application to EE by making it a module and following the instructions I have outlined.  You can then call your CI application directly from your templates!

    For each controller do you need a separate module.  Would you have an example I could follow.

  • #14 / Jan 11, 2010 6:44pm

    Brandon Jones

    5500 posts

    Hey everyone, I updated my posting with some more of my findings after developing some more with EE and CI together.

    Thanks for taking the time to do this.  I’d suggest making it more clear that Views are only used on the control panel side of EE.

  • #15 / Jan 12, 2010 12:34am

    vanquish

    48 posts

    Hey everyone, I updated my posting with some more of my findings after developing some more with EE and CI together.

    Thanks for taking the time to do this.  I’d suggest making it more clear that Views are only used on the control panel side of EE.

    No problem!  I updated the posting as you recommended.  I had to work through it with one of my own CI/EE projects, so I figured it would be nice to share some of my findings.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases