CodeIgniter: Tips & Tricks

Hello, Today I’ll post few modifications I use on every CI project. First, on the “url_helper“, I made this modification oon base_url() function:

[cc lang=”php”]
if ( ! function_exists(‘base_url’))
{
function base_url($link=FALSE)
{
$CI =& get_instance();
$baseUrl = $CI->config->slash_item(‘base_url’);
if ($link===TRUE) $baseUrl .= $CI->config->slash_item(‘index_page’);
return $baseUrl;
}
}
[/cc]

This allow me to use the base_url() function to work with and without the index.php. Sometimes you don’t know where your project will be hosted, and if the host supports mod_rewrite. In this case, on your links (those with index.php/controller) you just put

[cc lang=”php”]

[/cc]

and you will get the index.php or not, depending on your configuration, on config.php. If I use the TRUE parameter, the “index.php” string will be inserted before the controllers. Of course, you can alter the function to something like base_url(‘link’), or base_url(1), or anything you like. I don’t like to mess with the CI Core, so I saved this new helper on application/helpers directory.

Another Tip. This is very simple. Using the form validation, when I have a not required field, it isn’t repopulated with set_value() function if you don’t set any rules for the field. But if you set a blank rule, it will work:

[cc lang=”php”]
$this->form_validation->set_rules(‘field_name’, ”, ”);
[/cc]

When I use upload class, sometimes I have to delete the file when I change the file on database. And then It’s a good thing to use the absolute path when deleting, so I made a small constant declaration to set my root folder, the folder with index.php inside. In the index.php, add this line on bottom of the constant declarations:

[cc lang=”php”]
define(‘FULLPATH’, pathinfo(__FILE__, PATHINFO_DIRNAME));
[/cc]

Now, my FULLPATH constant is set with my base directory. Hope this can be usefull to someone. 😉

Did you like this? Share it:

7,509 Comments

  • Unblocked |

    I like the valuable info you provide in your articles.

    I will bookmark your weblog and test again right here regularly.
    I am moderately certain I will learn many new stuff right here!
    Best of luck for the next!

  • buy cbd oil |

    very handful of sites that happen to be detailed below, from our point of view are undoubtedly properly really worth checking out

  • Situs Resmi Judi Sabng Ayam |

    I’m really impressed with your writing skills as well as with the
    layout on your weblog. Is this a paid theme or did you modify it yourself?
    Either way keep up the nice quality writing, it’s rare to see a nice blog
    like this one today.

  • sbobet |

    Some truly marvelous work on behalf of the owner of this web site, perfectly outstanding content.

  • Ashdycle |

    tadalafil 5mg daily (Cialis) is suggested to help with particular cases of erectile disorder. Never ever share Tadalafil regarding other individuals who have not been examined by a certified healthcare supplier, as there could be a number of health disorders that will certainly avoid you from safely taking this drug. An overdose of Tadalafil is anticipated to generate the following symptoms: breast discomfort, nausea, irregular heart beat, fainting, and light-headedness.

  • Ugodycle |

    If you have any kind of things that are unclear about your procedure – do not be reluctant to ask your medical service provider for added info and expert recommendations. Since Tadalafil is known to offer the patient up to 36 hrs of efficiency, taking it a lot more often than that does not seem needed anyway. A few mild side impacts like stuffy nose, indigestion, back pain, headache, drippy nose, muscular tissue aches or flushing can happen at the beginning of the therapy yet normally disappear really quickly. It’s simply that common Tadalafil Where To Buy could be produced by an additional business, but all the international criteria are very carefully noted. If you have any kind of inquiries make certain you read through the patient’s brochure that has this medicine.

  • Boodycle |

    Take this medicine precisely as routed without going over the amount or taking it more often than suggested. We prepare to help you be finished with your shopping within simply a couple of times because we did a great deal of study to make it possible. tadalafil 6mg capsule functions in your body by assisting you make sure you constantly react appropriate to sexual stimulation.

So, what do you think ?