Directory structure:There are basically three folders in the Codignitor directory structure ... This is app, system and user_guide. Most of the work will be done in the application folder, where, along with other folders, the model, view, controller folder will be available. Below is a picture of the directory structure.
Below is the structure of the application folder in which the developer performs most of the tasks.
How it works:Main codeignitor file - index.php, so if you run http: //localhost/ci_tutorial/index.phpin a browser, you get the same "Welcome to Codeignitor" view. Now open the route.php file,which is located in the config folder. There you will find the default controller name at the end of the file.
$route [’default_controller’] =’ welcome’;The above code specifies that by default CI launches the welcome controller. and this file is available in the Controller folder named Welcome.php. You can change the default controller name here. Once you open the Welcome.php controller in the Controller folder, you will find the index function, which is the default Welcome controller function.
public function index() {$this-> load-> view (’ welcome_message’); }The above index function loads the view file ’ welcome_message ’ from the browse folder. So the whole concept is that routing tells which one is the default controller, then clicks on that controller, and then the index function (default) of that controller will look at the file defined in that function.
The index function in any controller is the default function. To run any other controller or function (besides the index function) in the controller, you can define it manually in the URL after creating the controller and its function in the folder. Let’s see how to do it.
Create a controller Test.php (controller name must start in uppercase) in the controller folder and write the code below.
defined (
’BASEPATH’
) OR
exit
(
’No direct script access allowed’
);
class
Test
extends
CI_Controller {
public
function
index()
{
$this
-> load-> view (
’test_message’
);
}
public
function
test_demo()
{
$this
-> load-> view (
’test_tutorial’
);
}
}
?>
Now create a view file test_message.php, write the below code and save it in your watch folder. defined (
’BASEPATH’
) OR
exit
(
’No direct script access allowed’
);
?>
"en"
>
Welcome to GeeksforGeeks
< / html >
Now in your browser enter this URL http: //localhost/ci_tutorial/index.php/Test . You will find below the result
Output:
So , here in the url put the name of the controller after index.php that will run the default index.php method from the test controller.
Now create a test_tutorial.php file in your watch folder and write the code below. defined (
’BASEPATH’
) OR
exit
(
’ No direct script access allowed’
);
?>
"en"
>
This is the test tutorial
< / html >
Now run the URL http: // localhost / ci_tutorial / index.php / Test / test_demoin your browser and below is the result.
Output:
So to run any other to a function from a controller, just specify the controller name and then the function name in the URL after index.php.