WEB开发笔记 www.chhua.com 每日练习 PHP控制器基本请求对象

最近很多事情要忙,所以,很久没有写每日练习了,今天就写一下PHP控制器中的基本Rrquest类。
PHP的请求主要是超全局数组$_POST和$_GET,比较易用,就是不太灵活,下面就写一个这样的类,大家可以按照自己的思路进行调整。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
 
<span style="color: #000000;font-weight: bold"><?php</span>
<span style="color: #000000;font-weight: bold">class</span> Request <span style="color: #009900">{</span>
	<span style="color: #000000;font-weight: bold">private</span> <span style="color: #000088">$request</span><span style="color: #339933">=</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/array" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">array</span></a><span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//提交上来的数据存放数组</span>
 
	<span style="color: #000000;font-weight: bold">public</span> <span style="color: #000000;font-weight: bold">function</span> __construct<span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #009900">{</span>
		<span style="color: #000088">$this</span><span style="color: #339933">-></span><span style="color: #004000">request</span><span style="color: #339933">=</span><span style="color: #000088">$this</span><span style="color: #339933">-></span><span style="color: #004000">initFromHttp</span><span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//实例化的时候,返回表单</span>
	<span style="color: #009900">}</span>
 
	<span style="color: #000000;font-weight: bold">private</span> <span style="color: #000000;font-weight: bold">function</span> initFromHttp<span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #009900">{</span><span style="color: #666666;font-style: italic">//接受表单数据</span>
		<span style="color: #b1b100">if</span> <span style="color: #009900">(</span><span style="color: #339933">!</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/empty" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">empty</span></a><span style="color: #009900">(</span><span style="color: #000088">$_POST</span><span style="color: #009900">)</span><span style="color: #009900">)</span> <span style="color: #b1b100">return</span> <span style="color: #000088">$_POST</span><span style="color: #339933">;</span>
		<span style="color: #b1b100">if</span> <span style="color: #009900">(</span><span style="color: #339933">!</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/empty" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">empty</span></a><span style="color: #009900">(</span><span style="color: #000088">$_GET</span><span style="color: #009900">)</span><span style="color: #009900">)</span> <span style="color: #b1b100">return</span> <span style="color: #000088">$_GET</span><span style="color: #339933">;</span>
		<span style="color: #b1b100">return</span> <a rel="noopener noreferrer nofollow" href="http://www.php.net/array" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">array</span></a><span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
	<span style="color: #009900">}</span>
 
	<span style="color: #000000;font-weight: bold">public</span> <span style="color: #000000;font-weight: bold">function</span> get<span style="color: #009900">(</span><span style="color: #000088">$name</span><span style="color: #009900">)</span><span style="color: #009900">{</span><span style="color: #666666;font-style: italic">//返回表单键名的值</span>
		<span style="color: #b1b100">if</span> <span style="color: #009900">(</span><span style="color: #339933">!</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/array_key_exists" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">array_key_exists</span></a><span style="color: #009900">(</span><span style="color: #000088">$name</span><span style="color: #339933">,</span><span style="color: #000088">$this</span><span style="color: #339933">-></span><span style="color: #004000">request</span><span style="color: #009900">)</span><span style="color: #009900">)</span> <span style="color: #b1b100">return</span> <span style="color: #0000ff">''</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//如果不存在的键名返回空值</span>
		<span style="color: #b1b100">return</span> <span style="color: #000088">$this</span><span style="color: #339933">-></span><span style="color: #004000">request</span><span style="color: #009900">[</span><span style="color: #000088">$name</span><span style="color: #009900">]</span><span style="color: #339933">;</span>
	<span style="color: #009900">}</span>
 
	<span style="color: #000000;font-weight: bold">public</span> <span style="color: #000000;font-weight: bold">function</span> set<span style="color: #009900">(</span><span style="color: #000088">$name</span><span style="color: #339933">,</span><span style="color: #000088">$value</span><span style="color: #009900">)</span><span style="color: #009900">{</span><span style="color: #666666;font-style: italic">//设置Request数组键值</span>
		<span style="color: #000088">$this</span><span style="color: #339933">-></span><span style="color: #004000">request</span><span style="color: #009900">[</span><span style="color: #000088">$name</span><span style="color: #009900">]</span><span style="color: #339933">=</span><span style="color: #000088">$value</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//</span>
	<span style="color: #009900">}</span>
<span style="color: #009900">}</span>
<span style="color: #666666;font-style: italic">//应用</span>
<span style="color: #000088">$reuqest</span><span style="color: #339933">=</span><span style="color: #000000;font-weight: bold">new</span> Request<span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
<span style="color: #b1b100">echo</span> <span style="color: #000088">$reuqest</span><span style="color: #339933">-></span><span style="color: #004000">get</span><span style="color: #009900">(</span><span style="color: #0000ff">"username"</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
<span style="color: #000000;font-weight: bold">?></span>
<span style="color: #339933"><</span>form<span style="color: #339933">></span>
<span style="color: #339933"><</span>input type<span style="color: #339933">=</span><span style="color: #0000ff">"text"</span> name<span style="color: #339933">=</span><span style="color: #0000ff">"username"</span> value<span style="color: #339933">=</span><span style="color: #0000ff">"WEB开发笔记"</span><span style="color: #339933">></span>
<span style="color: #339933"><</span>input type<span style="color: #339933">=</span><span style="color: #0000ff">"text"</span> name<span style="color: #339933">=</span><span style="color: #0000ff">"url"</span> value<span style="color: #339933">=</span><span style="color: #0000ff">"www.chhua.com"</span><span style="color: #339933">></span>
<span style="color: #339933"><</span>input type<span style="color: #339933">=</span><span style="color: #0000ff">"submit"</span><span style="color: #339933">></span>
<span style="color: #339933"></</span>form<span style="color: #339933">></span>

以上就是PHP控制器,基本表单提交数据的应用,希望对你的帮助。