Today I'm just gonna ramble about some basic stuff in CodeIgniter. In an ordinary CodeIgniter project, the CRUD methods are usually found in the Models. These Models are the business domain classes that make up the M of MVC.
MVC is probably better understood as such:
Transmitting data from controller to view
There are many cases where the view needs to output data from within the controller. A good example would be showing the user's name once the user has signed in. To do this, we have to first load CodeIgniter's Parser library:
After loading the library, and after loading the desired variables into the $data array, we will call the following line of code.
Database configuration
You have two files you need to edit: 'application/config/database.php' and 'system/database/drivers/DB_driver.php'.
If you face any problems with database connectivity, do use the following link: http://codeigniter.com/forums/viewthread/190254/#898578. Appending the code to database.php will give you a better idea of what actually went wrong with the connection.
MVC is probably better understood as such:
- The models contain business data information, and are closely connected to the database.
- The controllers call the CRUD methods in the models, and process the data, then output the data to the view.
- The view will display the manipulated data in the form of a webpage.
How to call a method in the Model
In a C# context, CRUD methods would usually be static so that they can be called without an object reference. Here is a better illustration--the following will be used:
List<Users> userList = User_DA.GetUsers();...instead of
User_DA dataAccess = new User_DA();However, in CodeIgniter, the methods will not be static. This seemed strange to me at first, but maybe you will understand the rationale after seeing some sample code within the controller:
List<Users> userList = dataAccess.GetUsers();
$this->load->model('Users');The getUsers() method will go like this:
$data['users'] = $this->Users->getUsers();
$query = $this->db->get('Users');I have to admit that the CodeIgniter syntax is a little untidy, though.
return $query->result();
Transmitting data from controller to view
There are many cases where the view needs to output data from within the controller. A good example would be showing the user's name once the user has signed in. To do this, we have to first load CodeIgniter's Parser library:
$this->load->library('parser');This can be placed in the constructor of the controller (i.e. __construc()), or simply at the top of the controller method. Did you notice that in the previous example the result of getUsers() is stored in $data['users']? The $data variable is a dictionary array that contains values that will be displayed in the view.
After loading the library, and after loading the desired variables into the $data array, we will call the following line of code.
$this->parser->parse('userview', $data);Now, in the view, you can use two ways to access $data['users']:
- {users}
- <?php users ?>
Autoloading libraries, helpers, and models
CodeIgniter provides access to an array of libraries and helpers. However, to use them we will always need to write lines of code such as $this->load->library('libraryname'); and $this->load->helper('helpername').
To automatically load any libraries, helpers, or models, simply edit the autoload.php file found under 'application/config'.
$autoload['libraries'] = array('database', 'session');This line autoloads the Database and Session libraries for me--they will be needed quite often throughout the web app.
Database configuration
You have two files you need to edit: 'application/config/database.php' and 'system/database/drivers/DB_driver.php'.
If you face any problems with database connectivity, do use the following link: http://codeigniter.com/forums/viewthread/190254/#898578. Appending the code to database.php will give you a better idea of what actually went wrong with the connection.
GET and POST variables in CodeIgniter
Instead of using $__GET[] and $__POST[], we can use the following methods provided by CodeIgniter: $this->input->get() and $this->input->post(). For example, to get a Username POST variable, we write the following:
$username = $this->input->post('Username');The plus side? It returns false if the variable doesn't actually exist, instead of throwing an exception--in other words it's been safely handled. On top of that, this method is automatically loaded by the framework, so there's no need to write any code to load it.
Miscellaneous stuff
Using sessions require you to provide a 32-character long encryption key, which is defined in 'application/config/config.php', under $config['encryption_key']. The website http://www.sethcardoza.com/tools/random_password_generator/ generates such a key fast and easy.