The past 4 months I have really enjoyed the CodeIgniter framework. It has allowed for faster development time for sites which need extended functionality past the typical blog or low end content management systems. This blog is aimed to help those beginner and intermediate CI (CodeIgniter) developers over their potential hurdles. Im basing most of this off of my own experience with developing and designing within CI. Follow along with the items below and try them as you go, everything will be based off of the CI version 2+.
Learn the fundamental Libraries and Classes
Database Class
This class holds all the functions which maintain data transaction between your web server and your database. You will need to verify that this library is running in any controller which will be pulling data. I find that I load this by default in my PHP construct because I know it will always be readily available.
Since this class is NOT auto initialized, here is the code to include within your construct to load:- $this->load->database();
You can then use such functions as:
- $this->db->get('table')
- $this->db->insert('mytable', $data);
- $this->db->update('table', $data);
- $this->db->delete('table', $data);
Input Class
The input class would be the second most used class within CI. Its auto initialized so you do not have to worry about calling it every time. This class handles GET and POST variables, but also handles many functions which grab / fetch information from users on the site. Since CI does not like using the basic global variables ($_POST & $_GET) you need to use:
- $this->input->get();
- $this->input->post();
- $this->input->get_post();
Personally I like to use the function get_post() because it will handle data transaction almost as a "$_REQUEST" works. This function seems to be really helpful for automatically switching between post / get.
Routes
Routes would be a real big item within Codeigniter. They basically handle data flow into controllers so setting up the correct route table would be important. If you give your controllers random names and need to map a specific URL then you can use the same methodology as bottom:- $routes['news/(:any)'] = "news/detail/$1";
The snippet above will allow you to use the structured url "news/test-news-article" and flow correctly into the news controller. Since we are using the fragment "(:any)" it will allow for any alphanumeric field within. Its important to know you can narrow your available selectable options, but it would require you re-building.