Search code examples
phpcodeignitercodeigniter-2codeigniter-urlcodeigniter-routing

How to extend anchor() function to anchor_admin() in CodeIgniter?


I would like to create my own function called anchor_admin() based on anchor() function in CodeIgniter.

I was thinking like:

I have defined admin path in config.php file e.g. like this:

$config['base_url'] = '';
$config['base_url_admin']   = 'my-custom-admin-folder';

and then

I need somehow create a new anchor_admin() function that extends anchor() function.

so instead of typing:

<?php echo anchor('my-custom-admin-folder/gallery', 'Gallery', 'class="admin-link"'); ?>

I would type only:

<?php echo anchor_admin('gallery', 'Gallery', 'class="admin-link"'); ?>

But the output wold be always:

<a href="http:/localhost/my-custom-admin-folder/gallery" class="admin-link">Gallery</a>

Basically I only need to ad the config variable $this->config->item('base_url_admin') at the end of the url generated by the core anchor() function.

How to do that?

Which files do I nned to create and where to put?

I guess creating a helper is not the way to go.

Should I create a library or could it be put as a function within my MY_Controller file in core folder of my application that I already have created and I am using it to load some stuff already?


Solution

  • In CodeIgniter you can 'extend' helpers ('extend' being a catch all term in this case as they're not actually classes). This allows you to add your own helper functions that will be loaded with the standard ones (in your case, the URL Helper).

    It's explained in the CodeIgniter docs here - http://ellislab.com/codeigniter/user-guide/general/helpers.html

    In your case you would want to do the following:

    1- Create the file MY_url_helper.php in application/helpers/

    2- Create your anchor_admin() function as below:

    function anchor_admin($uri = '', $title = '', $attributes = '') {
    
        // Get the admin folder from your config
        $CI =& get_instance();
        $admin_folder = $CI->config->item('base_url_admin');
    
        $title = (string) $title;
    
        if ( ! is_array($uri)) {
    
            // Add the admin folder on to the start of the uri string
            $site_url = site_url($admin_folder.'/'.$uri);
    
        } else {
    
            // Add the admin folder on to the start of the uri array
    
            array_unshift($uri, $admin_folder);
    
            $site_url = site_url($uri);
    
        }
    
        if ($title == '') {
    
        $title = $site_url;
    
        }
    
        if ($attributes != '') {
    
        $attributes = _parse_attributes($attributes);
    
        }
    
        return '<a href="'.$site_url.'"'.$attributes.'>'.$title.'</a>';
    
    }
    

    3- Use the helper and function how you normally would:

    $this->load->helper('url');
    
    echo anchor_admin('controller/method/param', 'This is an Admin link', array('id' => 'admin_link'));
    

    Hope that helps!