What does page controller mean in php?

零下一度
Release: 2023-03-10 16:34:01
Original
2827 people have browsed it
<?php/*页面控制器模式:按我的理解就是将业务逻辑和视图分离开来(通常最简便的写法是php和html代码是混合在一个文件里面的),
即一个业务逻辑文件对应一个视图文件。
    代码示例即注解如下:*///页面控制器模式namespace woo\controller;//业务逻辑文件 (文件名AddVenue.php)abstract class PageController {   //基类private $request;function __construct (){    $request = \woo\base\RequestRegistry::getRequest();        //通过注册表类获取一个处理用户请求信息的类if(is_null($request)){$request = new Request();
        }$this->request = $request;
    }    abstract function process();    function forward($resource){        //跳转include($resource);exit(0);
    }    function getRequest(){return $this->request;
    }
}class AddVenueController extends PageController { //这个类的作用就是向数据库写入一个venue数据(数据表结构类似:id,name)        function process(){try{$request = $this->getRequest();$name = $request->getProperty('venue_name');                //获取用户提交的venue的名称if(is_null($request->getProperty('submitted'))){            //判断是否表单提交,否的话跳转到add_nenue.php$request->addFeedback("choose a name for the venue");    $this->forward('add_nenue.php');                        
            } else if (is_null($name)){                                    //判断表单提交是否有name $request->addFeedback("name is a required field");    $this->forward('add_venue.php');                        //跳转add_venue.php            }        $venue = new \woo\domain\Venue(null,$name);                    //创建对象便可将它添加到数据库,具体内部的业务逻辑不必深究。$this->forward("ListVenues.php");                            //添加成功后跳转ListVenues.php,即一个列表数据的显示界面    } catch(Exception $e){$this->forward('error.php');                                //跳转到一个错误界面                    }
    }
}$controller = new AddVenueController();                                    //执行这个类的process()方法$controller->process();?>

<?php 
//视图文件 (文件名add_venue.php)require_once("woo/base/RequestRegistry.php");$request = \woo\base\RequestRegistry::getRequest();?>
<html>
<head>
<title>Add Venue</title>
</head>

<body>
    <h1>Add Venue</h1>
    <table>
    <tr>
        <td>
        <?php print $request->getFeedbackString('</td></tr><tr><td>')?>
        </td>
    </tr>
    </table>
    <form action="AddVenue.php" method="get">
        <input type="hidden" name="submitted" value="yes" />
        <input type="text" name="venue_name" />
    </form>
</body>
</html>
Copy after login

 

The above is the detailed content of What does page controller mean in php?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!