Aurora - An Article Project with Symfony 4 and Domain Driven Design

in #utopian-io7 years ago (edited)

Symfony 4 DDD Approach

Article application with Symfony 4 and DDD Approach.

Advantages of Domain-Driven Design

  • Eases Communication: With an early emphasis on establishing a common and ubiquitous language related to the domain model of the project, teams will often find communication throughout the entire development life cycle to be much easier. Typically, DDD will require less technical jargon when discussing aspects of the application, since the ubiquitous language established early on will likely define simpler terms to refer to those more technical aspects.
  • Improves Flexibility: Since DDD is so heavily based around the concepts of object-oriented analysis and design, nearly everything within the domain model will be based on an object and will, therefore, be quite modular and encapsulated. This allows for various components, or even the entire system as a whole, to be altered and improved on a regular, continuous basis.
  • Emphasizes Domain Over Interface: Since DDD is the practice of building around the concepts of domain and what the domain experts within the project advise, DDD will often produce applications that are accurately suited for and representative of the domain at hand, as opposed to those applications which emphasize the UI/UX first and foremost. While an obvious balance is required, the focus on domain means that a DDD approach can produce a product that resonates well with the audience associated with that domain.

Read more about DDD

Folder Structure

├── bin
├── config
│   ├── packages
│   │   ├── dev
│   │   ├── prod
│   │   └── test
│   └── routes
│       └── dev
├── public
├── src
│   ├── Aurora
│   │   ├── App
│   │   │   ├── EventListener
│   │   │   ├── Interface
│   │   │   ├── Support
│   │   │   └── Trait
│   │   ├── Console
│   │   ├── Domain
│   │   │   ├── Article
│   │   │   │   └── Entity
│   │   │   └── User
│   │   │       ├── Contract
│   │   │       └── Entity
│   │   ├── Http
│   │   │   └── Controller
│   │   ├── Infrastructure
│   │   │   ├── Article
│   │   │   └── User
│   │   └── Resources
│   │       ├── config
│   │       ├── doctrine
│   │       │   └── mapping
│   │       └── routing
│   ├── Authorization
│   │   ├── Console
│   │   ├── Controller
│   │   └── Entity
│   │       └── Oauth2
│   └── DataFixtures
├── templates
├── tests
│   ├── functional
│   ├── integration
│   └── unit
├── translations
└── var
    ├── cache
    └── log

Lets make it work

git clone https://github.com/bencagri/symfony4-ddd.git
cd symfony4-ddd
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.

Making Request

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.

Tip: You can use also postman.

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' 

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.



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Thank you for the contribution. It has been approved.

I am a big fan of DDD, here is one of the packages we use at my work place: https://github.com/funeralzone/valueobjects .
really helpful and friendly ValueObjects package.

There are a few suggestions I would make to your project however: try and not commit your IDE preference folder to git anymore (.idea), as it is not an actual contribution and it hardens collaboration while when practicing DDD you shouldn't need getters and setters in particular on your entities and your architecture should illustrate your bounded contexts first.

All of that, this is a nice first step into educating yourself and helping others grasp some of the concepts useful with DDD.

You can contact us on Discord.
[utopian-moderator]

Hello @decebal2dac

This is kinda workshop project with my team. We are also still DDD learners. Yeah, that .idea folder is missed. =/

Your comment and suggestions were highly appreciated.
I will share the getters and setters situation.

thanks.

Hey @decebal2dac, I just gave you a tip for your hard work on moderation. Upvote this comment to support the utopian moderators and increase your future rewards!

Hi! I am a robot. I just upvoted you! I found similar content that readers might be interested in:
https://airbrake.io/blog/software-design/domain-driven-design

I would be interested to see if you could further improve the current architecture to show a little bit more domain. One task that I always struggle within my projects as well.

This project will be a real project for who needs. So commits will continue, I will, of course, try to improve the architecture.

Thanks again.

Hey @bencagri I am @utopian-io. I have just upvoted you!

Achievements

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • Seems like you contribute quite often. AMAZING!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x