Home > Article > Backend Development > How to use CI7 framework in php?
PHP is a widely used web programming language with a rich ecosystem and community. Among many PHP frameworks, CodeIgniter is a lightweight framework that is popular among developers. This article will introduce how to use the CodeIgniter 3.1.11 (CI7 for short) framework for development.
I. System environment
II. Install CI7 Framework
Download the compressed file of the CI7 framework to your local computer. After decompression, you will see the following directory structure:
application system user_guide composer.json composer.lock index.php license.txt README.md
Among them :
The CI7 framework relies on some PHP extensions and libraries and needs to be installed using Composer. If Composer is not installed on your system, you can download and install it from the official website.
In the root directory of CI7, execute the following command to install the dependencies:
composer install
During the execution process, you may be prompted that the script will overwrite some existing files and you need to confirm.
The CI7 framework configuration file is located at application/config/config.php
and requires the following configuration:
$config['base_url'] = 'http://localhost/CI7/'; $config['index_page'] = ''; $config['encryption_key'] = 'fK8rHMq7sj8r8uCKzBQ7'; $config['uri_protocol'] = 'AUTO'; $config['enable_query_strings'] = FALSE; $config['allow_get_array'] = TRUE; $config['enable_query_strings'] = FALSE; $config['controller_trigger'] = 'c'; $config['function_trigger'] = 'm'; $config['directory_trigger'] = 'd'; $config['log_threshold'] = 1;
Among them, base_url
is the root URL for project access, encryption_key
is the security key, used to encrypt and decrypt sensitive information such as Cookies and Sessions. uri_protocol
is the way to obtain the URI string, with optional values such as AUTO, PATH_INFO, QUERY_STRING, REQUEST_URI, etc.
Visit http://localhost/CI7
in the browser, if you can see the welcome interface of the CI7 framework , the installation is successful.
III. CI7 development
The controller is the object in the CI7 framework that corresponds to URL routing and handles requests, located in application/controllers
directory. A basic controller class is defined as follows:
class My_controller extends CI_Controller { public function index() { // 显示欢迎界面 } public function hello() { // 显示"Hello, world!"界面 } }
Among them, by inheriting the CI_Controller class, you can obtain the built-in methods provided by the CI7 framework. The name of the controller is generally the same as the file name. When accessed, the controller name will be matched with the URI string to determine the execution method.
View is an HTML page template used to display content, located in the application/views
directory. The CI7 framework provides view objects (i.e. $this->load->view() method) to load view templates. Variables and data passed in the controller, HTML tags, etc. can be used in the view template.
Calling the view template in the controller:
class My_controller extends CI_Controller { public function index() { $data['title'] = "欢迎来到我的网站"; $this->load->view('welcome_message', $data); } }
Dynamicly parsing data in the view template:
<html> <head> <title><?= $title ?></title> </head> <body> <h1><?= $title ?></h1> <p>欢迎访问我的网站!</p> </body> </html>
Among them, the PHP short tag used e001818591141260ad444db4de1b1edb
can output variable values.
The model is an object used to process database-related operations in the CI7 framework and is located in the application/models
directory. Developers can interact with the database through model objects, such as adding, deleting, modifying, and checking the database, as well as searching and filtering data.
Use the Active Record class built into the CI7 framework to interact with the Mysql database. The sample code is as follows:
class My_model extends CI_Model { public function get_user($id) { $query = $this->db->get_where('user', array('id' => $id)); return $query->row_array(); } }
Call the model object in the controller:
class My_controller extends CI_Controller { public function index() { $this->load->model('my_model'); $user = $this->my_model->get_user(1); $data['user'] = $user; $this->load->view('user_profile', $data); } }
In the view template Dynamically parse data:
<html> <head> <title>User Profile</title> </head> <body> <h1><?= $user['name'] ?></h1> <p><?= $user['email'] ?></p> </body> </html>
Among them, use the $this->db->get_where()
method to perform query operations, and convert the query results into an array after a series of operations and return them. .
IV. Conclusion
CodeIgniter 3.1.11 is an excellent PHP framework. Using it for web application development can improve development efficiency and reduce code coupling. This article introduces the installation, configuration and simple use of the CI7 framework, hoping to help developers better master this framework.
The above is the detailed content of How to use CI7 framework in php?. For more information, please follow other related articles on the PHP Chinese website!