Introducing Laravel Rocket (last part)

Tram Ho

This is the next article in the series about the  of a Japanese engineer.

If you haven’t seen part 1 then link here .

API design

We can generate API automatically from the Swagger file. At that time, create the API definition file with a filename like documents / apiv1.yml and run the following command:

After running this command, the necessary files for the API such as setting routing, Controller, etc. will be generated.

Business Logic

After the automatic generation is completed, we will start writing the logic to complete the application.

In Laravel Rocket’s worldview, logic is not written in the Controller or Model, but in the Service layer. So you need to define the service in app / Services . Laravel Rocket also has a Generator ready for this.

After running the above command, the MessageService file will be generated, you will add methods to this file to define the logic.

In addition, common methods that are called from different locations not just from the Controller will write to the Helper. Of course, this Help can also be automatically generated.

You run the following command to generate the Help file.

Whether it is Service or Helper, when generating, will generate test file as well. However, this test file currently only stops at the level of checking whether it is possible to generate instances or not (because there is no method at all). In the future the author also intends to edit to be able to add more tests to the methods that have been added later.

Change the table structure

In case you want to change the table structure after generating , you edit files / db.mwb and then generate again with the following command.差分 が 更新 さ れ 、 Alter Table が 行 わ れ る Migration フ ァ イ ル が 生成 さ れ 、 Model 、 Repository 等 も ア ッ プ デ ー ト さ れ ま す。

After running this command, the changed content will be updated, the Migration file used for Alter Table will be generated, and other related files such as Model, Repository will also be updated.

However, for updating the PHP class such as Model, Repository, you should note that: The tool retains only the modified content manually after generate within the scope of possible. Therefore you need to confirm again after editing.

Edit Admin CRUD

Admin CRUD processes are already implemented using React, you can generate these React files with the APIs needed to use. However, you will have to customize quite a lot.

To customize, you edit the file * resources / assets / admin *, and then rebuild with yarn dev for example.

The generated code structure of Laravel Rocket

Repository Pattern

Repository Pattern of Laravel. This is where the focus of the Model’s generate, update, get handles.

Its additional features include:

  • Can add more methods in a more flexible way.

For example, you can specify the name of a certain column to get data by defining methods like findByMessage; Or you can get with the specified filter as an array like allByFilter

  • Filter is predefined by the buildQueryByFilter method

Therefore you can define filters freely by overriding this function. So you don’t need to define many functions to still access the model.

Decorator Pattern

A logical division pattern used for View from the Model, there is a Presenter class available for each Model in the app Presenters folder (geneate automatically).

This presenter is a self-written author, not a lib.

You can access the Presenter by inserting a present method as above. If there is no method described in Presenter, if there is name data in the Model, it will be output automatically.

For example, if no method name is defined, it will call the property name of $ model.

Response Object

In Laravel, Requests are divided into objects and can perform actions like Validation in them.

However, Response is also defined in object form and can perform operations like convert data of Model.

The classes are placed in the app Http Responses folder.

Conclusion

The introductory here is the end.

Hope everyone has a better overview of this Laravel Rocket tool.

Personally, I see that although this tool can automatically generate many necessary files, it also needs to customize a lot.

At the same time the author is writing the tool for both use and not for the community so there will be more points to be improved. Therefore should not use the tool at this time. Later, wait for him to improve further, so he might use it well ?

Share the news now

Source : Viblo