YII2 Day4: Modular Development

Source: Internet
Author: User
Tags php web development

Dilemma

Do a few years in the Windows Environment Program development (about 200 modules, millions of lines of code), to do PHP Web development, the discovery of module-level code reuse is really bad ah, reuse is basically the code copy and paste level, coupling is too high, not conducive to code packaging and duplication of publishing. such as file upload, contains the front-end page part of the presentation code and server processing upload file code, need to deal with the following issues:

    1. The name of the input control, because the server needs this to get the information in the $_files
    2. If the file is an image, it needs to be judged whether it is a real image, whether it is scaled, added watermark, etc.
    3. File storage location
    4. When multiple files are uploaded, the server handles the return of the information, such as an exception to a file.
    5. File information storage, upload files to the server, to record in the database, the date, path, what, good management.

There are more details, single from these 5 problems, it has inevitably involved front-end presentation and back-end database, so the use of Yii extension (Extension) way, can not meet the actual requirements (students with different opinions can leave a message to discuss). Therefore need more complete carrier to realize these functions, naturally consider the form of modules to achieve. Here's how:

Possible solutions
    1. Set up a attach module to handle all business
    2. Front-end display, packaged with widgets, placed inside the Attach module, built with a widgets directory
    3. Processing upload files, using action to implement
    4. The image processing, may use the third party class library, in the Attach module, constructs a vendor directory, holds the third party class library
    5. database table initialization code for creating or generating the appropriate file upload record table

The directory structure looks like this:

In this way, the module to a certain extent, can be released independently, other Yii users, just put this module in the modules directory, in the configuration file with reference, OK.

Module dependencies

Real development, not so smooth, there will always be more needs to come in. For example, after the Attach module, from the security aspects, need to determine whether the user has logged in, and some of the user's operation rights can be uploaded files, this time, is bound to rely on the user module, on the module dependency, Yii did not provide the official solution, let everyone free play. My advice is to use declarative solutions, such as in module files, to add instructions. A more compelling approach is to add an interface that forces the invocation of a third-party implementation.

Separate front and rear stations

Usually when an application is established, there is a front-end, usually the whole frontend directory and a backend directory, which distinguishes between the foreground and the background. However, after the development of modular mode, in order to ensure that the module as far as possible self-description, the independence characteristics, need to distinguish between the front and back of the module, the proposed way is to establish two, that is, the foreground Fcontroller and background Foundation Acontroller, other modules internal controller, inherit the corresponding controller, This enables the separation of the front and rear tables within the module. What is involved in the separation of the front and rear tables under simple summary

    1. User separation, front-end controller authentication is the foreground user, background controller authentication is the background user information
    2. Layout separation, front and rear layout should be very different, so in the Fcontroller and Acontroller two base classes, you need to set different layout, to achieve separation of layouts
    3. ... (not yet thought more, welcome to add)
Cross-module invocation

Cross-module calls inevitably occur, fortunately yii1.x has a getmodule method that initializes the module and resolves the file import problem. The drawback is that the IDE's code hints do not prompt for dynamic module calls, but can be resolved in phpdoc way, to participate in another blog post. Yii2, added a getinstance () method, this time the perfect solution to the code automatically prompted questions, like one.

Directory structure

As a result, the directory structure becomes this way:

Each function will try to solve the problem within the module.

YII2 Day4: Modular Development

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.