1

I am developping a Symfony3 application that will have a web interface but a Mobile Interface too (probably Ionic). So far I have created a REST-API using FOSRestBundle and NelmioApiDocBundle (amazing stuff btw.) and I can view/create/delete entities with the NelmioApiDocBundle Sandbox feature. With other words I have created the Controllers that implement a CRUD but also include transactions and event handling.

Now I'd like to also provide a web interface for some of those entities, so I created corresponding controllers and find myself duplicating createForm/submit/isValid etc. It would be nice if I could just delegate the form handling (POSTs) to the API controllers which already handle persistence and logic, but I'm running into quicksand.

Has anybody tried something like this or has a suggestion how to tackle such a situation? Any answer is hightly appreciated. Jorgo

1 Answer 1

0

Use Command and Command Bus pattern instead. 1, 2, 3. There are a number nice Symfony bundles, e.g. this one.

So possible approach is:

  1. Create the command (just dumb object like DTO with setters and getters, without any behavior)
  2. Create handler for the command and put there your logic and so on.
  3. Use that command as model in your form.
  4. Use that command as input data model in your REST API. Just use request body converters to transform your input JSON/XML into command.
  5. Validate it with validation middleware in your command bus library implementation (also possible to validate it directly if you use forms).
Sign up to request clarification or add additional context in comments.

2 Comments

Wow! Thanks for the quick answer. Will check out!
This is fantatastic. I asked for a solution to a problem and you gave me not just that, but the insight into being able to create an infrastructure that will elevate maintainability of the project. What I'm still struggling, with is how to use a command as form model. How do Request, Form and Entity play together? The request body converter would have to be a custome one. Right? Where does form handling happen and what properties does the Command have? How can the form submission access the data? Could you drop me a few foo-code lines that would demonstrate how to go about this?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.