CodeIgniter Training 1st part: MVC model and first steps
Created by Eric Murillo on 2019-06-11
4z.com
Table of Contents
Sending controller data to the view
In this series of trainings, we will see the basic concepts of CodeIgniter, a PHP framework that stands out for being lightweight, quick to learn and easy to use. In this first training we will study the Model View Controller and we will take the first steps in the development of a web page.
The trainings must be done in order and you cannot start a new one without having finished the previous one.
Know how to develop a website based on PHP.
Prepare a web server (XAMPP or LAMP server)
Know how to run MySQL commands to prepare the website database.
The goal of this training is to make life easier for someone who knows how web development works but needs to understand the framework used for 4z.com websites.
CodeIgniter makes use of the MVC pattern, a development pattern that separates an application into 3 layers:
· Model: it is the data access layer, in which the connections and queries to the database are made, and the data that it obtains are received by the Controller layer.
· Controller: here we find the logic of the program, this layer manipulates the information it retrieves from the database and sends it to the View. It also receives the user's actions captured by the View and responds to them.
· View: this part defines the user interface, specifies how the information obtained from the Controller will be displayed and captures the user's events.
Imagine that we have a table in the database with a list of employees, from them we store your name and your monthly salary, but we need to show a screen that shows the annual salary of each of the employees, each layer would be responsible for the next:
· Model: would establish the connection to the database and execute the query, in this case, Get the name and monthly salary of all employees.
· Controller: obtain the data and perform the necessary calculation to obtain the annual salary, in this case the monthly salary * 12, and send to the Vista layer the names of employees related to their annual salary.
· View: in this layer will be defined how the data is displayed, in our example, it could be an HTML table with two columns, Name and Salary.
During this and the following training, we will see how this example would be applied in CodeIgniter. In what remains of this first training we will study the basic operation of the controllers and how the views are displayed. The first step will be to download the framework from the official website. Once downloaded we can see the following folders:
· /application: folder with the files of our application, normally we will work here.
· /system: contains the CodeIgniter classes, which makes it work.
· /user_guide: here we have documentation and a great tutorial. It is not necessary for our website to work properly.
· index.php: this file is the starting point of our website; it must be at the root of where we want to have the page.
Before getting down to work, we will need to have a web server that shows us the pages and a database from which we can obtain the information, for this I recommend XAMPP, which will install everything we need to make these trainings. You can find a lot of trainings explaining how to install XAMPP in case you don't know the program.
XAMPP allow you to execute a webpage in your locahost based on Apache + MariaDB + PHP. Other option is to install CodeIgniter in a server configured with the CodeIgniter requirements, Linux, Apache, MariaDB and PHP. This is commonly known as a LAMP server.
First, we must copy the CodeIgniter content in our LAMP server or XAMPP installation. After that we should be able to see a dummy webpage on localhost or on the server depending of our selected option.
Let’s start with the CodeIgniter first modifications. We will first build the controller to see how it is done to execute a function in CodeIgniter, for this, we will create the employees.php file within /application/controllers and write the following code:
The code inside the index function shows the view 'main_page', now the controller would not work since the view does not exist, so we have to create the file 'main_page.php' within application/views and insert the following code:
Now we have everything necessary to understand the basic operation of the controllers in CodeIgniter. To call a function of a controller the following structure is used:
www.yourdomain.com/index.php/controller_name/function_name/argument1/argument2 ...
In this way, to be able to execute the show_home_page() function in employees.php that we have created, it will be enough to enter this URL (in future trainings we will see how to eliminate index.php from the URL):
www.yourdomain.com/index.php/employees/show_home_page
Thus, the show_home_page() function will be called, which simply executes the function $this->load->view ('main_page'), which is responsible for loading the view "main_page.php". If everything went well, we can see a page with the title "Hello 4z.com".
The last thing we will learn in this training will be how to send data from the controllers to the view, for it, we will return to our controller employees.php and we will make the following modifications in the function 'show_home_page':
What we have done has been to first add a $title argument to the function and enter it into the $data array. We send this array in the load->view function next to the name of the view. What we do with this is to allow the view 'main_page' to access the $data array. Now we will see how the 'main_page' view would look like to recover the driver's title:
As you can see, to access the value we must use a variable with the name of the key of the array in which we put it, in this case 'data_title'.
Surely you already know how to form the URL to test the operation of these modifications, remember that now we also have an argument in the function, so what is entered as an argument will be what appears in the title of the page.
At the end of the training you must submit a report with the steps to follow to perform an installation from scratch using XAMPP or LAMP. You must indicate in which folder the page is stored and provide screenshots of each step taken. This includes code and screenshots of the web views.
https://docs.switzernet.com/3/public/190612-CodeIgniter2/
https://docs.switzernet.com/3/public/190613-CodeIgniter3/
https://docs.switzernet.com/3/public/190614-CodeIgniter4/
https://docs.switzernet.com/3/public/190615-CodeIgniter5/
https://www.apachefriends.org/index.html
* * *
© 4z.com