Home  >  Article  >  Backend Development  >  How to use CI7 framework in php?

How to use CI7 framework in php?

WBOY
WBOYOriginal
2023-05-31 21:40:34859browse

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

  • PHP 5.6 or higher version
  • Mysql 5.1 or higher version

II. Install CI7 Framework

  1. Download the 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 application directory is the main directory for developers to develop
  • The system directory is the core code of the CI7 framework
  • user_guide directory is the user manual of the CI7 framework
  • composer.json is the dependency management file of the CI7 framework
  • index.php is the entry file of the CI7 framework
  1. Installation dependencies

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.

  1. Configuring CI7

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.

  1. Test whether the installation is successful

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

  1. Controller

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.

  1. View

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.

  1. Model

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn