Home  >  Article  >  Backend Development  >  fatfree-f3小型php框架(2)

fatfree-f3小型php框架(2)

WBOY
WBOYOriginal
2016-06-13 11:53:121348browse

fatfree-f3小型php框架(二)

我们的第一个例子不是太难懂吧,但是如果要做更多的功能与要求,我们就要在$f3 -> run()之前加一些别的路由:

<span style="font-size:18px;">$f3->route('GET /about',    function() {        echo 'Donations go to a local charity... us!';    });</span>
如果你不想把全局的命名弄得太乱,fatfree有不同的映射路由的方法去管理面向对象的类与方法:

<span style="font-size:18px;">class WebPage {    function display() {        echo 'I cannot object to an object';    }}$f3->route('GET /about','WebPage->display');</span>


http的要求同样也能路由到静态的类函数:

<span style="font-size:18px;">$f3->route('GET /login','Auth::login');</span>



路由和符号:

作为fatfree这样一门强大的特殊领域语言,可以指定单一的路由区控制不同的情况:

<span style="font-size:18px;">$f3->route('GET [email protected]',    function($f3) {        echo $f3->get('PARAMS.count').' bottles of beer on the wall.';    });</span>
[email protected],[email protected]rew/98([email protected]值为98)或者/brew/99这样子,然后这个98就会赋值[email protected],调用funtion去输出“98 bottles of beer on the wall”.

除了数字之外,[email protected],例如/brew/unbreakable然后就输出“unbreakable bottles of beer on the wall”.

还有另外一招:

<span style="font-size:18px;">$f3->route('GET [email protected]',    function($f3,$params) {        echo $params['count'].' cars.';    });</span>

还有一个方法,用“*”来代替任何不重要的信息,例如:
<span style="font-size:18px;">$f3->route('GET /car/*',    function() {        echo 'We have enough cars.';    });</span>
就是无论输入什么在/car/#$#%#@$#@[email protected]


如果要改变方法去别的网页,我们可以用这招:

<span style="font-size:18px;">$f3->route('GET|HEAD /go_google',    function($f3) {        $f3->reroute('http://www.google.com');    });</span>

也就是说输入 http://localhost/go_google就可以跳转页面了。(注意,这些都是在index.php中定义的,进入文件夹后会自动搜index中的路由,会自动识别到底是路由还是文件夹的,别担心)

reroute方法就是跳转页面用的,而且前面同事用到了GET和HEAD要求,除此之外还有一个POST,这个大家可以了解一下他们的区别。


给路由命名:

当你定义一个路由的时候,你可以给路由命名。可以用代码中路由的名字和模板来替代URL的类型。如果你想要这么做就要在定义路由的地方顺便定义了路由的名字,for example 

<code style="padding:0px; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; font-size:12px; color:inherit; background-color:transparent; border:0px; word-wrap:break-word"><span class="variable" style="color:rgb(58,135,173)">$f3</span><span class="object_operator" style="color:black">-></span><span class="string" style="color:black">route</span>(<span class="constant_encapsed_string" style="color:rgb(178,34,34)">'GET @beer_list: /beer'</span>,<span class="whitespace"> </span><span class="constant_encapsed_string" style="color:rgb(178,34,34)">'Beer->list'</span>);</code>
同时要注意,路由的名字必须用php的命名规则。这个例子就说明[email protected]??。

然后调用的时候就要用到命名专属数组ALISASES,看一个调用的例子

<code style="padding:0px; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; font-size:12px; color:inherit; background-color:transparent; border:0px; word-wrap:break-word"><span class="xml_tag" style="color:rgb(30,144,255)"><a> <span class="xml_attr" style="color:rgb(138,43,226)">href</span>=<span class="xml_data" style="color:red">"[email protected]_list}}"</span><span class="xml_tag" style="color:rgb(30,144,255)">></span>view beer list<span class="xml_tag" style="color:rgb(30,144,255)"></span></a></span><span class="xml_tag" style="color:rgb(30,144,255)">></span></code>

( a href是超链接的意思,可以看看html的用法)

当然了这种用法还可以用来跳转页面,例如刚刚学的那个reroute函数:

<code style="padding:0px; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; font-size:12px; color:inherit; background-color:transparent; border:0px; word-wrap:break-word"><span class="variable" style="color:rgb(58,135,173)">$f3</span><span class="object_operator" style="color:black">-></span><span class="string" style="color:black">reroute</span>(<span class="constant_encapsed_string" style="color:rgb(178,34,34)">[email protected]_list'</span>);<span class="whitespace"> </span><span class="comment" style="color:rgb(184,134,11)">// note the single quotes</span></code>

这次就不是跳转到google上了,而是跳转到“/beer”的页面上。

同时,我们常用的重载函数也适合这种情况,例子:

<code style="padding:0px; font-family:Monaco,Menlo,Consolas,'Courier New',monospace; font-size:12px; color:inherit; background-color:transparent; border:0px; word-wrap:break-word"><span class="variable" style="color:rgb(58,135,173)">$f3</span><span class="object_operator" style="color:black">-></span><span class="string" style="color:black">route</span>(<span class="constant_encapsed_string" style="color:rgb(178,34,34)">'GET @beer_list: [email protected]'</span>,<span class="whitespace"> </span><span class="constant_encapsed_string" style="color:rgb(178,34,34)">'Beer->bycountry'</span>);<span class="whitespace"></span><span class="variable" style="color:rgb(58,135,173)">$f3</span><span class="object_operator" style="color:black">-></span><span class="string" style="color:black">route</span>(<span class="constant_encapsed_string" style="color:rgb(178,34,34)">'GET @beer_list: [email protected][email protected]'</span>,<span class="whitespace"> </span><span class="constant_encapsed_string" style="color:rgb(178,34,34)">'Beer->byvillage'</span>);<span class="whitespace"></span><span class="comment" style="color:rgb(184,134,11)">// a set of key-value pairs is passed as argument to named route</span><span class="variable" style="color:rgb(58,135,173)">$f3</span><span class="object_operator" style="color:black">-></span><span class="string" style="color:black">reroute</span>(<span class="constant_encapsed_string" style="color:rgb(178,34,34)">[email protected]_list(@country=Germany)'</span>);<span class="whitespace"></span><span class="comment" style="color:rgb(184,134,11)">// if more than one token needed</span><span class="variable" style="color:rgb(58,135,173)">$f3</span><span class="object_operator" style="color:black">-></span><span class="string" style="color:black">reroute</span>(<span class="constant_encapsed_string" style="color:rgb(178,34,34)">[email protected]_list(@country=Germany,@village=Rhine)'</span>);</code>

[email protected]_list这个变量重载了,当然调用的分别是bycountry和byvillage两个不同的函数,然后根据这两个方法的不同来分别给参数,[email protected][email protected],[email protected]?果给两个,自然就是调用下面那个。








1楼modiziri昨天 10:19
恩恩
Statement:
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