fatfree-f3小型php框架课程(四)

WBOY
リリース: 2016-06-13 11:53:50
オリジナル
995 人が閲覧しました

fatfree-f3小型php框架教程(四)

这里还有一个重要的说明,

<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 /'</span>,<span class="constant_encapsed_string" style="color:rgb(178,34,34)">'Main->home'</span>);</code>
ログイン後にコピー
这是一个动态的函数调用,但是在调用home函数之前,f3会寻找一个叫做beforeRoute()的函数并且调用它,如果你定义了的话。然后调用home,然后走之前又会调用一个叫做afterRoute()的函数。而且如果你在同一个类中定义了好几个不同路由不同方法的好几个route的话,他们都是调用同一组before,afterroute的。


动态路由:

[email protected]@free是可以作为参数调用进入函数里面的,这里要说的是,路由同样也可以这样做。例如

<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 [email protected]'</span>,<span class="constant_encapsed_string" style="color:rgb(178,34,34)">'Products->@action'</span>);</code>
ログイン後にコピー

这样定义起来就很方便了,一条定义就解决了一堆定义。而且调用的时候只要[email protected],就可以直接动态调用该函数了。

接下来再看看静态的变量路由和框架常用定义

<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="comment" style="color:rgb(184,134,11)">// static method</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 [email protected]'</span>,<span class="constant_encapsed_string" style="color:rgb(178,34,34)">'Main::@genre'</span>);<span class="whitespace"></span><span class="comment" style="color:rgb(184,134,11)">// object mode</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 [email protected][email protected]'</span>,<span class="constant_encapsed_string" style="color:rgb(178,34,34)">'@controller->@action'</span>);</code>
ログイン後にコピー

但是,如果你定义了参数又没有给参数赋值,系统就会跳出HTTP 404 Not Found错误了。


路由就讲到这里了,下面讲变量部分:

在fatfree里面的变量定义是全局的,也就是说这个变量适用于整个MVC架构。这里讲的变量不是php中的变量啊,要区分开来,fatfree中定义变量是这样的:

<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">set</span>(<span class="constant_encapsed_string" style="color:rgb(178,34,34)">'var'</span>,<span class="string" style="color:black">value</span>)</code>
ログイン後にコピー
然后这个var就可以全局用了,看了上一章的人也会发现,有些特殊字符是不能作为变量名的,例如AUTOLOAD,这些是重载过的函数,有别的意思。而php中的变量是$var,并且不会重名,两者用$区分开来。

然后下面讲讲基本变量语法:

数组定义

<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">mset</span>(<span class="whitespace">    </span><span class="array" style="color:rgb(255,20,147)">array</span>(<span class="whitespace">        </span><span class="constant_encapsed_string" style="color:rgb(178,34,34)">'foo'</span><span class="double_arrow">=></span><span class="constant_encapsed_string" style="color:rgb(178,34,34)">'bar'</span>,<span class="whitespace">        </span><span class="constant_encapsed_string" style="color:rgb(178,34,34)">'baz'</span><span class="double_arrow">=></span><span class="lnumber" style="color:rgb(178,34,34)">123</span><span class="whitespace">    </span>)<span class="whitespace"></span>);</code>
ログイン後にコピー
调用变量:

<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">get</span>(<span class="constant_encapsed_string" style="color:rgb(178,34,34)">'var'</span>)</code>
ログイン後にコピー
删除变量:

<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">clear</span>(<span class="constant_encapsed_string" style="color:rgb(178,34,34)">'var'</span>)</code>
ログイン後にコピー
检查是否存在:

<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">exists</span>(<span class="constant_encapsed_string" style="color:rgb(178,34,34)">'var'</span>)</code>
ログイン後にコピー

刚才就说过一些特殊的名字是不能定义的,例如SESSION,REQUEST这些都不可以。但是因为他们有特殊含义并且经常调用,所以在php中有他们对应的映射变量,也就是说只要改php中这个量fatfree这个量也会变化。分别是$_SESSION和$_REQUEST。

也就说只要在前面加"%_"就行了、

这种定义好的全局变量很多,可以看这里来了解一下。


接下里开始快速学习,学过php或者C基本语法的都能看懂:

<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">set</span>(<span class="constant_encapsed_string" style="color:rgb(178,34,34)">'a'</span>,<span class="constant_encapsed_string" style="color:rgb(178,34,34)">'fire'</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">concat</span>(<span class="constant_encapsed_string" style="color:rgb(178,34,34)">'a'</span>,<span class="constant_encapsed_string" style="color:rgb(178,34,34)">'cracker'</span>);<span class="whitespace"></span><span class="echo" style="color:rgb(255,20,147)">echo</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">get</span>(<span class="constant_encapsed_string" style="color:rgb(178,34,34)">'a'</span>);<span class="whitespace"> </span><span class="comment" style="color:rgb(184,134,11)">// returns the string 'firecracker'</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">copy</span>(<span class="constant_encapsed_string" style="color:rgb(178,34,34)">'a'</span>,<span class="constant_encapsed_string" style="color:rgb(178,34,34)">'b'</span>);<span class="whitespace"></span><span class="echo" style="color:rgb(255,20,147)">echo</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">get</span>(<span class="constant_encapsed_string" style="color:rgb(178,34,34)">'b'</span>);<span class="whitespace"> </span><span class="comment" style="color:rgb(184,134,11)">// returns the same string: 'firecracker'</span></code>
ログイン後にコピー
还有一些原始函数:

<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">set</span>(<span class="constant_encapsed_string" style="color:rgb(178,34,34)">'colors'</span>,<span class="array" style="color:rgb(255,20,147)">array</span>(<span class="constant_encapsed_string" style="color:rgb(178,34,34)">'red'</span>,<span class="constant_encapsed_string" style="color:rgb(178,34,34)">'blue'</span>,<span class="constant_encapsed_string" style="color:rgb(178,34,34)">'yellow'</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">push</span>(<span class="constant_encapsed_string" style="color:rgb(178,34,34)">'colors'</span>,<span class="constant_encapsed_string" style="color:rgb(178,34,34)">'green'</span>);<span class="whitespace"> </span><span class="comment" style="color:rgb(184,134,11)">// works like PHP's array_push()</span><span class="echo" style="color:rgb(255,20,147)">echo</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">pop</span>(<span class="constant_encapsed_string" style="color:rgb(178,34,34)">'colors'</span>);<span class="whitespace"> </span><span class="comment" style="color:rgb(184,134,11)">// returns 'green'</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">unshift</span>(<span class="constant_encapsed_string" style="color:rgb(178,34,34)">'colors'</span>,<span class="constant_encapsed_string" style="color:rgb(178,34,34)">'purple'</span>);<span class="whitespace"> </span><span class="comment" style="color:rgb(184,134,11)">// similar to array_unshift()</span><span class="echo" style="color:rgb(255,20,147)">echo</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">shift</span>(<span class="constant_encapsed_string" style="color:rgb(178,34,34)">'colors'</span>);<span class="whitespace"> </span><span class="comment" style="color:rgb(184,134,11)">// returns 'purple'</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">set</span>(<span class="constant_encapsed_string" style="color:rgb(178,34,34)">'grays'</span>,<span class="array" style="color:rgb(255,20,147)">array</span>(<span class="constant_encapsed_string" style="color:rgb(178,34,34)">'light'</span>,<span class="constant_encapsed_string" style="color:rgb(178,34,34)">'dark'</span>));<span class="whitespace"></span><span class="variable" style="color:rgb(58,135,173)">$result</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">merge</span>(<span class="constant_encapsed_string" style="color:rgb(178,34,34)">'colors'</span>,<span class="constant_encapsed_string" style="color:rgb(178,34,34)">'grays'</span>);<span class="whitespace"> </span><span class="comment" style="color:rgb(184,134,11)">// merges the two arrays</span></code>
ログイン後にコピー
因为要做项目了,接下来会加快讲解,不懂的评论问我。


Fatfree的错误机制:

如果你觉得系统内置的错误机制没有很好地适应你的程序,你可以自己生成自己的错误机制:

<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">set</span>(<span class="constant_encapsed_string" style="color:rgb(178,34,34)">'ONERROR'</span>,<span class="whitespace">    </span><span class="function" style="color:rgb(255,20,147)">function</span>(<span class="variable" style="color:rgb(58,135,173)">$f3</span>)<span class="whitespace"> </span>{<span class="whitespace">        </span><span class="comment" style="color:rgb(184,134,11)">// custom error handler code goes here</span><span class="whitespace">        </span><span class="comment" style="color:rgb(184,134,11)">// use this if you want to display errors in a</span><span class="whitespace">        </span><span class="comment" style="color:rgb(184,134,11)">// format consistent with your site's theme</span><span class="whitespace">        </span><span class="echo" style="color:rgb(255,20,147)">echo</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">get</span>(<span class="constant_encapsed_string" style="color:rgb(178,34,34)">'ERROR.title'</span>);<span class="whitespace">    </span>}<span class="whitespace"></span>);</code>
ログイン後にコピー

error变量其实是一个内置的数组,他的元素分别是:

<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">`ERROR.code` - the HTTP status error code (`404`, `500`, etc.)`ERROR.status` - a brief description of the HTTP status code. e.g. `'Not Found'``ERROR.title` - header and page title`ERROR.text` - error context`ERROR.trace` - stack trace stored in an `array()`</code>
ログイン後にコピー

在做项目的时候最好把debug的最高等级设定好,这样就能更容易找出错误的根本原因:

<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">set</span>(<span class="constant_encapsed_string" style="color:rgb(178,34,34)">'DEBUG'</span>,<span class="lnumber" style="color:rgb(178,34,34)">3</span>);</code>
ログイン後にコピー
但是如果项目已经debug好了可以上市了,那就改为

<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">set</span>(<span class="constant_encapsed_string" style="color:rgb(178,34,34)">'DEBUG'</span>,<span class="lnumber" style="color:rgb(178,34,34)">0</span>);</code>
ログイン後にコピー

之前说过可以用set方法定义全局变量,但是我们需要的全局变量一般是很多的,如果全部都写到代码里面那就显得很庸余,所以我们常把一堆set的全局变量写在‘setup.cfg’里面,然后:

<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">config</span>(<span class="constant_encapsed_string" style="color:rgb(178,34,34)">'setup.cfg'</span>);</code>
ログイン後にコピー
或者把setup写成这样

<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="ini_section" style="color:black">[globals]</span><span class="ini_key" style="color:rgb(65,105,225)">num</span>=<span class="ini_value" style="color:rgb(220,20,60)">123</span><span class="comment" style="color:rgb(184,134,11)">; this is a regular string</span><span class="ini_key" style="color:rgb(65,105,225)">str</span>=<span class="ini_value" style="color:rgb(220,20,60)">abc</span><span class="comment" style="color:rgb(184,134,11)">; another way of assigning strings</span><span class="ini_key" style="color:rgb(65,105,225)">str</span>=<span class="ini_value" style="color:rgb(220,20,60)">"abc"</span><span class="comment" style="color:rgb(184,134,11)">; this is an array</span><span class="ini_key" style="color:rgb(65,105,225)">hash[x]</span>=<span class="ini_value" style="color:rgb(220,20,60)">1</span><span class="ini_key" style="color:rgb(65,105,225)">hash[y]</span>=<span class="ini_value" style="color:rgb(220,20,60)">2</span><span class="ini_key" style="color:rgb(65,105,225)">hash[z]</span>=<span class="ini_value" style="color:rgb(220,20,60)">3</span><span class="comment" style="color:rgb(184,134,11)">; dot-notation is recognized too</span><span class="ini_key" style="color:rgb(65,105,225)">hash.x</span>=<span class="ini_value" style="color:rgb(220,20,60)">1</span><span class="ini_key" style="color:rgb(65,105,225)">hash.y</span>=<span class="ini_value" style="color:rgb(220,20,60)">2</span><span class="ini_key" style="color:rgb(65,105,225)">hash.z</span>=<span class="ini_value" style="color:rgb(220,20,60)">3</span><span class="comment" style="color:rgb(184,134,11)">; this is also an array</span><span class="ini_key" style="color:rgb(65,105,225)">items</span>=<span class="ini_value" style="color:rgb(220,20,60)">7,8,9</span><span class="comment" style="color:rgb(184,134,11)">; array with mixed elements</span><span class="ini_key" style="color:rgb(65,105,225)">mix</span>=<span class="ini_value" style="color:rgb(220,20,60)">"this",123.45,FALSE</span></code>
ログイン後にコピー
其中,string型不一定要一行,可以写成

<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="ini_section" style="color:black">[globals]</span><span class="ini_key" style="color:rgb(65,105,225)">str</span>=<span class="ini_value" style="color:rgb(220,20,60)">"this is a \very long \string"</span></code>
ログイン後にコピー
路由也可以写在里面:

<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="ini_section" style="color:black">[routes]</span><span class="ini_key" style="color:rgb(65,105,225)">GET /</span>=<span class="ini_value" style="color:rgb(220,20,60)">home</span><span class="ini_key" style="color:rgb(65,105,225)">GET /404</span>=<span class="ini_value" style="color:rgb(220,20,60)">App->page404</span><span class="ini_key" style="color:rgb(65,105,225)">GET [email protected]</span>=<span class="ini_value" style="color:rgb(220,20,60)">Page->@controller</span></code>
ログイン後にコピー
更别说是map了

<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="ini_section" style="color:black">[maps]</span><span class="ini_key" style="color:rgb(65,105,225)">/blog</span>=<span class="ini_value" style="color:rgb(220,20,60)">Blog\Login</span><span class="ini_key" style="color:rgb(65,105,225)">[email protected]</span>=<span class="ini_value" style="color:rgb(220,20,60)">Blog\@controller</span></code>
ログイン後にコピー














関連ラベル:
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート