Welcome Guest, Not a member yet? Register   Sign In
Modular Extensions - Version 4.3

[eluser]wiredesignz[/eluser]
Using methods - A mini tutorial.

First create a method. ie: modules/showroom/methods/help.php
Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
* Modular Extensions - HMVC
*
* Showroom Help method
*
* Version 0.1 (c) Wiredesignz 2008-03-10
**/
class Help extends Showroom    //extends the module important!
{
    function Help()
    {
        parent::Showroom();
    }
    
    function index()
    {
        return array(
            'output' => $this->load->view('showroom_help.htm'),
            'title'  => 'Showroom Help - ',
        );
    }
}

Three ways of using methods in your module:
Code:
<?php

// 1. Have a function representing the method
function help($data = '')
{
    $this->load->method('help');
        
    return $this->help->index($data);
}

// 2. using an error override (error is called automatically by modules::run)
function error($method, $data = '')
{
    if(!$output = parent::error($method, $data, FALSE)) // FALSE suppresses error messages
    {
        $output = modules::run('error', $method, 'error_404');    // run another module      
    }

    return $output;
}

// 3. Do nothing
The module parent::error function will try to locate the method and run it.
A fatal error message will be displayed if the method is not found.

[eluser]wiredesignz[/eluser]
Version 4.0.17 is available on the wiki
Corrected method home path to match module home path

[eluser]jaume[/eluser]
can't get this to work...

When remapping to the default module/method all is ok, but when typing a module in the adress it says site.com/module does not exist. I've picked and modified the code from 4 different threads... How about some example app and/or documentation? It would be really appreciated! Wink

This is my default controller:

Code:
class default_controller extends Controller {

    function __construct()
    {
        parent::Controller();
        $this->load->helper('modules');
    
    }
    var $method;
   var $data=array();
  
   function _remap()
   {
        ($this->uri->segment(1) === FALSE) ? $module = 'home' : $module = $this->uri->segment(1);
        ($this->uri->segment(2) === FALSE) ? $method = 'index' : $method = $this->uri->segment(2);
        
      $content = modules::run($module, '', $method);    
        
      $data['title']   = $content['title'];
      $data['maincontent'] = $content['maincontent'];
      
      $this->load->view('main_view', $data);
   }
}

this is the routes.php

Code:
$route['default_controller'] = "default_controller";

$route['scaffolding_trigger'] = "";


$route['(.*)'] = 'default_controller/$1';

This is a testing module:

Code:
class Home extends Module {

    function __construct()
    {
        parent::Module();
    
    }
    function index()
   {
       return array(
            'title' => "This is home",
            'maincontent'  => "What a content! ;)"
       );
   }
}

Any thing else you need to check it?

TIA.

Jaume.

[eluser]wiredesignz[/eluser]
Are you also using .htaccess and have you set $config['index_page'] = ""; ?

Mybe you should try the example shown on the wiki to get familiar with ME.

[eluser]wiredesignz[/eluser]
Version 4.0.18 is available on the wiki
Added a few comments to the code
Fixed a bug when instantiating module methods

[eluser]wiredesignz[/eluser]
If you asked for a tutorial, here we go, I grabbed Developer13's Inktype blog application and ported it over to use modules. Thanks D13 Wink
Here's a few code samples:

application/controllers/public_controller.php
Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
* Public controller handles all requests
*
* Requires .htaccess
*
* $config['index_page'] = "";
* $route['(.*)'] = 'public_controller';
*
**/
class Public_Controller extends Controller
{
    function Public_Controller()
    {
        parent::Controller();
        //load the template model
        $this->load->model('mdl_templates');
    }
    
    function _remap()
    {        
        //get the route or set the defaults
        $module = 'blog';
        $method = $this->uri->segment(2) OR $method = 'home';
        
        // get some settings for the blog
        $settings = modules::run('blog/settings');
        
        //load the theme
        $template = $this->mdl_templates->get_default_template();
        
        $templates = explode(',', $template->loadorder);
        $templateName = 'templates/' . $template->name . '/';

        //render the partials
        foreach ($templates as $partial)
        {
            if ($partial == 'content')
            {
                //output the content view
                $content = modules::run($module, $templateName, $method);
                $this->output->append_output($content);
            }
            else
            {
                //output the fixed views
                $this->load->view($templateName.$partial, $settings);
            }
        }
    }
}
application/modules/blog/controllers/settings.php
Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
* Blog settings
*
**/
class Settings extends Module
{
    function Settings()
    {
        parent::Module();
        //load the settings models
        $this->load->model('mdl_settings');
        $this->load->model('mdl_categories');
        $this->load->model('mdl_blogroll');
        $this->load->model('mdl_pages');
        
        //set up the pages model
        $this->mdl_pages->publishedonly = TRUE;
    }
    
    function index()
    {
        // get the blog settings
        $settings = $this->mdl_settings->get();    
        $settings->categories = $this->mdl_categories->get();
        $settings->blogroll = $this->mdl_blogroll->get();
        $settings->pages = $this->mdl_pages->get();
        
        return $settings;
    }
}
application/modules/blog/controllers/blog.php
Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
* Blog module
*
**/
class Blog extends Module
{
    var $posts;
    var $page;
    var $numpages;
    var $prevpage;
    var $nextpage;
    var $nav;
    
    function Blog()
    {
        parent::Module();
        //load the utility helpers
        $this->load->helper('utils');
        
        //set up the posts model
        $this->load->model('mdl_posts');
        $this->mdl_posts->parse_vars = TRUE;
        $this->mdl_posts->paginate = TRUE;
        $this->mdl_posts->publishedonly = TRUE;
    }
    
    function error($method, $data = '')        
    {
        //trap unknown method calls, suppress error message
        if ($content = parent::error($method, $data, FALSE))
        {
            return $content;
        }
        
        return 'Page not found';
    }
}

[eluser]wiredesignz[/eluser]
Tutorial Continued...
application/modules/blog/methods/home.php
Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
* Blog home method
*
**/
class Home extends Blog
{
    function Home()
    {
        parent::Blog();
    }
    
    function index($templateName)
    {        
        //get the page from the route data
        $uri = $this->uri->uri_to_assoc(3);
        $this->page = $uri['page'] OR $this->page = 1;

        //set up the posts model
        $this->mdl_posts->page = $this->page;
        
        //get the post data
        $this->posts = $this->mdl_posts->get();
        $this->numpages = $this->mdl_posts->numpages;
        
        //run the utilities
        utils::prep_categories();
        utils::paginate();
        
        //build the content
        return $this->load->view($templateName.'index', $this);
    }
}
Probably needs more error checking but the basics are there. Thanks for your interest.

[eluser]Avatar[/eluser]
thank you, makes sense I can see how this is beneficial compared to modules within modules. Smile In alot of ways. This way you make one main module and just extend all the rest with different template/data and/or such inside the methods. Thank you for showing how to tie this things all together. Can we see your models? Are they HOT?
Please checkout this thread I pasted if you have time.
http://ellislab.com/forums/viewthread/73853/ SmileI know you want one of those transparent lcd screens Smile Who doesn't? off topic, sorry. ;-)

[eluser]jaume[/eluser]
[quote author="wiredesignz" date="1205205011"]Are you also using .htaccess and have you set $config['index_page'] = ""; ?[/quote]

He comes my .htaccess:
Code:
- - -
RewriteEngine on

RewriteCond $1 !^(index\.php|css|img)

RewriteRule ^(.*)$ index.php/$1 [L]
- - -

And, yes, I've set the $config['index_page'] = "";

I'll check the code you have posted.

Thanks,

Jaume.

[eluser]wiredesignz[/eluser]
Hey jaume, You may wan't to use the forum [ code ] tags, when posting code, it makes reading your submission easier. Thanks.




Theme © iAndrew 2016 - Forum software by © MyBB