- Programming with CodeIgniterMVC
- Eli Orr Yehuda Zadik
- 1807字
- 2021-08-06 16:48:05
CI directory tree
The following is the classic directory tree structure of CodeIgniter:

Note that when we add new plugins and other resources such as bootstrap
, new directories of resources may be added with a name of your choice so that you can refer to them from the specific resource you are developing using the CI BASEPATH
defined parameter as the directory path to the CI_PROJECT_ROOT
directory.
If we add a new directory under the project root, let's say bootstrap
, the path for including resources such as CSS, JavaScript, or images (for example, hello.png
) will be $path = BASEPATH."bootstrap/hello.png"
.
config.php
The CI main configuration files have the following major configurations:
$config['base_url'] = '';
The default is an empty string so that CI can calculate the base URL of our project root directory. We shall refer to the base URL in many places in our code, mostly to execute controllers. To get the base URL, we should call:
$base_url = base_url(); // defined in the URL // helper mentioned before.
The base_url()
function in the URL helper function returns the URI string to the CI project base. For example, if the CI project is developed on a domain named example.com
under a public_html
directory named mydev
, and we have a controller named find
, a method named stock
, and a directory named myprod
, we can call the find
or stock
method in both the myprod
and mydev
projects using base_url()
:
$url = base_url()."index.php/ find/stock";
In the mydev
project, we will get:
$url = "http://example.com/mydev/index.php/find/stock"
In the myprod
project, we will get:
$url = "http://example.com/myprod/index.php/find/stock"
Hence, in order to call a controller class named my_class
, we use:
$URL = base_url()."index.php/my_class/mymethod";
This will define $u
rl
as http://example.com/
mydev/index.php/my_class
.
To set the index page as a part of the URI path to CI controllers/methods, we use:
$config['index_page'] = 'index.php';
The index.php
file is the CI root PHP service that handles all the URI requests. It is used as part of a path URI to a resource, such as http://mysite.com/fci/index.php/tables_managemen
t/show
. However, we can hide the index.php
file by setting CI to hide the index.php file in the URI path for calling the CI resources such as http://mysite.com/fci/tables_managemen t/show
. To do so, we need to perform the following configuration steps:
- In the project root directory where the CI
index.php
file resides, an HTACCESS type file named.htaccess
is added with the following configuration lines, which reroutes a noneindex.php
URI referring to the CI project controllers path withoutindex.php
:RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L] <Files "index.php"> AcceptPathInfo On </Files>
For more on this, refer to http://en.wikipedia.org/wiki/Htaccess.
- We should make the change to the
/config/config.php
file so thatindex_page
will be empty in the URI path string instead of the defaultindex.php
. $config['index_page'] = '';
The .htaccess
file does the trick here by adding the index.php
file to the URI after receiving the URI request from the browser and before executing it. The result is that the user who is browsing will not see it, but it will call the desired resource properly, in a similar way to how we used index.php
:
The language setting is done as follows:
$config['language'] = 'english';
It is recommended that you leave this as default. Note that even if we use other languages, such as Arabic or Hebrew, it will be fine. We just make sure that our PHP files are saved as UTF-8 without BOM (byte order mark is a unicode character that marks the file-encoding method supporting multilanguage schemes; for more information, refer to http://en.wikipedia.org/wiki/Byte_order_mark to inform the browser that receives the rendered HTML page to process it as a UTF-8 file.
The exact meaning of this tag is out of the scope of this book and can be learned from HTML standard.
$config['charset'] = 'UTF-8';
Additionally, it is highly recommended for multi-language support to add the following line in our view file's HTML header:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
These settings inform the browser to process the rendered HTML page whose characters are encoded as UTF-8, which is the most common multilanguage standard for non-English languages such as Hebrew, Arabic, and Chinese.
Do not touch these settings; it is very useful to support multiple languages.
$config['enable_hooks'] = FALSE;
The preceding configuration, if set to TRUE
, will enable us to define hooks into CI events, where the hooks are defined in the application/hooks
directory. Do not touch these settings unless you have a specific plan for CI event hooks. Note that the concept of adding hooks to the CI core activity is out of this book's scope.
$config['subclass_prefix'] = 'MY_';
The preceding configuration will enable us to define naming roles to our library class name's prefix, in order to distinguish with other default libraries.
$config['permitted_uri_chars'] = 'a-z 0-9~%.:_\-';
The preceding code defines the allowed chars within a URI calling CI resources, mainly controllers. It is recommended to not touch this setting.
$config['allow_get_array'] = TRUE;
This will enable us to call the controller class methods with parameters, such as in the example provided earlier.
<?php echo base_url(); ?>index.php/my_handler/calc/5/7
The preceding code will provide the same results as the class method within the my_handler
class itself in the following format:
$Val= $this->calc(5,7);
The following configuration defines if a GET URL query string will be used:
$config['enable_query_strings'] = FALSE;
This configuration, if set to TRUE
, will enable us to call controller class methods with parameters in the GET URL query form:
<?php echo base_url();?>index.php/my_handler/calc.php?a=5&b=7
It is highly recommended to leave this as FALSE
, as CI provides a solution to pass parameters within URI, as shown in the calc
example at the beginning of this chapter.
The log threshold for the severity level is such that any event that is of the same or higher severity level will be logged to CI. The supported threshold levels and their meanings are as follows:
- 0: Disables logging (error logging turned off)
- 1: Error messages (including PHP errors)
- 2: Debug messages
- 3: Informational messages
- 4: All messages
$config['log_threshold'] = 4; // 4 is the highest level for all CI events from notice level // events and worse
The preceding configuration will generate error logs according to the log_threshold
()
level at /application/logs
if the error log was enabled.
Note that enabling the errors log will cause performance reduction in our web application. Use it only if you must for debugging needs. Otherwise set it to 0
.
$config['log_path'] = '';
The default log file path in the CI project is application/logs
. Do not touch this configuration unless you have a clear reason.
The date time format:
$config['log_date_format'] = 'Y-m-d H:i:s';
The default date time format setting is 2012-06-18 14:54:11
. It is recommended to not touch this configuration.
The cache file path:
$config['cache_path'] = '';
The default is application/cache
. It is recommended not to touch this configuration. The session key:
$config['encryption_key'] = '';
This encryption_key
must be set with a key in order to use the session class services. For example:
$config['encryption_key'] = 'cMGy4DSwGUvxYSar4279626HgOn2342efrwerr2TE2RF4G3reg4tF3etf';
An example of the session library usage within a controller and setting a session variable is as follows:
$uid = 119; // where uid is the id of the loggeing user $this->session->set_userdata ('this_user_id', $uid );
Getting the session variable in another controller is as follows:
$uid = $this->session->userdata('this_user_id');
The session data storage mechanism is as follows:
$config['sess_use_database'] = FALSE;
If the recommended configuration is set to TRUE
, we would use many session parameters of a large size stored in the associated default database.
Session expiration timeout in seconds:
$config['sess_expiration'] = 7200; // The number of seconds the session will be kept
Additional session configuration parameters can be found in the CI user manual. Cross-site scripting (XSS) filtering activation/deactivation:
$config['global_xss_filtering'] = FALSE;
This will enable XSS filtering on URI requests sent to the application. Note that all URI requests are processed initially by the root index.php
to analyze the URI request and issue the proper CI calls. If set to TRUE
it will protect URI requests from XSS type malicious attackers. It is recommended to set it to TRUE
even if we reduce a bit of our application performance.
$config['csrf_protection'] = FALSE;
If set to TRUE
the CI will prevent Cross-Site Request Forger y (CSRF/XSRF) attacks. The risk is when the fraud form is submitted. If we are accepting user data, it is strongly recommended that CSRF protection should be enabled. Note that when using AJAX, additional code may be required to enable CSRF protection with AJAX.
database.php
The database configuration enables to define one or more database connections that can be used by the application. The database configuration is a two-dimensional array in the following form:
$db['db_entry']['db_connection_param']
By setting the parameters for database default entry, we shall define the following parameters:
$db['default']['hostname'] = 'localhost'; // note: in some cases '127.0.0.1' must be used instead // localhost if the database server is in another server use // URI such as: 'domain.db.NNNNNNN.hostedresource.com' or // similar – advise our system admin/service provider // Optional configuration of DB server connection port $db['default']['port'] = '4009'; // In case our DB server operates on another port // otherwise we may drop the port config line! $db['default']['username'] = 'mydefaultdb'; $db['default']['password'] = 'mypass1'; $db['default']['database'] = 'mydatabase1'; $db['default']['dbdriver'] = "mysql"; $db['default']['dbprefix'] = ""; $db['default']['pconnect'] = TRUE; $db['default']['db_debug'] = TRUE; $db['default']['cache_on'] = TRUE; $db['default']['cachedir'] = ""; $db['default']['char_set'] = "utf8"; $db['default']['dbcollat'] = "utf8_general_ci";
By setting the parameters for another database entry named dbentry2
, we shall define the following parameters:
$db['dbentry2']['hostname'] = 'localhost'; $db['dbentry2']['username'] = 'mySecondDB'; $db['dbentry2']['password'] = 'mypass2'; $db['dbentry2']['database'] = 'mySecondDB'; $db['dbentry2']['dbdriver'] = "mysql"; $db['dbentry2']['dbprefix'] = ""; $db['dbentry2']['pconnect'] = TRUE; $db['dbentry2']['db_debug'] = TRUE; $db['dbentry2']['cache_on'] = TRUE; $db['dbentry2']['cachedir'] = ""; $db['dbentry2']['char_set'] = "utf8"; $db['dbentry2']['dbcollat'] = "utf8_general_ci";
There is no need to connect and load the default database as it is done automatically when loading the database class—however, the call is:
$this->load->database();
Or, for referring to a specific database entry name, it is:
$this->load->database('default');
In order to connect and load the dbentry2
database settings stated earlier, use the following code:
$this->dbentry2= $this->load->database(dbentry2', TRUE);
To use the default database with the database class library, db,
use:
$q1 = $this->db->query ("select * from mytable");
To use the dbentry2
database, use:
$q2 = $this->dbentry2->db->query ("select * from DB2table");
routes.php
Define the default controller that will be executed when referred via the URI to the base_url
of the project—let's say http://mydomain.com/myapp
so that myapp
is a subdirectory of public_html
in the sever and we have home_page_controller
.
$route['default_controller'] = "home_page_controller"
When the user issues http://mydomain.com/myapp
, due to the route configuration for the home controller, the URI that CI will issue will be as if the user is referring to http://mydomain.com/myapp/home_page_controller
.
$route['404_override'] = '';
In the preceding example, the default application/errors/error_404.php
page will be executed, in case the user refers to a non-existing project controller, such as http://mydomain.com/myapp/sadfasdfsdfsdi
.
We may decide, for example, to pop-up a message for the non-existing page and route to the default URI to minimize user inconvenience.
- Web全棧工程師的自我修養
- Spring實戰(第5版)
- Python高效開發實戰:Django、Tornado、Flask、Twisted(第3版)
- PHP+MySQL網站開發項目式教程
- The DevOps 2.5 Toolkit
- 大數據分析與應用實戰:統計機器學習之數據導向編程
- 自學Python:編程基礎、科學計算及數據分析(第2版)
- Python編程快速上手2
- 金融商業數據分析:基于Python和SAS
- Microsoft XNA 4.0 Game Development Cookbook
- Unity與C++網絡游戲開發實戰:基于VR、AI與分布式架構
- SFML Essentials
- Mastering MeteorJS Application Development
- Visual FoxPro程序設計
- OpenACC并行編程實戰