⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

CI Modular Extensions - HMVC & Grocery_CRUD



noskov.biz

noskov.biz
  • profile picture
  • Member

Posted 07 June 2012 - 21: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 [b].htaccess[/b] 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 [b]./application/config/config.php[/b] 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/[b]modules[/b]/ folder. Then create ./application/modules/[b]welcome[/b]/ folder and create in it ./applications/modules/welcome/[b]controllers[/b]/ and ./applications/modules/welcome/[b]views[/b]/ folders.

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

Go to the [b]./application/config/config.php[/b] 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 [b]welcome.php[/b] 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 ./[b]assets[/b]/ folder along with ./application/ and ./system/ CI-folders.

Create new [b]./application/modules/grocery_crud/[/b] folder and put into it content of grocery_CRUD ./application/ folder to have structure like this:
./application/
[indent=1]...[/indent]
[indent=1]/modules/[/indent]
[indent=2]/grocery_crud/[/indent]
[indent=3]/config/[/indent]
[indent=3]/controllers/[/indent]
[indent=3]/libraries/[/indent]
[indent=3]/models/[/indent]
[indent=3]/views/[/indent]
[indent=3]index.html[/indent]
[indent=2]/welcome/[/indent]
[indent=3]...[/indent]

Since we have HMVC our controllers have to extend [b]MX_Controller[/b]. 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 [b]./application/modules/grocery_crud/views/example.php[/b] 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 [b]./application/modules/grocery_crud/config/routes.php[/b] 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 [b]your_project.com/grocery_crud[/b] and see the index of grocery everything is fine.

Result in [b].zip archive[/b] can be found in ci_hmvc_grocery_crud.

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

Wathfea

Wathfea
  • profile picture
  • Member

Posted 08 June 2012 - 13: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.

noskov.biz

noskov.biz
  • profile picture
  • Member

Posted 08 June 2012 - 16:52 PM

Hi there!

[quote][color=#282828][font=helvetica, arial, sans-serif]Looks like I inspired you [/font][/color] :D[/quote]
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.

fdias

fdias
  • profile picture
  • Member

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.

noskov.biz

noskov.biz
  • profile picture
  • Member

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.

fdias

fdias
  • profile picture
  • Member

Posted 10 June 2012 - 14: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.

noskov.biz

noskov.biz
  • profile picture
  • Member

Posted 10 June 2012 - 14:47 PM

[quote][color=#282828][font=helvetica, arial, sans-serif]but you don't have to keep record on what line to change[/font][/color][/quote]
Yeah, absolutely agry with you!

edramirez

edramirez
  • profile picture
  • Member

Posted 29 October 2013 - 17: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.


buoncri

buoncri
  • profile picture
  • Member

Posted 28 November 2013 - 11:57 AM

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


jamesmm

jamesmm
  • profile picture
  • Member

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!