简体   繁体   中英

Extending base_url() to base_url_admin() not working properly in CodeIgniter

My config config_backend.php (autoloaded) looks like this:

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

$config['my_admin_url'] = 'admin';

My helper admin_helper.php looks like this:

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

function base_url_admin()
{
    $ci =& get_instance();

    $ci->config->load('config_backend');
    $ci->load->helper('url');

    return base_url().$my_admin_url.'/';
}

My code in the view looks like this:

<a href="<?php echo base_url_admin(); ?>">Admin</a>

But the problem is that instead of the correct output like:

<a href="http://localhost/admin">Admin</a>

The link looks like:

<a href="http://localhost">Admin</a>

Funny thing is that when I do in helper eg this:

function base_url_admin()
{
    $ci =& get_instance();

    $ci->config->load('config_backend');
    $ci->load->helper('url');

    return "idiot";
}

it outputs something like:

http://localhost/admin/idiot

So, I assume that admin_helper is loaded, but I don't know how the admin appear there?

Any idea what am I doing wrong?

You are supposed to return the my_admin_url config because you are not picking up the config item, you expect it to return it and/or the config returns a variable under the array key name ie $my_admin_url . But to return it, you can do this:

return base_url() . $ci->config->item('my_admin_url') . '/';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM