php - Symfony 2.5 & twig: how to clean up this code? -
i'm new symfony , utilize best-practices possible. code below works feels kinda dirty.
i worry, if write much code here. maybe miss symfony-background-magic don't know yet. details below.
what alter (why)? appreciate every advice become improve developer. in advance!
routing.yml:
items_edit: path: /items/edit/{id} defaults: { _controller: mybundle:items:edit, id: null } # null = if not set? requirements: id: \d+
itemcontroller.php:
do have pass $item
or info anyhow else gettable via twig?
public function editaction($id, request $request) { $em = $this->getdoctrine()->getmanager(); $repo = $em->getrepository('itemsrepo'); $item = $repo->find($id); $form = $this->createform(new itemformtype(), $item); if ($request->ismethod('post')) { $form->handlerequest($request); if ($form->isvalid()) { $em->persist($item); $em->flush(); $this->get('session')->getflashbag()->add('info', 'saved.'); homecoming $this->redirect($this->generateurl('items_list')); } } homecoming $this->render('edit.html.twig', array( 'form' => $form->createview(), 'item' => $item // !!! )); }
edit.html.twig:
do have add together {id: item.id}
here?
{% block content %} <form action="{{ path('items_edit', {id: item.id}) }}" method="post" {{ form_enctype(form) }}> {# ... custom stuff ... #} {{ form_end(form) }} {% endblock %}
id: null
\d+
requirement in route useless, because of you're editing existing entity, improve remove it; $em->persist($item);
unnecessary, because of you've persisted , flushed on creation, time don't need persist again, flush it. abount passing $item
form, if want show info user, "editing item title of some_title", or if there image field want show thumbnail .., can pass , retreive field value, it's u you.. additional note, /items/edit/{id}
not pretty, it's done /items/{id}/edit
, /items/{id}/delete
...
php symfony2 doctrine twig yaml
No comments:
Post a Comment