Stars: 132
Forks: 30
Pull Requests: 9
Issues: 1
Watchers: 13
Last Updated: 2020-04-09 21:41:55
Bootstrap Application for Symfony with Domain Driven Design
License: MIT License
Languages: PHP, HTML, Dockerfile, Shell
Article application with Symfony 4 and DDD Approach.
Advantages of Domain-Driven Design
├── bin
├── config
│ ├── packages
│ │ ├── dev
│ │ ├── prod
│ │ └── test
│ └── routes
│ └── dev
├── public
├── src
│ ├── Authorization
│ │ ├── Console
│ │ ├── Controller
│ │ └── Entity
│ │ └── Oauth2
│ ├── DataFixtures
│ └── Project
│ ├── App
│ │ ├── EventListener
│ │ ├── Interface
│ │ ├── Support
│ │ └── Trait
│ ├── Console
│ ├── Domain
│ │ ├── Article
│ │ │ └── Entity
│ │ └── User
│ │ ├── Contract
│ │ └── Entity
│ ├── Http
│ │ └── Controller
│ ├── Infrastructure
│ │ ├── Article
│ │ └── User
│ └── Resources
│ ├── config
│ ├── doctrine
│ │ └── mapping
│ └── routing
├── templates
├── tests
│ ├── functional
│ ├── integration
│ └── unit
├── translations
└── var
├── cache
└── log
git clone https://github.com/bencagri/symfony4-ddd-skeleton.git
cd symfony4-ddd-skeleton
composer install
bin/console doctrine:database:create
bin/console doctrine:schema:update --force
bin/console doctrine:fixtures:load
php -S 127.0.0.1:9002 -t public
then visit 127.0.0.1:9002/api/doc
you should see the documentation now.
# Start the application
docker-compose up -d
# View logs to monitor installation progress
docker-compose logs -f
# Stop the application
docker-compose down
When you try to make a request you will get an error. Because to make a request you need Access Token
.
Lets try making a request;
curl -X GET "http://127.0.0.1:9002/api/articles?page=1&per_page=10" -H "accept: application/json"
It should say;
{
"success": false,
"error": {
"code": 0,
"message": "A Token was not found in the TokenStorage.",
...
This project is using Oauth2. So, first you need is to create a client. For this, it also has a command to create a oauth client.
bin/console oauth:client:create
you should see something like;
Added a new client with public id 6_2x0l2r8t6e2o4sggww08wwk88gs8sggsog0wk8cow4w0gso0s0.
Go and check your oauth_client
table on database. you will see your secret also.
Basically we have Authorization code
, Password
, Client Credentials
and Refresh Token
grant types. You can read more on Oauth Grant Types.
In this case, I prefer to go with Client Credentials
grant type. For this, I need client_id
and client_secret
I already know client id, it produced a client and told me the ID when I run the command. And my secret is on database.
So, lets take an Access Token
to make a request to api.
curl -X GET \
'http://127.0.0.1:9002/oauth/v2/token?client_id=6_2x0l2r8t6e2o4sggww08wwk88gs8sggsog0wk8cow4w0gso0s0&client_secret=4biwdp70w2yog4gs0s0c0808kww0c88sowoggggsg0swk48w08&grant_type=client_credentials' \
-H 'cache-control: no-cache' \
-H 'content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW' \
-F client_id=1_4v0w0kiec9s0osgs8w40og8o4okc4ws08cos84c0gw4csoc8ws \
-F client_secret=4biwdp70w2yog4gs0s0c0808kww0c88sowoggggsg0swk48w08 \
-F grant_type=client_credentials
And The Response
{
"access_token": "OWUyMTMxYzJjN2I5Nzg0ZTQ1N2NlZDNkMWYxZjFiZGE5N2RjMTA4ZmI1ZTU4ZGE0YWI4NmU3YmQxZjgyNTJkZg",
"expires_in": 3600,
"token_type": "bearer",
"scope": null
}
great. I have an access token to make a request.
lets try to get an article now.
curl -X GET \
http://127.0.0.1:9002/api/article/1 \
-H 'authorization: Bearer OWUyMTMxYzJjN2I5Nzg0ZTQ1N2NlZDNkMWYxZjFiZGE5N2RjMTA4ZmI1ZTU4ZGE0YWI4NmU3YmQxZjgyNTJkZg' \
-H 'cache-control: no-cache' \
-H 'content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW' \
-H 'postman-token: 8635975a-6197-b8c6-c055-907a4668c503' \
-F client_id=1_4v0w0kiec9s0osgs8w40og8o4okc4ws08cos84c0gw4csoc8ws \
-F client_secret=4biwdp70w2yog4gs0s0c0808kww0c88sowoggggsg0swk48w08 \
-F grant_type=client_credentials
And the response is;
{
"success": true,
"data": {
"type": "article",
"id": "1",
"attributes": {
"title": "My Test Article 0",
"body": "lorem ipsum dolor sit amet.lorem ipsum dolor sit amet.lorem ipsum dolor sit amet.lorem ipsum dolor sit amet.lorem ipsum dolor sit amet.lorem ipsum dolor sit amet.lorem ipsum dolor sit amet.lorem ipsum dolor sit amet.lorem ipsum dolor sit amet.lorem ipsum dolor sit amet.",
"tags": {},
"createdAt": "2018-03-29"
},
"links": {
"self": "http://127.0.0.1:9002/api/article/1"
}
}
}
Great, I got the article information with a request to GET /api/article/1
This package was built around JSON API to take advantage of its features around efficiently caching responses, sometimes eliminating network requests entirely.