This solution is a simple contact form API service (no frontend expected).
At a minimum this solution provides the ability to:
- Create a message (name, email address and message)
- View messages
- Notify an admin when the contact form API (create) service is triggered. To change the recipient of this email, the
.env.example
file needs to be updated with a new value. The key is called :email
and can be found in the.env.example
file, if there is no value for this email, the fallback email would be the one set in the constant file called:admin_email
with a value of[email protected]
- I have followed the TDD approach by having some feature testing in place by testing ability to create contact and view contact before implementation.
- After completing these tests, I started creating the application logic, controllers, routes, models and model factories.
- I used laravel's form requests for data validation and model resources and model resource collection to transform and expost the data to the enduser/api consumer.
- After successfully creating a contact, there is an observer that observes and automatically generates and assign a uuid to the new record.
- When a contact is saved, an admin receives this notification with the sender of the contact form, their email address and the message body.
- This implementation uses an action
App\Actions\CreateContact
to implement the business logic. - This implementation uses model resources in exposing some information from our databases and can be found in
App\Http\Resources
and expose the data returned from the Contact Model to the user.
- [Composer] for dependency management.
- [PhpUnit] [PhpUnit] for the test suite.
- [Mailhog] for email service provider.
Before setting up this repository, the following are the dependencies that needs to be available on your machine:
- Composer
- PHP (I have PHP 8.1.11 installed)
- Mailhog (as seen in the .env.exaple file). For more information about setup and instructions, see here for more info: https://github.com/mailhog/MailHog
- Clone the repository:
git clone https://github.com/deendin/contact-form-with-laravel.git
- Assuming that the Dependencies listed above are satisfied, you can
cd
into the directory calledcontact-form-with-laravel.git
- When inside this repository directory, run
composer install
to install the project dependencies. - Modify the .env file and set the required database credentials.
- Run
php artisan migrate
to generate the database tables. - To test, make sure you are still in this repository directory and in your terminal, to run the test suite run
vendor/bin/phpunit
for the test. - Go to your email service provider either Mailtrap or Mailhog (https://0.0.0.0:8025/), but I have tested this on Mailhog, you should see an email that contains the contact message, name and email.
At a minimum we are looking to take a message (name, email address and message) and view messages.
This can be found in the public directory. This file is called contact_api_service.postman_collection.json
- Add ability to fetch a single contact.
- Properly handle the API exceptions, possibly in the Handler file or have a custom exception that will be wrapped around a try catch just incase an unknown exception occurs.
- Add user auth and authorization checks/Gate or policy before allowing the create contact request logic to get executed in the CreateContact Action. This is expected to check if the authed user has the permission to perform the intended request.
- Add Github Action for PHP-CS-Fixer that will be triggered before commit or push. This will act like a pre-commit.
- Have a basic front end to allow creation of the contact.