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> }
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>
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.