-->

Silex - get right condionals in finish middleware

2019-09-14 01:57发布

问题:

I want to create a pdf file out of some route-dependant data

{http://example.com/products/123/?action=update}

$app->finish(function (Request $request, Response $response) {

    // Make a pdf file, only if:
    // - the route is under  /products/
    // - the action is update
    // - the subsequent ProductType form isSubmitted() and isValid()
    // - the 'submit' button on the ProductType form isClicked()

});

As a normal form submission process I have:

public function update(Application $app, Request $request)
{
    $form = $app['form.factory']->create(ProductType::class, $product);

    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {

        if (!$form->get('submit')->isClicked()) {
            return $app->redirect('somewhere');
        }

        $product = $form->getData();

        $app['em']->persist($product);
        $app['em']->flush();

        return $app->redirect('product_page');
    }

    return $app['twig']->render('products/update.html.twig', array(
        'form' => $form->createView(),
    ));
}

Questions:

  1. Should I duplicate all of the conditionals in finish middleware?
  2. How to access the Product entity in finish middleware?

Consider having multiple resource types like Products, Services, Users, ...

标签: symfony silex