This is my second article on CodeIgniter. I'm also new to this framework. I'll try my best to give you some knowledge. My previous blog post was based on configuration of CodedIgniter. I think you are now familiar with it. Here I'm planning to explain how controllers and views included in CodeIgniter work when we are calling for controllers for a particular purpose. Let's start!
First you need to navigate to the folder that you have set up CodeIgniter. Then open the folder using Sublime Text or any other text editer ot IDE such as PhpStom.
I explained you what happens when we call for CITEST(my CodeIgniter folder) folder using URL, in previous article. We used the below URL.
http://localhost/PHPProjects/CITEST/ OR
http://localhost/PHPProjects/CITEST/index.php/welcome/index
Here, No need to call for index function at the end of the URL according to the URL format I mentioned. Since it is the default function, it is loaded automatically. If you change the name of index function, the above URL will give an error as shown below.
Creating New Function in Welcome Controller
Now I'm going to create another function in Welcome controller file and call for it. Open the file; application/controllers/Welcome.php. After the index function, create a new function called hello.
Important :
Start the name of the controller file with an uppercase letter.
When it is called in the browser, use lowercase letters.
Start the name of the controller file with an uppercase letter.
When it is called in the browser, use lowercase letters.
Code :
public function hello() {
echo "Hello Guys!";
}
Call this hello function using below URL.
http://localhost/PHPProjects/CITEST/index.php/welcome/hello
Result :
Creating new Controllers and Views
Now you need to create a new controller and a view called Test_Controller.php (in controllers folder) and test_func_view.php (in views folder). Use a capital letter to start a name of a controller file as a practice. Place the following code in the Test_Controller.php file. Optionally, you can copy and paste the security checking part to your code which is included in your default Welcome controller. This line of code prevent the file from direct access using the URL, for others.
Code:
defined('BASEPATH') OR exit('No direct script access allowed');
Code:
defined('BASEPATH') OR exit('No direct script access allowed');
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Test_Controller extends CI_Controller {
public function index() {
echo "This is my new index";
}
public function test_function () {
$this->load->view('test_func_view');
}
}
?>
Call this file using the below URL.
http://localhost/PHPProjects/CITEST/index.php/test_Controller
Result :
You can see the text within the index function of Test_Controller file is loaded. No need of typing even the function name as I told before.
The code I mentioned above includes another function called test_function. Its content is different from the above index function. Let me explain.
$this->load->view('test_func_view'); line says to call the view file included in the application/views folder. It is the front end file. Its code is given below.
<h1>This is the output of test function</h1>
Now I'm going to call this function.
http://localhost/PHPProjects/CITEST/index.php/test_Controller/test_function
Result :
Now I think you have an idea to call a function and a view included in a controller file in CodeIgniter. Try this with some different files and create advanced view files with good designs.
Good Luck!
How CodeIgniter works
Reviewed by DAL
on
August 11, 2017
Rating:
No comments: