Welcome Guest, Not a member yet? Register   Sign In
Extending url_helper.php (Possible Tip!)
#1

[eluser]SpYk3[/eluser]
Not sure if this is the right place to post this, not sure if this is even good practice.
However, I've got tunnel vision and need a break from coding for a minute, so I'll spend that minute sharing something and asking the great "Guru's" if this is decent/good/bad practice.

As the title suggest, I've made some helpful "extensions" to my CI url_helper.php (located usually in system/helpers). Keep in mind as you read this, I'm about 4 months new to CI coming from a long line of .Net languages.

The additives I've made are nothing very extensive and are meant only to help with simple calls, such as header calls to a JavaScript library.

I placed these funcs just beneath the line separating Base URL from Current URL, as base_url() is used repetitively.

Again, before I show code, Noob Status = Somewhat, so before anyone goes cutting my throat, please provide a good reason not to do this.

Code:
/**
* CSS URL
*
* Returns the "css_url" item from your config file
*
* @access    public
* @return    string
*/
if ( ! function_exists('css_url'))
{
    function css_url($uri = '')
    {
        if ($uri === '') {return base_url().'css/';}
        else {return base_url().'css/'.$uri;};
    }
}

// ------------------------------------------------------------------------

/**
* Javascript URL
*
* Returns the "js_url" item from your config file
*
* @access    public
* @return    string
*/
if ( ! function_exists('js_url'))
{
    function js_url($uri = '')
    {
        if ($uri === '') {return base_url().'js/';}
        else {return base_url().'js/'.$uri;};
    }
}

// ------------------------------------------------------------------------

/**
* Images URL
*
* Returns the "images_url" item from your config file
*
* @access    public
* @return    string
*/
if ( ! function_exists('images_url'))
{
    function images_url($uri = '')
    {
        if ($uri === '') {return base_url().'images/';}
        else {return base_url().'images/'.$uri;};
    }
}

// ------------------------------------------------------------------------

If this is helpful to anyone, well then your welcome, otherwise, eh, whatev...

Keep in mind, you may need to adjust the code some if your root directory varies from the "norm" (ie. js, css, and images folders are usually placed in root directory).
#2

[eluser]misplacedme[/eluser]
I wouldn't separate each individual type out or force them to use a specific folder. My css folder is /styles, my javascript is /scripts and my images is /img.

Instead, I'd combine the three functions into a "create asset" function. This allows you to have more than the three types.
Code:
if ( ! function_exists('create_asset'))
{
    function create_asset($uri = '')
    {
        if ($uri === '') {return base_url();}
        else {return base_url().$uri;};
    }
}
#3

[eluser]stuckinphp[/eluser]
I'm in the eh whatev basket too:

Code:
if ( ! function_exists('asset_url'))
{
    function asset_url($uri = '')
    {
       return base_url().'assets/'.$uri;
    }
}

But I must say, thanks for the post, always nice to see others example code to make sure the rest of us aren't doing something horrbibly overdone. And you posted in the right place too fyi.
#4

[eluser]InsiteFX[/eluser]
You should not touch the original CI url_helper !

You create a MY_url_helper in application/helpers

InsiteFX
#5

[eluser]SpYk3[/eluser]
@misplacedme: Not a bad idea, would provide more flexibility, think I'll add it in as a function. I still like the idea of direct folder calls i can tie specific file names in, if for nothing more than easy readability when i have to pass the code on to a maintenance engineer or something of that sort. The easier the API is to follow when you deliver a finished product, the less service calls you have to answer personally.
@InsiteFX: Yeah, my coworker says the same thing, but I come from a .net world where more files = more overhead, so its a force of habit to want to mod everything that is as "ground level" as possible
#6

[eluser]SpYk3[/eluser]
On another note, all the return statements can be simplified as such:

Code:
/*    Old Code    */
    if ( ! function_exists('create_asset'))
    {
        function create_asset($uri = '')
        {
            if ($uri === '') {return base_url();}
            else {return base_url().$uri;};
        }
    }
Code:
/*    New Code    */
    if ( ! function_exists('create_asset'))
    {
        function create_asset($uri = '')
        {
            return ($uri === '' ? base_url() : base_url().$uri);
        }
    }

*Update: A coworker of mine made a good point, one line if statements don't tend to do so well on auto-document progs and can cause some confusion among the those who have to support it, however, i feel if it's in the back end and not necessarily something that will need more doc than the command line and param, then do it how you want, because at that level, you'll be the one maintaining it anyway. The one call comment earlier bout ease of reading was for end user/maintenance api, in that they only need know the simple command and the param
#7

[eluser]InsiteFX[/eluser]
When I start getting to many files then I will create library like utils and add the methods in there that I use the most.

InsiteFX
#8

[eluser]SpYk3[/eluser]
also a grand idea. I'll work on a library later, i'm kinda under pressure to finish a dead project in a quick hurry. long story.
But anyway, yeah, a library would make more since, especially since all i'm doing is moding a library to begin with. But if i library it, it will come at a time when i have more time to review a full url_help extender and make a full set of functions worth making a small library for. I know there are already tons of plugins and the such available, so if i share anything, it will probably just be simple things like this. Simple "tips" that I use, namely, not just to help others, but to get good feedback like this.
Anyway, i'm rambling, lol. Back to the grind!
#9

[eluser]stuckinphp[/eluser]
Already a couple around, never liked them enough to use them though.

https://tohin.wordpress.com/2008/12/22/a...deigniter/
http://codeigniter.com/wiki/Asset_Helper/




Theme © iAndrew 2016 - Forum software by © MyBB