Embed CodeIgniter application as component

This forum is for general questions about extensions for Joomla! 3.x.

Moderators: pe7er, General Support Moderators

Forum rules
Forum Rules
Absolute Beginner's Guide to Joomla! <-- please read before posting, this means YOU.
Forum Post Assistant - If you are serious about wanting help, you will use this tool to help you post.
Post Reply
AustinGrey
Joomla! Apprentice
Joomla! Apprentice
Posts: 5
Joined: Fri Jun 22, 2018 4:12 pm

Embed CodeIgniter application as component

Post by AustinGrey » Fri Jun 22, 2018 4:45 pm

Hi everyone,

The situation: How to get a codeigniter application working as a component in Joomla?

I'm new to Joomla development, and just barely learning how to make a functioning Joomla site. I'm an intern at an educational institution and what they've had me and all the other interns before is create new CodeIgniter applications - extending the existing website which was and is purely CodeIgniter. However more and more of the users at the institute are requesting little changes to static pages and the navigation on the website, which makes sense, but is bogging down my manager. So he asked me to either build a CMS or find one that we can switch all our development over to. Joomla seems to fit, we only need to render the current applications to the content section and we can build new ones in Joomla

Where I'm at now - I've got a working Joomla install, I can create pages and navigation just fine. I built a com_helloworld component that just echoed Hello World and it worked as expected, the template placed the result into the content section. Our codeigniter apps dont need to touch anything in the template, just render what they normally would. So naively I made a com_ci folder, put the CodeIgniter base installation into that, and then renamed it's index.php to ci.php, discovered and installed (using a blank xml). Now when I navigate to (site)/joomla/index.php?option=com_ci I instead see the template rendered and in the content section is another copy of the template rendered (with nothing inside the inner content section). There was an extension called xCIDeveloper that was supposed to be what I'm looking for, but it only looks like it was made for Joomla 1 and 2. If I were to navigate straight to (site)/joomla/components/com_ci/ci.php I see the codeigniter welcome page like expected.

What am I expecting - I'm guessing it has something to do with routing. But I have no clue where to start and how it would work together. For those unfamiliar with CodeIgniter it automatically routes URLs like (site)/index.php/(controller name)/(function name)/[(function parameters), ...] to the corresponding controller function with the passed in parameters, which is in charge of eventually rendering the view. What I would expect is that I could have a URL like (site)/joomla/index.php?option=com_ci/(controller name)/(function name)/[(function parameters), ...] or (site)/joomla/index.php?option=com_ci&controller=(controller name)&function=(function name)&params=(function parameters), ...


Any hints or tips on how I can fix the routing? Or if there is an updated CI component out there somewhere?

AustinGrey
Joomla! Apprentice
Joomla! Apprentice
Posts: 5
Joined: Fri Jun 22, 2018 4:12 pm

Re: Embed CodeIgniter application as component

Post by AustinGrey » Tue Jul 03, 2018 11:02 pm

Sorry for answering my own question, but this thread got no responses and 80+ views so I'm going to go ahead and let those looking know that it is possible and relatively straight forward. It took a bit of work, but essentially the process was as follows:

Using Joomla 3.8.8 and a fresh CodeIgniter 3.1.9 (I suspect any Joomla 3.x and CodeIgniter 3.x will do)

If you already have a modularized CodeIgniter app, then this is easy, if not, you will need to think about how to break apart the app into modular components - each of which will become their own Joomla component
  1. Create a bare-bones Joomla component (site folder, admin folder, xml), and place the Code Igniter installation into the site folder. develop your code from the site folder.
  2. Naming convention conflicts: Joomla looks for [name of component].php to run a component, but CodeIgniter expects to be run from a file named index.php. Rename index.php to [name of component].php and set the name in the application/config/config.php of codeigniter. For the remaining steps I'll call it the entry point to avoid confusion.
  3. In the entry point, set the path to the application and system folders using full path names from the html root, as they would exist once installed in joomla. With php 7, you can use __DIR__ to make this easy.
  4. The next hurdle is code igniter's routing, it expects URL/controller/method/list/of/args when running, but when Joomla calls you component the title is going to be mod rewritten to remove any chance of you keeping that the same. Instead, I wrote into the xml file a set of input variables for the component, ci_controller, ci_method, and ci_args. These are going to be made available using Joomla calls to your component, so you can access them still. Heres the code snippet I wrote

    Code: Select all

    $controller = JFactory::getApplication()->input->get('ci_controller', "", "");
        $method = JFactory::getApplication()->input->get('ci_method', "", "");
        $args = JFactory::getApplication()->input->get('ci_args', "", "");
        $destination = "";
        if ($controller !== ""){
            $destination .= $controller;
            if($method !== ""){
                $destination .= "/".$method;
                if($args !== ""){
                    $destination .= "/".implode("/", explode(",", $args));
                }
            }
            $route['(.*)'] = $destination;
  5. After that things should be working, links inside your app should be aware the application is running in Joomla -> linking to other methods and controllers are as easy as putting a entry point?ci_controller=Other_controller&ci_method=method_name&ci_args=list,of,args url together. API calls are different, if you dont want HTML returned you can link directly to the entry point and then use standard /controller/method/args urls like before (useful for AJAX)
I hope that helps anyone else looking!
Austin


Post Reply

Return to “Extensions for Joomla! 3.x”