Home > php教程 > php手册 > thinkphp3.2.2前后台公用类架构问题,thinkphp3.2.2公用

thinkphp3.2.2前后台公用类架构问题,thinkphp3.2.2公用

WBOY
Release: 2016-06-13 09:20:25
Original
1059 people have browsed it

thinkphp3.2.2前后台公用类架构问题,thinkphp3.2.2公用

3.13之前好多项目都使用前后台公用类,在lib/action下创建Baseaction做为公共继承类,发现3.2.2里面很多人都用A调用,这样每用一次要用A调用,好麻烦,小编特意偷懒。亲测使用以下方法解决。欢迎加强

thinkphp3.2.2的这么创建Application/Common/Controller/BaseController.class.php

<span> 1</span> <?<span>php
</span><span> 2</span> <span>namespace Common\Controller;
</span><span> 3</span> <span>use</span><span> Think\Controller;
</span><span> 4</span>     <span>/*</span><span>*
</span><span> 5</span> <span>    * 前后台公用基类
</span><span> 6</span> <span>    * modify author : Jack
</span><span> 7</span> <span>    * modify time : 2014-7-12
</span><span> 8</span>     <span>*/</span>
<span> 9</span>     <span>class</span> BaseController <span>extends</span><span> Controller{
</span><span>10</span>         
<span>11</span>         <span>public</span> <span>function</span> _initialize() {<span>//</span><span>全局变量</span>
<span>12</span>                dump('基类'<span>);
</span><span>13</span>             <span>$this</span>-><span>cfg();
</span><span>14</span> <span>        }
</span><span>15</span>      }
Copy after login

在Home/Controller/ZixunController.class.php中

<?<span>php
namespace Home\Controller;
</span><span>use</span><span> Common\Controller\BaseController;
</span><span>class</span> ZixunController <span>extends</span><span> BaseController {

    </span><span>public</span> <span>function</span><span> index() {
        </span><span>$result</span> = <span>$this</span>-><span>lists();
        dump(</span><span>$result</span><span>);
    }
}</span>
Copy after login

      当然,在前后台还可以创建自己的基类,比如后台建AdminController.class.php继承BaseController.class.php,前台创建HomeController.class.php继承BaseController.class.php各自模块继承各自的基类,这样项目可以更清晰,可以避免重复造轮子,省很多事情,但是必须注意的是每个类必须声明命名空间,但是使用的资源可以在各自的基类中定义之后后面不用在写一次。比如AdminController.class.php继承BaseController.class.php,就不用再写use Think\Controller了,直接使用use Common\Controller\BaseController就可以了。

Related labels:
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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template