Welcome Guest, Not a member yet? Register   Sign In
How to make a reusable base_url_admin() function?
#1

[eluser]term25[/eluser]
I need to setand reuse a function similar to base_url(), but with url to admin folder/controller e.g. base_url_admin() so when I use this function it will output the path to admin:

Code:
<?php

echo base_url();
// will produce http://localhost/mywebsite

echo base_url_admin();
// will produce http://localhost/mywebsite/admin123

?>

What is the best practice to create these reusable functions in CI?
#2

[eluser]Leo78[/eluser]
Create a file in helpers folder (in the application) - MY_url_helper.php

Then add this code:

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

function base_url_admin()
{
    return base_url() . 'admin123';
}
#3

[eluser]Aken[/eluser]
I'd suggest using site_url() if you're dealing with URLs and links to pages on your website. base_url() is more for static assets.

I'd also put the suffixed content into the function's parameter, rather than outside. That'll guarantee a proper URL, just in case you toss in a slash in the wrong place or something by mistake.

Code:
function admin_url($url = '')
{
    return site_url('admin123/'.$url);
}
#4

[eluser]term25[/eluser]
Thanks guys. Probably using helper is the way to go, as you have suggested.




Theme © iAndrew 2016 - Forum software by © MyBB