Home  >  Q&A  >  body text

Regarding whether it is necessary to create a new Base.php. . . .

Isn’t there a public common class in thinkphp5? Moreover, there is also a common class definition under the default APP. If there is no common under the module, it will be inherited under the APP. After the common.php file is created under the controller module, it can be inherited directly without reference. . . It’s also easy to control and reduces code. . . Creating a new Base is a bit unnecessary, isn't it? Please ask the teacher to clarify! !

A.zlA.zl2620 days ago1127

reply all(1)I'll reply

  • ringa_lee

    ringa_lee2017-09-11 17:15:12

    It is definitely necessary. Many public operations must be written in the base.php file. For example, if you log in, you can just make a judgment in the base.php file. You don’t need to verify it in the controllers of other subclasses. Whether you are logged in or not, you need to write the login verification in the public common class, and you need to verify it once in each controller. It is no different from process-oriented development, and you cannot experience the meaning of object-oriented development.

    reply
    0
  • A.zl

    I don’t understand what you mean. There is no problem in writing public verification in the common class. For example, login verification. For the backend, there are only two situations, one is logged in and the other is not logged in. Then you can write it like this ( code show as below) $request = request(); if($request->path()=='admin/user/login'){ if(session('admin.admin_id')){ $this -> error('You are already logged in!','index/index'); } }else{ if(!session('admin.admin_id')){ $this -> error('You are not logged in yet!','user/login'); } } There is no problem in writing this way! It also eliminates the need to add use appadmincommonbase; in each controller class. After all, the meaning of a public class is a class that everyone uses. Since this process has been done in thinkphp, it is necessary to create a new base.php file. That's up for debate. . After all, the more code and the more calls, the more problems there will be, I don’t know if that’s right! . . .

    A.zl · 2017-09-11 17:24:25
  • Cancelreply