Home >Backend Development >PHP Tutorial >thinkphp基础入门(二)

thinkphp基础入门(二)

WBOY
WBOYOriginal
2016-06-13 12:29:02754browse

thinkphp基础入门(2)

第一节介绍了thinkphp基本路径问题,第二节将介绍thinkphp的常见用法(M层跟V层)

我们先在Controller层新建个IndexController.class.php(新建文件的格式为xxxController.class.php,建议首字母大写)其他写法,框架将不识别为控制器文件

新建个index函数

<span style="font-family: 'Microsoft YaHei'; font-size: 16px;"><span style="color: #000000;">php namespace Home\Controller; </span><span style="color: #0000ff;">use</span><span style="color: #000000;"> Think\Controller; </span><span style="color: #0000ff;">class</span> IndexController <span style="color: #0000ff;">extends</span><span style="color: #000000;"> Controller {          </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> index(){         </span><span style="color: #800080;">$this</span>->assign('variable',"输出变量到模板中"); <span style="color: #008000;">//</span><span style="color: #008000;">assign的作用是将控制器的变量输出到V层</span>        <span style="color: #800080;">$this</span>->display();<span style="color: #008000;">//</span><span style="color: #008000;">assign完后一定要display一下才能输出到模板中</span>     }</span>

 

<span style="font-family: 'Microsoft YaHei'; font-size: 16px;">然后我们在View文件夹中新增个Index文件夹(名称要与控制器名称一致,比如AaaController的控制器就要新建个Aaa文件夹)<span style="color: #888888;">,在Index里面新建个index.html文件(这名字要与Index控制器中display的函数名一致),输入以下代码:<br></span></span>
<span style="font-family: 'Microsoft YaHei'; font-size: 16px;"><span style="color: #0000ff;"><span style="color: #ff00ff;">DOCTYPE html</span><span style="color: #0000ff;">></span><span style="color: #0000ff;"><span style="color: #800000;">html </span><span style="color: #ff0000;">lang</span><span style="color: #0000ff;">="en"</span><span style="color: #0000ff;">></span><span style="color: #0000ff;"><span style="color: #800000;">head</span><span style="color: #0000ff;">></span>    <span style="color: #0000ff;"><span style="color: #800000;">meta </span><span style="color: #ff0000;">charset</span><span style="color: #0000ff;">="UTF-8"</span><span style="color: #0000ff;">></span>    <span style="color: #0000ff;"><span style="color: #800000;">title</span><span style="color: #0000ff;">></span>Document<span style="color: #0000ff;"></span><span style="color: #800000;">title</span><span style="color: #0000ff;">></span><span style="color: #0000ff;"></span><span style="color: #800000;">head</span><span style="color: #0000ff;">></span><span style="color: #0000ff;"><span style="color: #800000;">body</span><span style="color: #0000ff;">></span><span style="color: #000000;">    {$variable}</span><span style="color: #0000ff;"></span><span style="color: #800000;">body</span><span style="color: #0000ff;">></span><span style="color: #0000ff;"></span><span style="color: #800000;">html</span><span style="color: #0000ff;">></span></span></span></span></span></span></span></span>

 

<span style="color: #888888; font-family: 'Microsoft YaHei'; font-size: 16px;"> {$xxx} 这可以输出controller中assign的变量,这时我们在url输入http://localhost/你的文件夹名/index.php?m=Home&c=Index&a=index,将会出现“输出变量到模板”这几个字,这样就实现了输出变量到模板中。这个非常常用!!<br><br>然后第二个常用的地方就是U函数,I函数 这里不多说了 详见api文档<br><br>没有display函数的都是不会渲染到模板上的,可以用于操作逻辑,然后如果ajax中请求一个地址,这个请求地址为控制器某个函数(体现在url的a参数上),其他操作跟纯生的php编程差不多了。</span>
<span style="color: #888888; font-family: 'Microsoft YaHei'; font-size: 16px;"> </span>
<span style="font-family: 'Microsoft YaHei'; font-size: 16px;"><em id="__mceDel"> </em></span>
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