- The Question
- Stepwise Implementation
- Prerequisites
- Installing laravel
- Configuring a database
- Project Documentation
- Links
- Ending thoughts.
Create a simple subscription platform (only RESTful APIs with MySQL) in which users can subscribe to a website (there can be multiple websites in the system). Whenever a new post is published on a particular website, all it's subscribers shall receive an email with the post title and description in it. (No authentication of any type is required).
- Install laravel
- Prerequisites
- Configuring a database
- Project Planning.
- Links
- Ending Thoughts.
- Assumed you have composer installed from https://getcomposer.org
- Assumed you have node installed from https://nodejs.org (optional)
- Assumed you are using a Visual Studios Code as your IDE. (optional)
- Assumed you have
PHP>=7.*
installed in your system with the path of the executable in your$PATH
variable.
$ composer global require laravel/installer
$ laravel new your-app-name
$ cd your-app-name
$ composer update
If you use VS Code as your IDE, please continue with this command
$ code .
๐ก Please open an integrated terminal for your
laravel artisan
commands.
By default, laravel & this project assumes you are using MySQL.
- Driver: MySQL
- Username: any_username
- Password: any_password
- Database: some_name
:alert: Please update the necessary credentials in the
.env
found in the project's root. If you do not find one, use the.env.local
file to create one.
We have considered the following tables in the database:
- A table for multiple websites is websites.
- A table for posts on a website is posts.
- A table for list of subscriptions is mailing_list.
- A table for maintaining a email queue named
jobs
generated byphp artisan queue:table
.
The following tables define routes to the different API's.
Please append
/api
to the routes
Method | Route | Description |
---|---|---|
GET | /websites |
Get all websites |
POST | /websites |
Add a new website |
GET | /websites/{id} |
Get details of a website |
POST | /websites/{id} |
Update all details of a website |
PUT | /websites/{id} |
Specific details of a website |
Method | Route | Description |
---|---|---|
GET | /websites/{id}/posts |
Gets details of all posts of a website. |
POST | /websites/{id}/posts |
Add a new post to the website. |
GET | /websites/{id}/posts/{pid} |
Get details of a particular post of a website. |
POST | /websites/{id}/posts/{pid} |
Update all details of particular post of a website. |
PUT | /websites/{id}/posts/{pid} |
Update specific details of particular post of a website. |
Method | Route | Description |
---|---|---|
POST | /websites/{id}/subscribe |
Add a new subscriber to the website. |
PUT | /websites/{id}/subscribe |
Update name by email in query like [email protected] |
DELETE | /websites/{id}/subscriber |
Remove subscription from the specific website by email in query like [email protected] |
GET | /websites/{id}/subscriber |
Show the details of subscriber of a website by email in query like [email protected] |
Three models have been considered.
- A model for Website
- A model for Posts
- A model for Subscribers
The routes above, have their respective controllers bound by the Models.
- The WebsiteController, uses the Website Model to implement the routes defined for websites. The controller handles all functions of Create, Update, View/Read actions of a website.
- The PostController, uses the Posts Model to implement the routes defined for posts Create, Update, View/Read actions of a post.
- The MailingListController, uses the Subscriber Model to implement the routes defined for mailing-list/subscription. This controller is responsible for handling the mailing-list or subscription-table of a particular website.
A dedicated PostObserver is declared to observe for any new post created & dispatch a mailing job which is enqueued to the bottom of Queue.
The PostObserver is "booted" in the EventServiceProvider, defining that the Post Model is bound by this observer.
Notifications are not perfectly handled. There was no attention given to the templating. Only a working implementation can be guaranteed.
- To notify the subscriber about any new post (template by
php artisan make:mail
), NotifySubscriber is defined. - An asyncronous queue job (template generated by
php artisan make:job
) SendEmailJob handles the queue as well as finally invokes the "notifying" with the help of NotifySubscriber.
The following links may be helpful:
- Laravel Docs - https://laravel.com/docs [At the time of writing the version of the docs was 9.x]
- PHP Docs - https://www.php.net/manual/
- Composer - https://getcomposer.org
If you want to run the api, you can use the button below:
You can also use the JSON
link to deploy on Postman.
The front-end of this application will be a work-in progress. With this simple application, I have actually learnt a lot more than any other projects undertaken.
Thanks to Team Inisev....
Thank You & Regards,
@anrcry (Anweshan Roy Chowdury)