Welcome Guest, Not a member yet? Register   Sign In
Global Includes?
#1

[eluser]SplashCreativity[/eluser]
Hey everyone, I'm not new to MVC frameworks though I am relatively new to codeignitor.
What can I say, I love it!

I have an issue though, I have my welcome controller for the first page, which goes something like this:

Code:
function index()
    {
        $header['title'] = "Auto Jantes";
        $header['keywords'] = array("Auto", "Jantes");
        $header['description'] = "Development Website";
        $header['content_title'] = "Bienvenue chez AutoJantes";
        
        $this->load->helper('form');
        
        $header['email'] = array(
            'name'      =>    'email',
            'value'     =>    'Adresse email',
            'class'     =>    'menu_input',
            'onClick'    =>    'this.value=\'\''
        );
        
        $header['password'] = array(
            'name'      =>    'password',
            'value'     =>    'Mot de passe',
            'class'        =>    'menu_input',
            'onclick'    =>    'this.value=\'\''
        );
        
        $header['submit'] = array(
            'name'        =>    'submit',
            'value'        =>    'Connexion',
            'class'        =>    'menu_submit'
        );
        
        $header['register'] = array(
            'name'        =>    'register',
            'content'    =>    'Joindre',
            'class'        =>    'menu_submit',
            'onClick'    =>    'location.href=\'register\''
        );

        $this->load->view('template/header', $header);
        $this->load->view('welcome');
        $this->load->view('template/footer');
    }

As you can see I am using the form helper in my header, this is because the header page has a box which asks users to login (shown on each page) unless they are logged in it will show a menu.

My issue is, I don't want to have to place the 17 lines for that login form on every controller / controller function I make. That would suck, instead I'm wondering is there no easy way of including the form helper stuff on every controller by default?


Best Regards
Stephen
#2

[eluser]stuffradio[/eluser]
An easy way would be to make a helper with this code in it... make it so all you have to do is load the helper and maybe execute the function.
#3

[eluser]SplashCreativity[/eluser]
That worked perfectly, just for anybody who doesn't understand and may be experiencing the same problem.

1. I created a new file: /system/application/helpers/login_helper.php
2. Added the code for the file in a function.

Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

function get_form()
{
    $header['email'] = array(
        'name'      =>    'email',
        'value'     =>    'Adresse email',
        'class'     =>    'menu_input',
        'onClick'    =>    'this.value=\'\''
    );
    
    $header['password'] = array(
        'name'      =>    'password',
        'value'     =>    'Mot de passe',
        'class'        =>    'menu_input',
        'onclick'    =>    'this.value=\'\''
    );
    
    $header['submit'] = array(
        'name'        =>    'submit',
        'value'        =>    'Connexion',
        'class'        =>    'menu_submit'
    );
    
    $header['register'] = array(
        'name'        =>    'register',
        'content'    =>    'Joindre',
        'class'        =>    'menu_submit',
        'onClick'    =>    'location.href=\'register\''
    );
    
    return $header;
}

?>

3. I edited my autoload.php to include the helper 'login'

4. In my controller I used the code
Code:
$header = get_form();
$header['title'] = "Auto Jantes";
$header['keywords'] = array("Auto", "Jantes");
$header['description'] = "Development Website";

Take notice of the get_form (because I returned the $header variable in my function, it added the variables to my $header variable in my controller)

Thank you very much stuffradio

Regards
Stephen
#4

[eluser]stuffradio[/eluser]
Glad to help Smile
#5

[eluser]Bramme[/eluser]
The solution Brandon Dickson brings in this topic could be off value to you too!
#6

[eluser]Skulls[/eluser]
helpers are made for flexibility functions...
the $header stuff i see there is static... you should throw that $header information in a config and then set to autoload the config file or load the config where you need it

the config file:
Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    $header['email'] = array(
        'name'      =>    'email',
        'value'     =>    'Adresse email',
        'class'     =>    'menu_input',
        'onClick'    =>    'this.value=\'\''
    );
    
    $header['password'] = array(
        'name'      =>    'password',
        'value'     =>    'Mot de passe',
        'class'        =>    'menu_input',
        'onclick'    =>    'this.value=\'\''
    );
    
    $header['submit'] = array(
        'name'        =>    'submit',
        'value'        =>    'Connexion',
        'class'        =>    'menu_submit'
    );
    
    $header['register'] = array(
        'name'        =>    'register',
        'content'    =>    'Joindre',
        'class'        =>    'menu_submit',
        'onClick'    =>    'location.href=\'register\''
    );
    
    $config['header']=$header;

?>

and then in the controller you have (after the config file is autoloaded or manualy loaded)

Code:
$header = $this->config->item('header');
$header['title'] = "Auto Jantes";
$header['keywords'] = array("Auto", "Jantes");
$header['description'] = "Development Website";

in conclusion, you already have the config functions to get and store the static information.. so it makes no sense to make a helper for something already built Big Grin
#7

[eluser]xwero[/eluser]
I would use the Template inheritance helper this keeps it all in the view files where it is supposed to be.
#8

[eluser]GSV Sleeper Service[/eluser]
onclick?! ugh... nasty




Theme © iAndrew 2016 - Forum software by © MyBB