


Detailed explanation of how Composer+Git creates a 'service class library'
Introduction
I always think that now PHP has progressed into the field of engineering. In the past, PHP developers regarded speed as beauty, and speed and scale were always contradictory. Today's PHP projects, especially larger projects, have gradually evolved to a level that requires both engineering and scale. Engineering a code means evolving into an increasingly complex architecture. For complex architectures, microservices are often a good choice.
I needed this question in a recent project. I need to develop a map service. This service is of course not in the form of a simple class library, but has its own database and its own service interface. In this case, the best option is servitization. Of course, there are many ways to service, such as Thrift, HTTP, etc. But I evaluated the current department environment. PHP is the mainstream language, and the progress of my project is also relatively tight. In my eyes, Thrift, HTTP and other methods all use network protocols to achieve service decoupling. It seems to be a serious solution. I think this approach is not necessary when the project is not clearly in critical condition. The disadvantage of using network protocol servitization is that it introduces significant complexity. This complexity often means investment in manpower, material resources, and time. So I hope to be able to provide a "service class library" in the PHP language for development.
What I am thinking of is PHP’s Composer.
Modification of Composer
Create service class library
First, I need to change my "service class library" from My application (named xxx/main1) is independent. For this independence, I did not choose to create a directory in the application (in fact, I thought about creating a directory such as Services). However, if the code is coupled with the business program, I feel that due to human laziness, it is difficult to control oneself from beginning to end and insist on not using various convenient functions in the application. So my choice is to create a new project in the Git repository and name it xxx/mapService.
composer.json
Now there are two Git projects (xxx/main1 and xxx/mapService). I added the following statement to the composer.json file in main1:
The composer.json in mapService is as follows:
This configuration tells the main1 project that the Git address of mapService needs to be used version of.
Of course you need to pay attention to the following points:
- dev-master means directly using the master branch of mapService. If mapService has other tags, you can use the tag information here
- repositories is the address of the project
- The service I have here is placed on the GitLab built by our company
- mapService The namespace of the src folder below is xxxx\\xxxx\\MapService\\ and supports PSR-4
- mapService uses illuminate/database
Last use composer update -vvv can download the mapService we need and put it in the vendor directory.
Update and modification
Our editor is now in the main1 project. If we have edited and modified the mapService project, and want to merge it into the master branch of mapService , directly enter the vender/xxx/mapService directory and perform Git corresponding operations. This allows direct code modifications.
Independent configuration
The combination of this structure is only the first step in completing the long march of thousands of miles. What is more important later is that when writing this service, I need to always remember not to use everything in main1, so as to maintain the independence of mapService (independence is one of the necessary conditions for servitization). For example, the first problem I encountered was that the configuration file needs to be independent.
My implementation method is to create a Config class directly in mapService, and the configuration is directly written in this class.
I have always felt that the implementation of this configuration file is a bit frustrating, because in this way, this configuration file enters the Git library. But I really can't think of a better solution. There is a way in Laravel to create Config in Laravel's config folder by implementing ServiceProvider, but this method only applies to Laravel. There is no universality. On the other hand, I think which database the service uses is itself part of the service, and it seems to have nothing to do with putting it in the Git library of the service.
Directory structure
The directory structure is as above
- Configs provides configuration files
- Contracts provides interface protocols
- Exceptions provides exceptions
- Supports provides third-party methods or class libraries
- Models provides interaction with the database
- Node.php implementation Specific interface
The most important thing about the service is the interface protocol. So create a Contracts folder and interface the provided services.
# The exception handling of the interface should try to use exceptions instead of error codes for interaction. And these exceptions should be customized as much as possible. In this way, there is the possibility of unified processing at the upper level.
Thinking
I position this architectural model as a service-oriented model at the PHP code level. Applicable scenarios should be:
- Later planning service-oriented
- Scenarios where both manpower and thinking in the early stage hope to maintain rapid development
and Git The difference between SubTree and SubModule
In fact, these three methods all use one project as the class library of another project. SubTree and SubModule are Git solutions. Composer is a solution for the PHP language. In addition to the function of adding a project to another project, it also provides solutions such as adding versions and dependency solutions. If your project is in PHP, then using Composer is undoubtedly a better choice.
Later protocol serviceization
If my mapService wants to be protocol service-oriented later, then the mapService project can be simplified into an SDK. For the upper-layer business logic, Just use composer update to update.
Service registration and discovery
The so-called "service class library" I call here does not solve the problem of service registration. I have no way of knowing how many projects use me. services. This may require additional process work.
The above is the detailed content of Detailed explanation of how Composer+Git creates a 'service class library'. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











When developing an e-commerce website, I encountered a difficult problem: how to provide users with personalized product recommendations. Initially, I tried some simple recommendation algorithms, but the results were not ideal, and user satisfaction was also affected. In order to improve the accuracy and efficiency of the recommendation system, I decided to adopt a more professional solution. Finally, I installed andres-montanez/recommendations-bundle through Composer, which not only solved my problem, but also greatly improved the performance of the recommendation system. You can learn composer through the following address:

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

The Laravel framework has built-in methods to easily view its version number to meet the different needs of developers. This article will explore these methods, including using the Composer command line tool, accessing .env files, or obtaining version information through PHP code. These methods are essential for maintaining and managing versioning of Laravel applications.

VprocesserazrabotkiveB-enclosed, Мнепришлостольностьсясзадачейтерациигооглапидляпапакробоглесхетсigootrive. LEAVALLYSUMBALLANCEFRIABLANCEFAUMDOPTOMATIFICATION, ČtookazaLovnetakProsto, Kakaožidal.Posenesko

I'm having a tricky problem when doing a mail marketing campaign: how to efficiently create and send mail in HTML format. The traditional approach is to write code manually and send emails using an SMTP server, but this is not only time consuming, but also error-prone. After trying multiple solutions, I discovered DUWA.io, a simple and easy-to-use RESTAPI that helps me create and send HTML mail quickly. To further simplify the development process, I decided to use Composer to install and manage DUWA.io's PHP library - captaindoe/duwa.

Laravel 8 provides the following options for performance optimization: Cache configuration: Use Redis to cache drivers, cache facades, cache views, and page snippets. Database optimization: establish indexing, use query scope, and use Eloquent relationships. JavaScript and CSS optimization: Use version control, merge and shrink assets, use CDN. Code optimization: Use Composer installation package, use Laravel helper functions, and follow PSR standards. Monitoring and analysis: Use Laravel Scout, use Telescope, monitor application metrics.

To install Laravel, follow these steps in sequence: Install Composer (for macOS/Linux and Windows) Install Laravel Installer Create a new project Start Service Access Application (URL: http://127.0.0.1:8000) Set up the database connection (if required)
