Home > Backend Development > PHP Problem > Where are the methods of the php controller?

Where are the methods of the php controller?

(*-*)浩
Release: 2023-02-24 17:16:01
Original
2909 people have browsed it

The role of the controller is usually to obtain the data in the model and give it to the view to display. So how should we write it during development?

Where are the methods of the php controller?

1. Create the class file of Controller. My file name is MatchController.class.php (recommended learning: Getting Started with PHP Programming To master)

<?php/**
 * 比赛操作相关控制器功能类 
 */class MatchController{
    /**
     * 比赛列表操作
     */
    public function listAction(){
        header(&#39;Content-Type: text/html;charset=utf-8&#39;);
        //实例化相应的模型类对象,调用某个方法,实现固定功能
        // require &#39;./MatchModel.class.php&#39;;
        // $m_match = new MatchModel();
        //通过工厂获得对象
        require &#39;./Factory.class.php&#39;;
        $m_match = Factory::M(&#39;MatchModel&#39;);
        $match_list = $m_match->getList();
        // $m_match2 = Factory::M(&#39;MatchModel&#39;);

        // 载入负责显示的html文件
        require &#39;./template/match_list_v.html&#39;;
    }
    /**
     * 比赛删除
     */
    public function removeAction(){
        
    }
Copy after login

2. Instantiate the controller object (front-end controller or request dispatcher) in the entry file, the file name is index.php

In order to allow index.php to perform the actions we want to operate, some parameters should be passed to index.php to tell the entry file how to do it.

If we want to delete a game information in the game list (Game Controller), we can pass the parameters to index.php like this:

index.php?c=match&a=remove&id=N
Copy after login

The corresponding HTML file should be written like this:

Where are the methods of the php controller?

index.php:

<?php
//动作
$default_action = &#39;list&#39;;
$a = isset($_GET[&#39;a&#39;])?$_GET[&#39;a&#39;]:$default_action;

//实例化控制器类
require &#39;./MatchController.class.php&#39;;
//实例化
$controller = new MatchController();
//调用方法
$action_name = $a.&#39;Action&#39;;
$controller -> $action_name();//可变方法
Copy after login

The above is the detailed content of Where are the methods of the php controller?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template