CodeIgniter helpful hints |
[eluser]Pygon[/eluser]
Mine is a personal tip regarding extending libraries/etc: Where possible, use the original library methods but wrap them in code or replace the code using a condition to extend the functionality rather than duplicating and changing the original. Makes it quite a bit easier to upgrade if the original function changes. MY_Lib.php Code: <?php I find that I can accomplish most tasks using this method with very little copied code and it is easier to update should the base function change (since I don't have to figure out what I changed). Not an end-all be-all but more of a preference for keeping things clean and updateable.
[eluser]Lone[/eluser]
Heres a new one for helping with $_POST data as I wanted an easy to use function that gets all post data and xss_cleans it for me without setting the config variable required to do this. Best used on a page that is receiving form data eg. 'do_add_item' Extend the Input library with the following function: Code: function post_all() { To use in a controller etc. just use... Code: $post = $this->input->post_all(); Let me know what you think of this method please if there is something wrong/awesome about it
[eluser]JOKERz[/eluser]
SUPERB THREAD!! Should be sticky. Thanx for all the hints guys...
[eluser]xwero[/eluser]
Reviving the thread. Try to take advantage of the class variables. An example
[eluser]BaRzO[/eluser]
i am using in index.php BASEDIR cons to find where is the base directory. Code: define('BASEDIR', realpath(dirname(__FILE__)).'/');
[eluser]Lone[/eluser]
I just updated my above post to handle arrays in $_POST as well as it errored out on it today for us.
[eluser]xwero[/eluser]
a few php hints : - Instead of doing this Code: $i = 0; Code: foreach($array as $key => $item) - Instead of doing this Code: substr($str,0,strlen($str)-1); Code: substr($str,0,-1); Learn from my code sins
[eluser]xwero[/eluser]
a comment hint : If you use the curly backet new line rule to format your code you could put single line comments behind the opening backet. Code: // some comment Code: if($true)
[eluser]xwero[/eluser]
Keeping the thread warm. If you keep CI above the public directory it's a bit tricky to move from one server to another because the full path is added to the bootstrap file (index.php). To make it easier on yourself you can move the EXT, FCPATH and SELF constant creation above the $system_folder and $application_folder variables. This makes it possible to create a base_folder like this Code: $base_folder = str_replace('public/'.SELF,'codeigniter/',FCPATH); In this example CI is located in a directory on the same level as the public directory. This $base_folder variable you can be used to prefix the $system_folder and $application_folder variables. Together with the Automatic config[base_url] it saves you a few headaches
[eluser]xwero[/eluser]
back with some more hints: This time about the uri library To get the last segment you can do Code: $this->uri->segment($this->uri->total_segments()); To check if a segment value is present Code: in_array('value',$this->uri->segment_array()); |
Welcome Guest, Not a member yet? Register Sign In |