Infyom – When coding gets too boring

Tram Ho

Laravel is a PHP framework that has been developed and used widely in the past few years, Laravel versions from constantly being updated and upgraded several times a year. There are updates, but in general, the usage of the developer framework has not changed much since the framework’s directory structure is almost still around the main components:

  1. Controllers
  2. Models
  3. Migrations
  4. Repositories
  5. Routes
  6. Views

With a basic CRUD project, having to repeat the action of creating controller, models, repositories … makes developers sometimes feel really boring, not to mention having to copy and paste html interface from one module to another, just replace each name, or title. Moreover, the setup of packages and themes takes a lot of time. To solve this problem in today’s post I would like to introduce the Infyom package, a powerful laravel package that allows auto-generating almost all of the boring things in a laravel project. Infyom allows you to use a simple command line to generate api doc, controller, migration, model … and even the LTE admin theme, bootstrap, metronic … Imagine just a few simple commands you have created. a user management screen with built-in functions of adding, editing, deleting, sorting, paging … Too idle? Currently, laravel’s blade is gradually being replaced by Front-end libraries with better experience like ReacJS, Vuejs … so the remarkable part of Infyom is the auto-generate CRUD api. Let’s see what it has.

1. Setup

Infyom setup is completely similar to other laravel packages. Currently the version of Infyom is continuously updated to match the corresponding version of Laravel. So which laravel version you are using, use the corresponding Infyom version. Since laravel 8 has just released, Infyom has not updated yet, it is currently version of Infyom 7. Maybe v8 will be available tonight

Next, open the appProvidersRouteServiceProvider.php and edit mapApiRoutes as follows:

This will allow us to separate the controller related API will be automatically generated in the Http/Controllers/API. Next if you want to create basic working folders for your project, then just

And your directory structure should be similar to this, with the necessary base files and directories available

2. How to use?

The usage is quite simple, we can refer to https://labs.infyom.com/laravelgenerator/docs/7.0/introduction Like other pakages after publishing we will have a very important file located. In config/infyom/laravel_generator.php Here are mainly true / false options, if you do not need to use something you just need to be false, otherwise it is true. For example

If you want to use softDelete for the table then you need to change it to true instead of false as default. One weakness is that you do not have an option command to specify which table will have softDelete and which table is not to use softDelete. So before you initialize the table you need to go to this config to fix it manually. After configuring is done we will try to create api for the module with full CRUD functionality, for example Category

After running the above command it will ask us to enter the fields of the table

Inside:

  • name: is the name of the field, for example: category_name
  • db_type: data type, eg string . If you want to add the number of characters, enter string,255
  • html_type: is the html type in the form, for example: input, textarea …
  • options: additional information if present, which is usually ignored.

Next, infyom will ask you about validation of this field for example: required, unique …

If you do not want to enter any more fields, you just need to Enter to ignore. Once created, the list of generated files should look like this, including migration files

The system will ask if you want to run the migrate to create a database, you can choose Yes / No. Here I choose Yes and because the new project created it runs all available navigation files.

However, pay attention, if after typing yes, you get any errors such as: database error has not been created, cannot connect … Then after fixing the error, you need to run the migation using laravel command as usual. .

So successfully created the necessary files for the project, moreover, when opening these files, there is always CRUD code available for you, all you need to do is edit it according to your needs and use it.

Okay, but sometimes you make a mistake and want to start over, Infyom gives you a rollback command to undo what you just did.

Inside :

  • $ MODEL_NAME: My Model name here is Category
  • $ COMMAND_TYPE: api, scaffold or api_scaffold, in my example it’s api

However, it is not very happy that if you run the migration file, the rollback will not delete that table in the database, but if you want to delete the table in the db, you must run the laravel migration:rollback command first. I don’t really understand the intentions of developer Infyom =)). However, in return when looking in the pile of files that Infyom creates, you will be very happy, because this is a job that all developers hate is to write Test. Infyom has written tests for you both controller and repository already. (Get high)

Alternatively, if you don’t want to create such files, you can create them individually using the commands that Infyom provides below:

Create migration

Create model

Controller creation

Create repository

Create a request

Create test

There are also many other commands related to template creation, but in the scope of this article I did not mention, you can refer to the Infyom homepage https://labs.infyom.com/laravelgenerator/docs/ 7.0 / generator-commands

Creating a relationship

This part is quite long, you can read more at https://labs.infyom.com/laravelgenerator/docs/7.0/relationships offline. There are all the relationships you need and when initializing the Infyom table will automatically create for you.

3. Conclusion

So I have introduced you to a pretty good package to help minimize the boring work in the coding process, hope you will bring infyom to your project for the same experience. No matter how good a package is, there will be strengths and weaknesses, hopefully you will know how to apply it in the right place at the right time depending on the circumstances of the project. Thanks!

4. References

https://labs.infyom.com/laravelgenerator/

Share the news now

Source : Viblo