Part of the EllisLab Network
   
3 of 11
3
Modular Extensions - (HMVC) - Version 2.1.8
Posted: 20 February 2008 03:11 PM   [ Ignore ]   [ # 21 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1840
Joined  06-10-2007

Updates:
Added run()  - to load and execute a module in one call. Thanks Sam Dark.
Added index() - as the default run method. Thanks Edmilson Lima.
Added in_subdir() - as sub-directory path helper

 Signature 

URI Language Identifier | Modular Extensions - HMVC | Validation Callbacks into Models | View Object PHP5 | Read the User Guide

Profile
 
 
Posted: 20 February 2008 03:57 PM   [ Ignore ]   [ # 22 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  286
Joined  12-25-2007

Superb, wonderful! Finally we have a decent HMVC implementation in CI!
May it need some adjustments, but I think it is following the right path.
It is simple, easy and flexible, as things must be in our loved framework.
Now we need more usage examples, a section in the Wiki, etc.
May it could also goes someday to the core, who knows? smile

 Signature 

Oh God… Why didn’t you show me CodeIgniter before?

Profile
 
 
Posted: 20 February 2008 05:00 PM   [ Ignore ]   [ # 23 ]  
Lab Assistant
RankRank
Total Posts:  251
Joined  08-05-2007

So it’s basically groups of controllers, models and views?

That sounds great, especially with not having to add extra segments to the url! How does it deal with clashes of controllers though?

E.g. if a news module has a controller called admin (module/controller/function -> news/admin/index) and another module has the same named controller (user/admin/index)

 Signature 

CI Base - CodeIgniter Repository

Profile
 
 
Posted: 20 February 2008 05:05 PM   [ Ignore ]   [ # 24 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1840
Joined  06-10-2007

You’ll get an error if you try to load two classes with the same name. Blame it on PHP raspberry

It’s only modules as HMVC controllers, Models and Views are still located as per normal.

I usually create a sub-directory in application/views to match the module name, same with Models if necessary.

Remember this library will work with Zach’s Matchbox modular separation. It doesn’t replace it. wink

 Signature 

URI Language Identifier | Modular Extensions - HMVC | Validation Callbacks into Models | View Object PHP5 | Read the User Guide

Profile
 
 
Posted: 21 February 2008 03:17 AM   [ Ignore ]   [ # 25 ]  
Summer Student
Avatar
Total Posts:  19
Joined  02-20-2008

Hallo everyone,
My first message here on the forum.
I used to have my own framework made with PEAR libraries some several years ago, but since then PHP has come a long way, so the framework become quite outdated.

Since then I’ve started to look for a replacement framework, one which will be able to form cute URL’s, be small and be fast. That’s why I believe I’ve chosen CI. But… before I make my first project I decided to read everything I could find on the subject, including the book “CI for rapid PHP app. devel.”. And then I realized that I don’t have code reuse in my apps, something that standard PHP, PEAR and Savant2/3 used to give me. The ability to define a - I call them - modules (menu, pool box, banners, simple search form), which I will be able to include anywhere in the application and they would work out of the box.

The style I’m used to work is the following:
- one view template for header
- many view templates for the main section (reports, registration forms go here, it’s the central part of the page)
- one view template for the footer section

Header then includes the “modules” I’ve mentioned before, such as menu and maybe a search box, while the footer includes “modules” such as banners section or some similar.
Of course the “main” section is where the main content should be presented to the visitor - texts, pictures, registration forms, etc.

So I’ve decided to do the forum search. From what I can see, this topic is exactly what I need (please correct me if I’m wrong). But… I don’t find my self able to understand the code logic here.
I would politely ask you to well, give me some clues, a mini tutorial sort of and tell me and probably other people that wonder - how this code can be used? Where should it be placed, how should the code structure be divided. I believe that one simple example with one “included module/library/view” would be just about enough.

And please don’t mind my ignorance in MVC, since I do come from modular style of PHP programming.
Thanks a lot.

Gjoko Pargo.

Profile
 
 
Posted: 21 February 2008 04:01 AM   [ Ignore ]   [ # 26 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1840
Joined  06-10-2007

I am currently working on updating a car dealer website to use CI.

On this site we have a search form that needs managing, so I have created a module to process the form submission, populate the dropdowns from a database and render the form as a view partial. (see image)

Search module:

class Search extends HMVC
{
    
var $_make, $_price, $make_opts, $found;
    
    function
Search()
    
{
        parent
::HMVC();
        
$this->load->model('vehicles_model', 'vehicles');
                
        
//get the distinct vehicle makes from the table
        
$this->make_opts = $this->vehicles->findAll_makes();    
    
}
    
    
function index($page = '')    //$page name is provided from the main view
    
{
        
if ($page == 'showroom') $this->run();
        return
$this->render();
    
}
    
    
function run()
    
{
        
if ($_POST) // search data from the form
        
{
            $_make  
= $this->input->post('select-make');
            
$_price = $this->input->post('select-price');
        
}
        
else       //  search data in the url
        
{
            $_make  
= (int)$this->uri->segment(2);
            
$_price = (int)$this->uri->segment(3);        
        
}
        
        
if ($_make OR $_price)   //use a bit of logic to build database querys
        
{
            $this
->_make  = $_make;
            
$this->_price = $_price;
            
            
$search_opts = array(
                
'isonline',
                
'sellprice = 0',
                
'sellprice > 0 AND sellprice < 5000',
                
'sellprice > 4999',
            );
            
            
$_qry   = ($_make) ? "make = '{$this->make_opts[$_make]}' AND " : '';
            
$_order = ($_price) ? " ORDER BY sellprice" : " ORDER BY make";
            
            
$this->found = $this->vehicles->findPaged("{$_qry}{$search_opts[$_price]}{$_order}", 12);
        
}
        
else
            
$this->found = $this->vehicles->findPaged("isonline ORDER BY make", 12);
    
}
    
    
function render()    //build the searchbox view partial
    
{
        $price_opts
= array(
            
'All price ranges',
            
'New arrivals - POA',
            
'Cars under $5000',
            
'Cars $5000 and over',
        );
        
        
$data = array(
            
'select_makes'  => form_dropdown('select-make', $this->make_opts, $this->_make),
            
'select_prices' => form_dropdown('select-price', $price_opts, $this->_price)
        );
        
        return
$this->load->view('search/box', $data, TRUE);
    
}
}

Image Attachments
Untitled-1.jpg
Click thumbnail to see full-size image
 Signature 

URI Language Identifier | Modular Extensions - HMVC | Validation Callbacks into Models | View Object PHP5 | Read the User Guide

Profile
 
 
Posted: 21 February 2008 04:10 AM   [ Ignore ]   [ # 27 ]  
Summer Student
Avatar
Total Posts:  19
Joined  02-20-2008
wiredesignz - 21 February 2008 04:01 AM

I am currently working on updating an old car dealer website to use CI.

On this site we have a search form that needs managing, so I have created a module to process the form submission, populate the dropdowns from a database and render the form as a view partial. (see image)

Thats how you call them? Partials? Ok. I believe this piece of code is called from all of the site pages. Can you please tell me how do you accomplish this using CI ?
It would be great if you could upload your code.

Much appreciated.

Profile
 
 
Posted: 21 February 2008 04:16 AM   [ Ignore ]   [ # 28 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1840
Joined  06-10-2007

The Searchbox partial view code:

<div id="search-box">
    <
div class="box-inner">
        <
h1>CAR SEARCH</h1>
        
<?php echo form_open('showroom')?>
            
<p>
               <
label>Car Make<br />
                    
<?php echo $select_makes ?>
               
</label>
           </
p>
            <
p>
               <
label>Price Range<br />
                    
<?php echo $select_prices ?>
               
</label>
            </
p>
            <
input type="submit" value="Find Now!" id="submit-button" />
        </
form>
    </
div>
</
div>

The main View template has this code to load and run the search module:

<div id="left">
    
<?php echo $this->modules->run('search', $page) ?>
</div>

If you need more info, I’ll try to help. wink

Image Attachments
Untitled-2.jpg
Click thumbnail to see full-size image
 Signature 

URI Language Identifier | Modular Extensions - HMVC | Validation Callbacks into Models | View Object PHP5 | Read the User Guide

Profile
 
 
Posted: 21 February 2008 04:55 AM   [ Ignore ]   [ # 29 ]  
Summer Student
Avatar
Total Posts:  19
Joined  02-20-2008

Well, I think I am ready to give CI a try. Dude, thank you very much for the code and the time you spent in putting together the example above.  cheese

Profile
 
 
Posted: 21 February 2008 08:40 AM   [ Ignore ]   [ # 30 ]  
Grad Student
Rank
Total Posts:  87
Joined  08-23-2007

Wow, this is an amazing addition to CI, wiredesignz! It’s something I (and I’m sure many other CI-users) have been waiting for!

One question though: I would really like to use your library together with Matchbox, but in Matchbox controllers are placed in modules/module_name/controllers/controller_name. How can I call these controllers? I tried:

<?php $this->modules->run('test/controllers/test'); ?>

But of course it tries to load a controller called “controllers”, so that doesn’t work. Is there an easy way to acomplish this (maybe edit the in_subdir method?)

Another question:
If I understand it correctly, you can only render one method per module-controller (the index method). Would it also be possible to render more controller methods of the same controller as partials, e.g. render the index method, the sidemenu method and the footer method of the same controller. I hope you understand what I mean…

Anyway, this is going to make CI even more enjoyable!

Profile
 
 
   
3 of 11
3
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 719, on June 06, 2008 10:16 AM
Total Registered Members: 62663 Total Logged-in Users: 46
Total Topics: 77199 Total Anonymous Users: 2
Total Replies: 416748 Total Guests: 255
Total Posts: 493947    
Members ( View Memberlist )