Home > Backend Development > PHP Tutorial > thinkphp基础入门(2),thinkphp基础入门_PHP教程

thinkphp基础入门(2),thinkphp基础入门_PHP教程

WBOY
Release: 2016-07-12 08:56:53
Original
958 people have browsed it

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>
Copy after login

<span> </span>

<span>然后我们在View文件夹中新增个Index文件夹(名称要与控制器名称一致,比如AaaController的控制器就要新建个Aaa文件夹)<span>,在Index里面新建个index.html文件(这名字要与Index控制器中display的函数名一致),输入以下代码:<br /></span></span>
Copy after login
<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>
Copy after login

<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>
Copy after login
<span> </span>
Copy after login
<span><em id="__mceDel"> </em></span>
Copy after login

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1110978.htmlTechArticlethinkphp基础入门(2),thinkphp基础入门 第一节介绍了thinkphp基本路径问题,第二节将介绍thinkphp的常见用法(M层跟V层) 我们先在Controller层新建...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template