Home >php教程 >php手册 >thinkphp基础入门(2),thinkphp基础入门

thinkphp基础入门(2),thinkphp基础入门

WBOY
WBOYOriginal
2016-06-13 08:44:12894browse

thinkphp基础入门(2),thinkphp基础入门

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

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

新建个index函数

<span><?<span>php
 namespace Home\Controller;
 </span><span>use</span><span> Think\Controller;
 </span><span>class</span> IndexController <span>extends</span><span> Controller {
     
     </span><span>public</span> <span>function</span><span> index(){
         </span><span>$this</span>->assign('variable',"输出变量到模板中"); <span>//</span><span>assign的作用是将控制器的变量输出到V层</span>
        <span>$this</span>->display();<span>//</span><span>assign完后一定要display一下才能输出到模板中</span>
     }</span>

 

<span>然后我们在View文件夹中新增个Index文件夹(名称要与控制器名称一致,比如AaaController的控制器就要新建个Aaa文件夹)<span>,在Index里面新建个index.html文件(这名字要与Index控制器中display的函数名一致),输入以下代码:<br /></span></span>
<span><span><!</span><span>DOCTYPE html</span><span>></span>
<span><</span><span>html </span><span>lang</span><span>="en"</span><span>></span>
<span><</span><span>head</span><span>></span>
    <span><</span><span>meta </span><span>charset</span><span>="UTF-8"</span><span>></span>
    <span><</span><span>title</span><span>></span>Document<span></</span><span>title</span><span>></span>
<span></</span><span>head</span><span>></span>
<span><</span><span>body</span><span>></span><span>
    {$variable}
</span><span></</span><span>body</span><span>></span>
<span></</span><span>html</span><span>></span></span>

 

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