自己动手写个小框架之六
系统开发中常常会使用页面传值,我们可以进行适当的封装,以方便使用。在kernel/request.php中
1 <?php 2 3 class Request { 4 5 private $key; 6 7 public function __construct() { 8 session_start(); 9 } 10 11 public function set($key, $value) { 12 $this->key = $key; 13 $_SESSION[$key] = $value; 14 } 15 16 public function get($key) { 17 $value = "null"; 18 if (isset($_SESSION[$key])) { 19 $value = $_SESSION[$key]; 20 } elseif (isset($_POST[$key])) { 21 $value = $_POST[$key]; 22 } elseif (isset($_GET[$key])) { 23 $value = $_GET[$key]; 24 } 25 26 return $value; 27 } 28 29 } 30 31 ?>
在控制类中,我们在indexAction中赋值,在requestAction中获取该值。其中,indexAction对default.tpl进行渲染而requestAction对default1.tpl进行渲染。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <?php 2 3 class defaultController extends Controller { 4 5 public function indexAction($parameter) { 6 $request = new request(); 7 //给session赋值 8 $request->set("luf&d", "luf&d"); 9 $this->render('default.tpl', array("value" => $parameter[0], 10 "Name" => "index Action page", 11 "FirstName" => array("John", "Mary", "James", "Henry"), 12 "LastName" => array("Doe", "Smith", "Johnson", "Case"), 13 "contacts" => array(array("phone" => "1", "fax" => "2", "cell" => "3"), 14 array("phone" => "555-4444", "fax" => "555-3333", "cell" => "760-1234")))); 15 } 16 17 public function newAction($parameter) { 18 $this->render('default1.tpl', array("value" => $parameter[0], 19 "Name" => "new Action page")); 20 } 21 22 public function argsAction($parameter) { 23 $this->render('default1.tpl', array("value" => $parameter[0] . $parameter[1], 24 "Name" => "new Action page")); 25 } 26 27 public function requestAction($parameter = NULL) { 28 $request = new request(); 29 //获取"luf&d"的值 30 $getinfo = $request->get("luf&d"); 31 $this->render('default1.tpl', array("value" => $getinfo, 32 "Name" => "request Action page")); 33 } 34 35 } 36 37 ?>
default1.tpl模板页为:
1 <{config_load file="test.conf" section="setup"}> 2 <{include file="header.tpl" title=foo}> 3 <PRE> 4 ^_^<{$value}> 5 6 <{* bold and title are read from the config file *}> 7 <{if #bold#}><b><{/if}> 8 <{* capitalize the first letters of each word of the title *}> 9 Title: <{#title#|capitalize}> 10 <{if #bold#}></b><{/if}> 11 12 The value of <{ldelim}>$Name<{rdelim}> is <b><{$Name}></b> 13 14 <b><{$Name|upper}></b> 15 16 <{include file="footer.tpl"}>
运行结果:
在浏览器输入地址:http://localhost/dluf/index.php/default/request
在系列七中将介绍.htaccess设置,配合路由类限制访问入口。