Setting up and understanding the Codeigniter framework
Now download the codeigniter framework from it's official website and place that in the root directory of server configured with PHP. Look into the directory structure of codeigniter framwork files. Now i'm going to discuss about the folders and files included in its directory.
and the shown folders and files are included
in the structure of codeigniter framework.
Here also a folder in the above directory named as "system" in which library class functions of Codeigniter framework are included. By using the system library of CI, one can easily make a good app rapidly .All one has to do is just follow the rules of working in CI framework.
Lets talk about its working and directory Structure-
first we would discuss about each folder included in its application folder because it is most essential and will help to understand it's working.
1.cache: At first its not necessary to know about this folder and it's use. but you can understand it as it is used for saving the cached files in it. In this directory a .htaccess file is also placed which is used to secure data and files in this directory.
2.config: This folder contains setting and config files in it . so several files are included in it
A.config.php: In this file some configurations are done using $config array like this:
$config['encryption_key'] = 'encrypted_key';//setting encryption key
B.database.php: In this file database configurations are done. so this file play a very important role in Codeigniter framework . so before looking at this tutorial please open up this file in text editor and look it on your own to understand how configurations are done in this file.
Now lets have a look how username , password , database_name ,and any other info are configured in this file:
$db['default'] = array(
'dsn' => '',
'hostname' => "host_name",
'username' => 'username',
'password' => 'password',
'database' => 'database_name',
'dbdriver' => 'driver_name');
C.routes.php: Its also very useful file in Codeigniter framework. Basically it is used for URL routing
this way:
$route['default_controller'] = 'controller_name';
By this firstly given controller's content will be shown through base URL.
***************************************************************************
Before going to further case study of CI directory structure we will discuss how a URL is organised in Codeigniter framework.
http://example.com/index.php/controller_name/controller_methods/
here controller_name is the name of controller defined in controller directory and controller_name is the name of method defined in controller_name controller.
****************************************************************************
Now we only will discuss about Model , View and controller Directories and will make a "Hello World!" app using codeigniter framework to understand how we work in this framework of PHP.
controller: In this directory controllers files are defined .
First create a file having hello.php as that's name and write these codes in that file
<?php
class hello extends CI_Controller{
function index()
{
echo "hello world";
}
}
?>
and type url in your browser as http://yourdomain.com/hello
and "hello world " will be rendered on that page
Explanation : class name of the controller should be same as its file name and this controller class should extend to CI_Controller class . so that CI library functions could be used in this controller class.
VIEWS: In this directory basically files containing apps front end structure are placed which are used for making the view.
Creating a view and calling it in a controller class:
For view: in views folder create a file having name hello_view.php
and write these codes into that file
<html>
<title>my first view file</title>
<body>
<h1>Hello World</h1>
</body>
</html>
Now in controller folder's hello.php file write these codes
<?php
class hello extends CI_Controller{
function index()
{
$this->load->view("hello");
}
}
?>
now using your browser type http://youdomain.com/hello
and this "hello world " will be rendered as h1 heading on that page.
Explanation : for calling a view created in views folder we have to write $this->load->view("view_name"); where view_name is the name of view that you want to include.
Next Tutorial we will discuss about how to make a model for a CI app.
for now you can fork me on Github : My github profile
Comments
Post a Comment