This chapter is based on PHP Laravel

preface

People often ask

  • How to design a catalog?
  • How is the code distributed?
  • How do you write a maintainable project?

“Bad” project I also did not write less, the following is a reference to the Internet each big guy’s article summary and personal development experience.

Controller

When you start PHP, you know that Controller represents the C layer of MVC. The concept of MVC itself is code separation, which teaches you how to separate business. However, with the continuous development of business, the complexity of code also increases, and the links between functions are complicated. You end up with an MVC that looks like this, so it’s no longer possible to build a growing business solely on the idea of an MVC.

Now let’s redefine the Controller’s tasks and capabilities so that the Controller only controls Http Reqeust requests, thus conforming to the SOLID single function principle.

Writing business code directly in the Controller makes the code extremely bloated and difficult to maintain and extend

<? php namespace App\Http\Controller; class UserController extends Controller{ publicfunction register(Request $request) {$user = new User();
			$user->username = $request->input('username');
			$user->password = $request->input('password');
			$result = $user->save();

			return $result; }}Copy the code

At this point, we should think about how to separate the business code. Do we introduce the concept of Service

Service

Service itself is translated as Service

  • Inject external methods, public methods, into services
  • Inject the Service into the controller

UserController

<? php namespace App\Http\Controller; class UserController extends Controller{ public$request;
		
		protected $userService;
		
		public function __construct(Request $request, UserService $userService)
		{
			$this->request = $request;
			
			$this->userService = $userService;
		}
		
		public function register()
		{
			//... validation
			return $this->userService->register ($this->request->all()); }}Copy the code

UserService

<? php namespace App\Service; class UserService{ publicfunction register($data)
		{
            $username = $data['username'];
            $password = $data['password'];
         
			$password = encrypt ($password);
			
			$user = new User();
			$user->username = $username;
			$user->password = $password;
			$result = $user->save();

			return $result; }}Copy the code

So far, we have at least kept the business completely separate from the request. However, if all the services and curds are written in the Service, it will just transfer the bloated Controller to the Service, and the Service will have no meaning. Therefore, we need to continue to split the Service and separate the R operation to the database, because the OPERATION of CUD is basically constant, while the R operation varies according to the complexity of the business. So independent R operations. At this point we refer to the concept of Repository.

Repository

We use Repository to assist the Model and encapsulate the related query logic into different Repositories to facilitate the maintenance of logical code

  • Conforms to the single principle of SOLID
  • Reversal of dependencies conforming to SOLID

UserController

<? php namespace App\Http\Controller; class UserController extends Controller{ public$request;
		
		protected $userService;
		
		public function __construct(Request $request, UserService $userService)
		{
			$this->request = $request;
			
			$this->userService = $userService;
		}
		
		public function getUserInfo()
		{
			//... validation
			return $this->userService->getUserInfo ($this->request->all()); }}Copy the code

UserService

<? php namespace App\Service; class UserService{ public$userRepository;
        
        public function __construct(UserRepository $userRepository) {$this->userRepository = $userRepository;
        }
        public function getUserInfo()
		{
            return $this->userRepository->getUserInfo($data); }}Copy the code

UserRepository

<? php namespace App\Repository; class UserRepository{ publicfunction getUserInfo($data)
		{
            $userId = $data['user_id'];
            $result = User::where('id'.$userId)->first();
			
			return $result; }}Copy the code

After solving the problem of R, someone asked, can CUD be put together because it is relatively unified and simple? The answer is NO, and we use a new noun, Action.

Action

This was learned from @charlie_Jade’s article

Each operating independently file, such as the CreateUser, DeleteUser, UpdateUser

  • Conforms to the single principle of SOLID

UserController

<? php namespace App\Http\Controller; class UserController extends Controller{ public$request;
		
		protected $userService;
		
		public function __construct(Request $request, UserService $userService)
		{
			$this->request = $request;
			
			$this->userService = $userService;
		}
		
        public function register() {/ /... validationreturn $this->userService->register($this->request->all());
        }

		public function getUserInfo()
		{
			return $this->userService->getUserInfo ($this->request->all()); }}Copy the code

UserService

<? php namespace App\Service; class UserService{ publicfunction getUserInfo(UserRepository $userRepository)
		{
            return $this->userRepository->getUserInfo($data);
		}

        public function register() {$result = (new CreateUser())->execute($this->request->all());
            
            return $result; }}Copy the code

UserRepository

<? php namespace App\Repository; class UserRepository{ publicfunction getUserInfo($data)
		{
            $userId = $data['user_id'];
            $result = User::where('id'.$userId)->first();
			
			return $result; }}Copy the code

CreateUser

<? php namespace App\Action; use App\Model\Member; class CreateUser extends CreateUserWallet { publicfunction execute(array $data)
		{
			$models           = new Member();
			$models->tel      = $data['tel'];
			$models->password = $data['password'];
			$result           = $models->save ();
				
			return $result; }}Copy the code

The above code logic is shown in the following figure

Common

In development, you may need some common methods (non-public classes, such as email, etc.) such as checking a user’s balance, checking whether a user is registered or online, generating an order number, etc. It’s easier to use Common. It’s more like a public library

Event

Optionally used when you don’t care about the result, but Listen for Event also provides a queue.

Exception

Don’t use Return for all your error messages. Many times your Return may not be your Return

Thank you

Thank you for reading this post, and if you have any new ideas, please feel free to discuss them in the comments section.

Refer to the article

Laravel large-size project architecture: http://oomusou.io/laravel/architecture/#Service Laravel application architecture design idea using action: https://segmentfault.com/a/1190000015208089? How to use the Service mode: http://oomusou.io/laravel/service/ SOLID object oriented design principles: https://www.cnblogs.com/shanyou/archive/2009/09/21/1570716.html