Home > Backend Development > PHP Tutorial > Thinkphp3.2.2 front and backend common class architecture issues, thinkphp3.2.2 common_PHP tutorial

Thinkphp3.2.2 front and backend common class architecture issues, thinkphp3.2.2 common_PHP tutorial

WBOY
Release: 2016-07-13 10:13:16
Original
941 people have browsed it

Thinkphp3.2.2 front-end and back-end common class architecture issues, thinkphp3.2.2 public

Many projects before 3.13 used front-end and back-end common classes, create Baseaction as a public class under lib/action Inheriting the class, I found that many people in 3.2.2 use A to call, so every time I use it, I have to use A to call, which is very troublesome, and the editor is deliberately lazy. Personal test uses the following method to solve it. Welcome to strengthen

thinkphp3.2.2 creates 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

In 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

Of course, you can also create your own base class in the front and backend. For example, if you create AdminController.class.php in the backend and inherit BaseController.class.php, and in the frontend you create HomeController.class.php and inherit BaseController.class.php, each module inherits its own base. Class, this way the project can be clearer, avoid reinventing the wheel, and save a lot of things. However, it must be noted that each class must declare a namespace, but the resources used can be defined in their respective base classes without having to write them again later. For example, if AdminController.class.php inherits BaseController.class.php, there is no need to write use ThinkController. Just use use CommonControllerBaseController.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/916820.htmlTechArticlethinkphp3.2.2 front-end and back-end common class architecture issues, thinkphp3.2.2 common before 3.13, many projects used front-end and back-end common classes , create Baseaction as a public inheritance class under lib/action, and find...
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template