How do we build our Laravel project?
Laravel development is not just about writing code. We are going to review a text project, an essential approach to software quality assurance. In this project, we structure and organize our code to make it easier to maintain and understand. This is a vital practice for any developer, especially when working in a team.
First, we organize our code into specific folders. We have a folder under "http/drivers" called "Happy". This organization is not mandatory, but it helps to clearly separate different aspects of the project, especially when you start creating more modules or working on larger projects.
Why is the testing structure important?
Testing is crucial in development. We ensure that our code works correctly through testing, which avoids uploading bugs to production. By running our tests from the terminal, we can verify that the changes made do not affect what is already working correctly.
- We run the tests with:
vendor/bin/phpunit
- We check that all the tests pass without errors, which indicates that our implementation is correct.
How do we set up our test space?
To create a robust test environment, we set up a database specifically for this purpose. This ensures that the tests do not interfere with the main database or affect the actual data.
- We create a new file for the test database:
database.sqlite
- We set up access to this database in our configuration file:
'connections' => [ ' sqlite' => [ ' driver' => 'sqlite',.. . ' database' => base_path('database/database.sqlite'), ]] ]
What basic files should we prepare?
To start from scratch, we create several base files that will allow us to structure our project in a logical and orderly manner:
-
Tests
php artisan make:test Http/Controllers/Happy/Happy/PostControllerTest
-
Model, Factory and Migration
php artisan make:model Post -mf
-
Controller
php artisan make:controller Happy/PostController --resource
Each of these steps sets the stage for a clean and organized code. We keep the same folder structure in both the test area and the main control, which makes it easier for future programmers to understand the project.
How do we ensure success by following these practices?
We encourage you to replicate each of these steps in your project. Repetition and practice are essential to master these techniques. Building your Laravel projects with this methodology will allow you to keep your code clean, well organized and easy to maintain over time. Keep learning and improving your skills!
Want to see more contributions, questions and answers from the community?