The Setup

This setup guide assumes that you already have a working install of CodeIgniter 2.1. If you don’t, see my blog post Setting up a Fresh Install of CodeIgniter 2.1. It will guide you through the CodeIgniter 2.1 setup process.

Follow these steps to get the add-on up and running. Also, for a more in depth explanation of the add-on visit wiredesignz’s HMVC Wiki Page.

  1. Download wiredesignz HMVC zip file from bitbucket.org.
  2. Extract the files from the zip.
  3. Move all of the files from the newly extracted folder’s ./core/ directory to CodeIgniter’s /application/core/ directory on your server.
  4. Move the MX directory and all of its files from the newly extracted folder’s ./third-party/ directory to CodeIgniter’s /application/third-party/ directory on your server.
  5. Add the following to your /application/config/config.php file on your server:
    1
    2
    3
    $config['modules_locations'] = array(
    APPPATH.'modules/' => '../modules/',
    );

    Or, if you are like me and easily confused by how similar the /application/modules/ directory is to the /applications/models/ directory then you can change the directory name to whatever you wish. For example, you could change the directory to /applications/widgets/ by changing the word ‘modules’ in both the key and value position of the array element like so:

    1
    2
    3
    $config['modules_locations'] = array(
    APPPATH.'widgets/' => '../widgets/',
    );

    You can also add multiple locations like this:

    1
    2
    3
    4
    $config['modules_locations'] = array(
    APPPATH.'widgets/' => '../widgets/',
    APPPATH.'apps/' => '../apps/',
    );

    Now the HMVC add-on would look for a module directory at /application/widgets/ and at /applications/apps/.

  6. Lastly, create the modules directory naming it whatever you called it in step 5. For this example, I am going to create a directory /application/widgets/.

Test It

Now the installation is complete and should be up and running. Let’s test it out and make sure it’s working. Complete the following steps:

  1. First let’s create a module. I am going to call this module hmvc.
    1. We will start by fleshing out the structure. Create the following directories:
      • /application/widgets/hmvc/
      • /application/widgets/hmvc/controllers/
      • /application/widgets/hmvc/models/
      • /application/widgets/hmvc/views
      It is important to note that their are other directories that can be included in your module. The directories that I am aware of are /application/widgets/module_name/config/ and /application/widgets/module_name/library/; there may be more. For more information about this please refer to wiredesignz’s HMVC Wiki Page.
    2. Next, let’s create the module controller: /application/widgets/hmvc/controllers/hmvc.php
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');class Hmvc extends MX_Controller {
      public function index( )
      {
      $this->load->view('hmvc_view');
      }
      }
      /* End of file hmvc.php */
      /* Location: ./application/widgets/hmvc/controllers/hmvc.php */
      Notice that in the above code the class extends MX_Controller instead of CI_Controller. You will want to do this in every controller that will call a module. I would suggest just doing it in all of your controllers to reduce confusion.
    3. Now create the module view. Since this will be injected into another page, we can skip all of the <html>, <head>, and <body> tag stuff. So let’s just create a simple file: /application/widgets/hmvc/views/hmvc_view.php
      1
      <h2>This is from the HMVC module page.</h2>
    4. Now, assuming that you have gotten rid of that pesky index.php from your URL, point your browser at http://yourdomain/hmvc/hmvc/index. You should see: This is from the HMVC module page. on the page. Due to the convention built into the HMVC package, you can also get there from http://yourdomain/hmvc/hmvc or even from http://yourdomain/hmvc.

     

  2. Next we will want to create a test page to test injecting the module in another page. We will create these pages in the way in which we are accustomed to creating pages in CodeIgniter: in the /application/controllers/ and the /application/views/ directories.
    1. Create a test controller: /application/controllers/test_hmvc.php
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');class Test_hmvc extends MX_Controller {
      public function index()
      {
      $this->load->view('test_hmvc_view');
      }
      }
      /* End of file test_hmvc.php */
      /* Location: ./application/controllers/test_hmvc.php */
    2. Now create the view: /application/views/test_hmvc_view.php
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      <!DOCTYPE html>
      <html lang="en"><head>
      <meta charset="utf-8">
      <title>Testing HMVC</title>
      </head>
      <body>
      <h2>This is from the Test HMVC page.</h2>
      <?php echo Modules::run( 'hmvc/hmvc/index' ); ?>
      </body>
      </html>
    3. Now point your browser at http://yourdomain/test_hmvc/ and you will see:This is from the Test HMVC page.This is from the HMVC module page.

That’s it!

You now have a running working version of HMVC in CodeIgniter 2.1. Now that you have it set up, I would suggest reading wiredesignz’s HMVC Wiki Page to cement your understanding of this add-on.

Advertisements