Home > Article > Backend Development > How to use the CodeIgniter framework in PHP
CodeIgniter is an open source lightweight PHP framework that is widely used in the development of web applications. CodeIgniter supports the MVC (Model-View-Controller) design pattern and has a series of functions such as efficient URL routing, secure form processing, database abstraction layer, libraries and auxiliary functions.
This article will introduce how to use the CodeIgniter framework in PHP.
1. Install and configure the CodeIgniter framework
First, download the latest version of the CodeIgniter framework source code from the official website https://codeigniter.com/. Unzip the source code to the directory where the project is located, and rename the folder to "ci" (this article uses "ci" as an example).
In order for the CodeIgniter framework to work properly, we need to create the "index.php" file in the project root directory and add the following code to the file:
<?php define('BASEPATH',__DIR__.'/ci/system/'); define('APPPATH',__DIR__.'/ci/application/'); require_once BASEPATH.'core/CodeIgniter.php';
Code explanation:
__DIR__
is a magic constant in PHP that points to the absolute path to the directory where the current script file is located. BASEPATH
Constant defines the path where the CodeIgniter framework core file is located. APPPATH
Constant defines the path where the CodeIgniter application files are located. require_once
function to load the CodeIgniter core file. 2. Create controllers and views
In the CodeIgniter framework, controllers are important components that operate business logic and respond to requests. Create a new controller file, "test.php" in the "application/controllers" directory under the project root directory (this article uses "test" as an example).
Define a method named "index" in the controller as follows:
<?php class Test extends CI_Controller { public function index() { $data['title'] = "欢迎使用CodeIgniter"; $data['content'] = "这是一篇CodeIgniter框架的教程"; $this->load->view('welcome_message', $data); } }
Code explanation:
class Test extends CI_Controller
indicates that the Test class inherits from the Controller core class defined in CodeIgniter. public function index()
Defines an index method of the Test controller, which will be automatically executed when the controller is called. $data
The array contains the data to be passed to the view, that is, the title and content. $this->load->view('welcome_message', $data);
Method passes data to the view. Next, we need to create a view file named "welcome_message.php" in the "application/views" directory. In this file, we can display the title and content through the data in the $data
array.
As shown below:
<!DOCTYPE html> <html> <head> <title><?php echo $title; ?></title> </head> <body> <h1><?php echo $title; ?></h1> <p><?php echo $content; ?></p> </body> </html>
Code explanation:
b5db4ca876fd66710baf594d1eefac34
and 3562c61acdf0f314f8e4b5527838c70b
are used to output the title and content respectively. 3. Start the CodeIgniter framework
Now that we have prepared the controller and view, we can start the CodeIgniter framework and view the results. Enter http://localhost/index.php/test in the browser and you will see that the page outputs the title and content we defined in the view.
If you do not want to enter "index.php" in the URL, you can modify the following configuration items in the "config.php" file in the "application/config" directory:
$config['index_page'] = '';
Change "index_page "Set the value to an empty string to hide "index.php".
Summary
This article briefly introduces how to use the CodeIgniter framework in PHP. Developers can quickly build Web applications through the CodeIgniter framework, enabling code reuse and rapid iteration of Web applications. Through the CodeIgniter framework, a series of functions such as MVC design pattern, secure form processing, and database abstraction layer can be implemented, which allows developers to focus on the implementation of business logic without worrying about the processing of underlying details.
The above is the detailed content of How to use the CodeIgniter framework in PHP. For more information, please follow other related articles on the PHP Chinese website!