Jump to content


CI Modular Extensions - HMVC & Grocery_CRUD


  • Please log in to reply
9 replies to this topic

#1 noskov

noskov

    grocery CRUD Guru

  • Advanced Member
  • PipPipPip
  • 175 posts
  • LocationKiev

Posted 07 June 2012 - 09:46 PM

This is a compilation of CodeIgniter, Modular Extentions - HMVC and lovely Grocery_CRUD. Maybe for someone it will be useful :)

First of all download CI and make a fresh instalation. No one likes /index.php/ in the urls, so we add for our project .htaccess file with these lines:
RewriteEngine on
RewriteCond $1 !^(index\.php|robots\.txt|assets)
RewriteRule ^(.*)$ /index.php/$1 [L]

and remove "index.php" from line #54 in our ./application/config/config.php file to have this instruction:
$config['index_page'] = '';

--------------------------------------------------------------------------------

Now we are going to make our CI application modular. Download Modular Extentions - HMVC and put it's content (just "core" and "third_party" folders) to the corresponding CodeIgniters folders.

In the next step create ./application/modules/ folder. Then create ./application/modules/welcome/ folder and create in it ./applications/modules/welcome/controllers/ and ./applications/modules/welcome/views/ folders.

Then move ./application/controllers/welcome.php file to ./application/modules/welcome/controllers/welcome.php. And ./application/views/welcome_message.php file to ./application/modules/welcome/views/welcome_message.php (ughh, sorry for this :wacko:).

Go to the ./application/config/config.php file and add this line to say where our modules are located:
$config['modules_locations'] = array(APPPATH.'modules/' => '../modules/');

Now just change line#28 in welcome.php into this, so our contoller extends MX_Controller and not CI_Controller:
class Welcome extends MX_Controller {

After this, if we see in the browser favorite "Welcome to CodeIgniter!", then everythigs is OK and our CI is modular.

--------------------------------------------------------------------------------

Now go to the grocery_CRUD and download it. Use examples_database.sql for creating your test db. Then put ./assets/ folder along with ./application/ and ./system/ CI-folders.

Create new ./application/modules/grocery_crud/ folder and put into it content of grocery_CRUD ./application/ folder to have structure like this:
./application/

...

/modules/

/grocery_crud/

/config/

/controllers/

/libraries/

/models/

/views/

index.html

/welcome/

...


Since we have HMVC our controllers have to extend MX_Controller. Go to the ./application/modules/grocery_crud/controllers/examples.php file and change line#3 into this:
class Examples extends MX_Controller {

I encoutered the problem: MX_Loader doesn't want to load library if it's name capitilize. So change and the line#11 into:
$this->load->library('grocery_crud');

and add line#12:
$this->load->helper('url');
to load the url_helper for our links.

Then go to the ./application/modules/grocery_crud/views/example.php file and change lines#31-36 into these:
<a href='<?php echo site_url('grocery_crud/examples/customers_management')?>'>Customers</a> |
<a href='<?php echo site_url('grocery_crud/examples/orders_management')?>'>Orders</a> |
<a href='<?php echo site_url('grocery_crud/examples/products_management')?>'>Products</a> |
<a href='<?php echo site_url('grocery_crud/examples/offices_management')?>'>Offices</a> |
<a href='<?php echo site_url('grocery_crud/examples/employees_management')?>'>Employees</a> |
<a href='<?php echo site_url('grocery_crud/examples/film_management')?>'>Films</a>

And the last thing: add ./application/modules/grocery_crud/config/routes.php file with this content:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

$route['grocery_crud'] = 'examples';

/* End of file routes.php */
/* Location: ./application/modules/grocery_crud/config/routes.php */
in order to have routing to the examples controller, when we access your_project.com/grocery/

If you can browse your_project.com/grocery_crud and see the index of grocery everything is fine.

Result in .zip archive can be found in ci_hmvc_grocery_crud.

Additional documentation you can find in wiki & docs of CI, Modular Extentions - HMVC and grocery_CRUD.

#2 Wathfea

Wathfea

    Member

  • Members
  • PipPip
  • 19 posts

Posted 08 June 2012 - 01:19 PM

Looks like I inspired you :D
Great work anyway.
Maybe you should add some template to it.
for it you have to make a views dir in your application folder if it is not already there.
After create a template.php with this lines:
<?php $this->load->view('templates/header'); ?>
    <?php $this->load->view($main_content); ?>
<?php $this->load->view('templates/footer'); ?>

After this make a templates folder in this views folder and add a header.php and a footer.php as well.

header.php
<!DOCTYPE HTML>
<html lang="en" class="no-js">
    <head>
	    <meta charset="UTF-8">
	    <link rel="stylesheet" href="<?php echo base_url();?>/css/style.css" />
	    <title> My page </title>
    </head>
    <body>

footer.php

</body>
</html>

Now your template base is ready.

But how you use it via the controllers?

It is simple.

In the controller whatever what function you edit use this method to pass the data:
function index() {
	    $data['users'] = array('Test1','Test2','Test3');
	    $data['main_content'] = 'login_form'; //load the login_form.php view to the template.
	    $this->load->view('template', $data); 
    }
   

And finally in the the view just foreach the passed data.
<?php foreach ($users as $user): ?>
echo $user.'<br />';
<?php endforeach?>

I hope It help.

#3 noskov

noskov

    grocery CRUD Guru

  • Advanced Member
  • PipPipPip
  • 175 posts
  • LocationKiev

Posted 08 June 2012 - 04:52 PM

Hi there!

Looks like I inspired you :D

Yeap :P Our yesterday searching with you prompted me to write this post. I've heard about HMVC, but I have never used it. Only a few days ago I tried to install MX, just for interesting and testing. And I like it!

I didn't mention about templates couse of I wanted to focus just on the Modular Extensions and Grocery CRUD. Thank you for posting about this, I think, it's really useful for those, who are going to differentiate their view files.

#4 fdias

fdias

    grocery CRUD Addict

  • Members
  • PipPipPip
  • 87 posts

Posted 10 June 2012 - 02:34 AM

Great post noskov.biz.

It would be great if Grocery Crud also allowed modular extensions or plugins. Right now we have to manually keep track of all changes or improvements we make before we can update.

Thanks for the step-by-step I've been using CI and GC for a while now and never tested the modular extension HMVC. It looks great and I might just use this as my new CI base.

Cheers.

#5 noskov

noskov

    grocery CRUD Guru

  • Advanced Member
  • PipPipPip
  • 175 posts
  • LocationKiev

Posted 10 June 2012 - 09:03 AM

You are welcome!

I think, developers have allways to keep an eye on changes of code that they use :) Probably plugins don't save from monitoring as they will be changing too.

#6 fdias

fdias

    grocery CRUD Addict

  • Members
  • PipPipPip
  • 87 posts

Posted 10 June 2012 - 02:30 PM

It's true you still have to monitor its changes, but you don't have to keep record on what line to change. Plugins would make it much easier to implement new optional features IMHO.

#7 noskov

noskov

    grocery CRUD Guru

  • Advanced Member
  • PipPipPip
  • 175 posts
  • LocationKiev

Posted 10 June 2012 - 02:47 PM

but you don't have to keep record on what line to change

Yeah, absolutely agry with you!

#8 edramirez

edramirez

    Advanced Member

  • Members
  • PipPipPip
  • 63 posts
  • LocationMetro Manila

Posted 29 October 2013 - 05:56 PM

From the beginning, I developed applications with CI, HMVC, and GC. Since GroceryCRUD already incorporates JQuery and Twitter Bootstrap, I was able to access these libraries without having to add them separately.



#9 buoncri

buoncri

    Newbie

  • Members
  • Pip
  • 2 posts

Posted 28 November 2013 - 11:57 AM

@edramirez can you explain the way you use to do that.



#10 jamesmm

jamesmm

    Newbie

  • Members
  • Pip
  • 2 posts

Posted 18 January 2014 - 08:39 AM

Thanks for the great tutorial! I got it working great!

 

However I was trying to use grocery with a new module I created called companies and I'm not able to get that to work.

 

Do you have any examples of using this once it's working?

 

Thanks!






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users