Welcome Guest, Not a member yet? Register   Sign In
Modular Extensions - Modules
#1

[eluser]wiredesignz[/eluser]
Here's an example for a ME Validation Module which is totally self contained, it loads the CI Validation library, sets up the fields and rules, and maps its own class variables into the Validation Object.
Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Login_validation extends Controller
{
    function Login_validation()
    {        
        parent::Controller();
        $this->load->library('validation');
        
        /* set up the validation object */
        
        $this->validation->set_error_delimiters('', '');
    
        $this->validation->set_fields(array(
            'username' => 'Username',
            'password' => 'Password',
            'remember' => 'Remember'
        ));
        
        $this->validation->set_rules(array(
            'username'     => 'trim|required',
            'password'     => 'trim|required',
            'remember'  => ''
        ));
        
        /* reference the validation object variables */
        
        $this->username =& $this->validation->username;
        $this->password =& $this->validation->password;
        $this->remember =& $this->validation->remember;

        $this->error_string =& $this->validation->error_string;
    }
    
    function run()
    {
        return $this->validation->run();
    }
}

Usage:
Code:
modules::load('login_validation');

if ($_POST)
{
    if ($this->login_validation->run())
    {
        $username = $this->login_validation->username;
        $password = $this->login_validation->password;
    }
}
#2

[eluser]Avatar[/eluser]
COOL Smile
#3

[eluser]badgeek[/eluser]
how to get module name from a model loaded from module? Tongue tried $this->_parent_name but it return the model name

thanks
#4

[eluser]wiredesignz[/eluser]
I dont recommend trying to call methods in a module from a model, it breaks the MVC design pattern.

Plus this question is basic OOP theory you need to know.

However, You can do this in the parent module: Tongue
Code:
class What_ever extends Controller
{
    function What_ever()
    {
        parent::Controller();
        $this->load->model('some_model');
        //$this->some_model->_parent =& $this;
        $this->some_model->_parent = get_class($this); // just supply the module name
    }
}

And parent isn't really a good description, the module is more like the owner
#5

[eluser]badgeek[/eluser]
hmm i dont need to call module function from a model, just want to retreive the module name is that possible?
#6

[eluser]wiredesignz[/eluser]
I just showed you how to do it badgeek Tongue
#7

[eluser]badgeek[/eluser]
ahhh ok hehe thanks,
#8

[eluser]wiredesignz[/eluser]
Default Controller runs a module from the URL
Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
* Modular Extensions - HMVC
*
* Default controller runs a module from the URL
*
* Version 0.1 (c) Wiredesignz 2008-03-09
**/
class Default_controller extends Controller
{
    function Default_controller()
    {
        parent::Controller();
        modules::load('search');    //required by all modules
    }

    function _remap()
    {
        $module = $this->uri->segment(1) OR $module = 'home';
        $method = $this->uri->segment(2) OR $method = 'index';
        
        $content = modules::run($module, '', $method);    
        
        $this->load->view('default_layout', $content);
    }
}
Routes
Code:
$route['(.*)'] = 'default_controller';
#9

[eluser]wiredesignz[/eluser]
The Home Module
Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
* Modular Extensions - HMVC
*
* Home module
*/
class Home extends Controller
{
    function Home()
    {
        parent::Controller();
    }
    
    function index()
    {
        $this->search->vehicles_model->findPaged("featured = 'Yes'", 4);
        
        $data['home'] =& $this; //the view has access to $this
        
        return array(
            'output' => $this->load->view('home_content', $data),
            'title'  => 'Home - '
        );
    }
    
    function featured()        //used by home_content view
    {
        $vehicle = $this->search->vehicles_model->iterate();
        
        return ($vehicle) ? $this->load->view('hot_pick', $vehicle) : FALSE;
    }
}
#10

[eluser]Mikle[/eluser]
Does this example work with latest ME? I need to set title, meta tags and content from modules...




Theme © iAndrew 2016 - Forum software by © MyBB