1.创建controller的类文件,我这里文件名为matchcontroller.class.php(推荐学习:php编程从入门到精通)
<?php/** * 比赛操作相关控制器功能类 */class matchcontroller{ /** * 比赛列表操作 */ public function listaction(){ header('content-type: text/html;charset=utf-8'); //实例化相应的模型类对象,调用某个方法,实现固定功能 // require './matchmodel.class.php'; // $m_match = new matchmodel(); //通过工厂获得对象 require './factory.class.php'; $m_match = factory::m('matchmodel'); $match_list = $m_match->getlist(); // $m_match2 = factory::m('matchmodel'); // 载入负责显示的html文件 require './template/match_list_v.html'; } /** * 比赛删除 */ public function removeaction(){ }
2.在入口文件中实例化控制器对象(前端控制器或请求分发器),文件名index.php
为了能让index.php去执行我们要操作的动作,应该传给index.php一些参数,来告诉入口文件怎么做。
假如我们要在比赛列表(比赛controller)中删除一条比赛信息,可以这样传参给index.php:
index.php?c=match&a=remove&id=n
相应的html文件应该这样写:
index.php:
<?php//动作$default_action = 'list';$a = isset($_get['a'])?$_get['a']:$default_action;//实例化控制器类require './matchcontroller.class.php';//实例化$controller = new matchcontroller();//调用方法$action_name = $a.'action';$controller -> $action_name();//可变方法
以上就是php控制器的方法在哪的详细内容。
