search
HomeCMS TutorialPHPCMSDoes phpcms have a controller?

Does phpcms have a controller?

Feb 17, 2023 am 10:37 AM
cmscms systemphpcms

phpcms has controllers. The phpcms controller is the class file of the module, which is stored under "phpcms/modules". Each module has an independent naming method. The class name is in the format of "filename.php naming"; the controller class inherits the system's function library by default. Can be used directly. The class name of the controller class and the controller file name must be the same.

Does phpcms have a controller?

The operating environment of this tutorial: windows7 system, phpcms v9 version, DELL G3 computer

phpcms has a controller.

What is a controller in phpcms

phpcms controller is the class file of the module, which is stored under phpcms/modules. Each module has an independent naming method. The class name is in the file name.php naming format. The controller class inherits the system's function library by default and can be used directly. The class name of the controller class and the controller file name must be the same.

Add a new controller

Next we will create a new test folder under phpcms/modules, and create a new one named mytest.php in the test folder file, add the following code in the file:

defined('IN_PHPCMS') or exit('No permission resources.');class mytest {    function __construct() {
    }    public function init() {
        $myvar = '这是默认加载!';        echo $myvar;
    }    public function mylist() {
        $myvar = '这是自定义list!';        echo $myvar;
    }
}

Then we can enter the following access method in the browser

http://domain name/index.php?m=test&c= mytest

The init() method is loaded by default

http://domain name/index.php?m=test&c=mytest&a=mylist

Loading is the mylist method

Template introduction

Front-end template

Website front-end template location Under the phpcms/templates/default/ module

We add the loading template method in the init() method of the controller mytest.php file

include template('test', 'mytest', 'default');

Create a new one under the phpcms/templates/default/ directory The test folder and mytest.html file are enough.

Backend template

The background template file is in the phpcms/modules/module name/templates directory

Controller with permissions

Backend controller

Create a new mytest_admin.php file under phpcms/modules/admin/. The file code is as follows:

defined('IN_PHPCMS') or exit('No permission resources.');
pc_base::load_app_class('admin','admin',0);
class mytest_admin extends admin{
    function __construct()
    {
    }
    public function index(){
        echo "后台控制器";
    }
 
}

Access address: http://domain name /index.php?m=admin&c=mytest_admin&a=index

phpcms custom model

Introduce the model on the class

pc_base::load_sys_class('model', '', 0);

Then the class extends inheritance model

Add the following code in the __construct method:

$this->db_config = pc_base::load_config('database');
$this->db_setting = 'default';
parent::__construct();

Query statement

function init(){
    $sql = 'select * from v9_news ';
    $data = $this->get_array_by_sql($sql);
    var_dump($data);
}
public function sql_query($sql) {
    if (!empty($this->db_tablepre)) $sql = str_replace('phpcms_', $this->db_tablepre, $sql);
    return parent::query($sql);
}
 
public function fetch_next() {
    return $this->db->fetch_next();
}
//通过SQL语句查询一条结果
public function get_one_by_sql($sql){
    $this->sql_query($sql);
    $res = $this->fetch_next();
    $this->free_result();
    return $res;
}
//通过sql语句查询数组
public function get_array_by_sql($sql){
    $this->sql_query($sql);
    $res = $this->fetch_array();
    $this->free_result();
    return $res;
}
 
//释放数据库结果资源,调用底层完成
public function free_result() {
    $this->db->free_result();
}

PHP Chinese website, a large number of free PHPCMS tutorials , welcome to online learning!

The above is the detailed content of Does phpcms have a controller?. 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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment