Thinkphp5’s directory architecture:

1. Model layer

1. Complex project design needs to distinguish data layer, Logic layer, Service layer and other different Model layers, so we can create Model, Logic and Service directories under the module directory, and divide all Model operations on user table into three layers:

  • Data layer: Model/UserModel is used to define data-related auto-validation, auto-completion, and data access
  • Logical layer: Logic/UserLogic Defines user-related business Logic
  • Service layer: Service/UserService Defines Service interfaces related to users

2. All three Model operation classes inherit from Model class.

Data layer: Home/Model/UserModel. Class. PHP

namespace Home\Model;
use Think\Model;
class UserModel extends Model{}Copy the code

Logic layer: Home/Logic/UserLogic class. PHP

namespace Home\Logic;
use Think\Model;
class UserLogic extends Model{}Copy the code

Service layer: Home/Service/UserService class. PHP

namespace Home\Service;
use Think\Model;
class UserService extends Model{}Copy the code

We can use Thinkphp’s built-in D method to call these three model classes:

D('User') // Instantiate UserModel
D('User'.'Logic') // Instantiate UserLogic
D('User'.'Service') // Instantiate UserService
Copy the code

2. View layer

1. Fetch () takes the name of the template and prints it with the template file.

class Index extends Controller{
    /** * Home page */
    public function index()
    {
        return $this->fetch(); }}Copy the code

If fetch() does not pass arguments, the program will automatically look for view/index/index.html render output.

If you pass an argument, such as “hello”, the program will look for view/index/hello.html to render the output.

     /** * Home page */
    public function index()
    {
        return $this->fetch('hello');
    }
Copy the code

2. Display () simply prints what is passed. If no argument is passed, the Layout is rendered, but there is no content.

class Index extends Controller{
    /** * Home page */
    public function index()
    {
        return $this->display(); }}Copy the code

If you pass an argument, such as “hello”, the page will simply print the string “hello”.

     /** * Home page */
    public function index()
    {
        return $this->display('hello');
    }
Copy the code

3. View () and fetch(0) are used the same way:

class Index extends Controller{
    /** * Home page */
    public function index()
    {
        returnview(); }}Copy the code

Controller Controller layer

ThinkPHP’s controller layer consists of the core controller and the business controller:

  • The core controller is completed by the internal App class of the system and is responsible for the scheduling control of applications (including modules, controllers and operations), including HTTP request interception, forwarding, loading configuration, etc.
  • Service controllers are completed by user-defined controller classes.

The implementation principle of the multi-layer service controller is similar to the hierarchical model, as follows:

Controller/UserController // For user business logic control and scheduling
Event/UserEvent // For the user's event response operations
Copy the code
  • The access controller is responsible for external interactive responses through URL requests, such as http://domain name /home/user/login.html
  • The event controller **** is responsible for internal event responses and can only be invoked internally, so it is isolated from the outside world. Specifically, hierarchical controllers outside of the access controller can only be instantiated internally.

1, the access Controller Home/Controller/UserController PHP are defined as follows:

namespace Home\Controller;
use Think\Controller;
class UserController extends Controller{
        // User login
        public function login(){
            // Triggers the user login event
            $event = new \Home\Event\UserEvent();
            $event->login();
        }
         // User registration
        public function register(){
            // Trigger the user registration event
            $event = new \Home\Event\UserEvent();
            $event->register(); }}Copy the code

Home/Event/ userevent.php

namespace Home\Event;
use Think\Controller;
class UserEvent extends Controller{
        // User login event
        public function login(){
           // Write the business logic here
        }
        // User registration event
        public function register(){
           // Write the business logic here}}Copy the code

3, call multi-level controller three ways:

Direct instantiation:

namespace Home\Controller;
use Think\Controller;
class UserController extend Controller {
        // User login
        public function login(){
            // Triggers the event
            $event = new \Home\Event\UserEvent();
            $event->login(); }}Copy the code

A function instantiation:

namespace Home\Controller;
use Think\Controller;
class UserController extend Controller {
        // User login
        public function login(){
            // Triggers the event
            $event = A('User'.'Event');
            $event->login();
            // Or use it directly
            // R('User/login','','Event');         }}Copy the code

R function call:

namespace Home\Controller;
use Think\Controller;
class UserController extend Controller {
        // User login
        public function login(){
            // Triggers the event
            R('User/login'.' '.'Event'); }}Copy the code