Welcome Guest, Not a member yet? Register   Sign In
Aliases [Helper]
#1

[eluser]DeaD SouL[/eluser]
I just wanted to share it with you

INSTALLATION:

1) create a new file in system/application/helpers/aliases_helper.php
2) add the following code into that file:
Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
* Aliases helper
*
* @package        Aliases
* @subpackage    Helpers
* @category    Helpers
* @author      Mubarak Alrashidi
* @version     0.0.1
* @copyright   Copyright (c) 2010-2011 Mubarak Alrashidi, (DeaDSouL)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/


$CI =& get_instance();

if ( ! function_exists('set_alias'))
{
    function set_alias($aliases = '', $classes = '')
    {
        global $CI;
        if ( is_array( $aliases ) )
        {
            foreach ( $aliases as $alias => $class)
            {
                $return[$alias] =& $CI->$class;
            }
        }
        else
        {
            $return[$aliases] =& $CI->$classes;
        }
        return $return;
    }
}

if ( ! function_exists('get_aliases'))
{
    function get_aliases()
    {
        global $CI;
        $CI->config->load('aliases');
        return set_alias( config_item( 'aliases' ) );
    }
}


/* End of file aliases_helper.php */
/* Location: ./system/application/helpers/aliases_helper.php */

3) create a new file in system/application/config/aliases.php
4) add the following code into that file:
Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/*
|--------------------------------------------------------------------------
| Default Aliases Array
|--------------------------------------------------------------------------
*/

//$config['aliases']['alias_name_1_here'] = 'class_name_1_here';
//$config['aliases']['alias_name_2_here'] = 'class_name_2_here';
//$config['aliases']['alias_name_3_here'] = 'class_name_3_here';

/* End of file aliases.php */
/* Location: ./system/application/config/aliases.php */

5) loading the helper file:
A. Manually:
Code:
$this->load->helper( 'aliases' );
B. Automatically: by editing system/application/config/autoload.php, and add aliases
Code:
$autoload['helper'] = array( 'aliases' );



USING ALIASES:

1) set an alias to ($this-some_model_name)
extract( set_alias( 'm_name', 'some_model_name) );

set_alias():
This function has two parameters
First: the name of the alias
Second: the name of the class (model,library,..etc)
example 1:
we have this model (user_profile_model), and we'd like to make an alias for it
Code:
extract( set_alias( 'profile_m', 'user_profile_model' ) );
// so instead of using this:
$this->user_profile_model->get($id);
// we'll be able to use:
$profile_m->get($id);

example 2:
setting more than one alias at once:
Code:
$array_alias = array
(
    'profile' => 'user_profile_model',
    'valid' => 'form_validation',
    'img' => 'image_lib'
);
extract( set_alias( $array_alias ) );
// then just use them:
$profile->get($id);
$valid->set_rules( 'email', 'Email', 'required' );
$img->display_errors();


get_aliases():
This function is used to get the default aliases that you specified in system/application/config/aliases.php
instead of setting aliases in every page, you can specify the most used class that you'll need in the config aliases file.

example:
editing system/application/config/aliases.php:
Code:
$config['aliases']['profile']  = 'user_profile_model';
$config['aliases']['valid']    = 'form_validation';
$config['aliases']['img']      = 'image_lib';

simply we do:
Code:
// this will generate the aliases
extract( get_aliases() );
// then we just start using them
$profile->get($id);
$valid->set_rules( 'email', 'Email', 'required' );
$img->display_errors();


Why Aliases?:
1) forget about "$this->".
2) creating a short name for long classes name.
3) it just make sense to have a name that express what it does.
4) save your time (because you won't have to write $this-> every time).
5) hmmmmm, what else Smile


Regards




Theme © iAndrew 2016 - Forum software by © MyBB