看了《PHP敏捷开发框架CodeIgniter》第7章,发现从设计角度来讲,CodeIgniter整体设计框架,用其设计者得话讲就是:
“我想要简化问题,所以,我决定创建一个大的控制器对象包含很多其它对象的实例:…当一个用户创建他们自己的控制器时,他们能够轻松地访问任何资源,不用担心作用域的问题。”
第8章看了个开头,为了增加体验,把开始的例子,也就是对PHP函数floor的测试代码贴在这里,在start.php中:
<?php
class Start extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->helper('url');
$this->load->helper('form');
$this->load->library('unit_test');
$this->unit->active(TRUE);
}
function index()
{
// $data['mytitle']="My site";
// $data['base']=$this->config->item('base_url');
// $data['css']=$this->config->item('css');
$data['mytitle']="A website to monitor other websites";
$data['text']="Please log in here!";
$this->load->view('entrypage',$data);
}
function assessme()
{
$this->load->library('session');
$name=$_POST['username'];
$password=$_POST['password'];
if($name=='fred' && $password=='12345')
{
$newdata=array('status'=>'ok');
$this->session->set_userdata($newdata);
$ip=$this->session->userdata('ip_address');
echo $ip;
$this->mainpage();
}
else
{
$this->index();
;}
}
function mainpage()
{
$test=floor(1.56);
$expected_result=1;
$test_name='test php floor function';
$this->unit->run($test,$expected_result,$test_name);
$test=floor(2.56);
$expected_result=1;
$test_name='tests php floor function';
$this->unit->run($test,$expected_result,$test_name);
echo $this->unit->report();
$this->load->view('mainpage');
}
}
?>
两个视图文件不变。另外,如果将$this->unit->report()变成$this->unit->result(), 看不到想要的结果!