Indicate form which method of the class to use

0

I have a form that will display the fields to be inserted in the bd, so far so good, the problem lies in the following:

I have created a class (this file will receive several distinct requests) with methods add , edit , delete , find , etc. I need to send my form to the file that will receive the request and find out which request I am sending ( add , edit , delete , etc.) and forward to the right method to handle the functionality , does anyone know how I can tell the form to execute a certain method of my class?

Edit - Added code

file: ServicosController.php

class ServicosController extends AppController {

public function add() {
    $servico = $_POST['servico'];

    if (!$servico) {
        echo 'Serviço inválido.';           
    } else {
        $database->insert('servicos', [
            'title' => $servico['title'],
            'value' => $servico['value']
            ]);
    }
}

}

Since the submission form has no particularity for the moment, it's only 2 inputs msm ...

    
asked by anonymous 22.09.2015 / 19:51

1 answer

0

You can use the Symfony Form Component to perform this task. With this component, you can use an object to render a form; just as it is possible to map values submitted through a form to an object.

See an example that they provide:

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;

$form = $formFactory->createBuilder()
    ->add('task', 'text')
    ->add('dueDate', 'date')
    ->getForm();

$request = Request::createFromGlobals();

$form->handleRequest($request);

if ($form->isValid()) {
    $data = $form->getData();

    // ... perform some action, such as saving the data to the database

    $response = new RedirectResponse('/task/success');
    $response->prepare($request);

    return $response->send();
}
    
22.09.2015 / 20:07