PHP-View icon indicating copy to clipboard operation
PHP-View copied to clipboard

Can I access the slim properties from the templates?

Open NeftaliAcosta opened this issue 5 years ago • 2 comments

Through my API I already have a route that receives parameters and creates users, this works perfectly for me. However, I would like to implement an HTML form for the API to provide API clients, the form must be able to access the method (to the database model), so that from the same form you can create the users.

I already have the simple form in HTML.

The question is: How do I access the slim models, so that, once the values of the form have been obtained, I will be able to send them to the model to create the user.

My route to create users:

$this->post('crear', function ($req, $res, $args) {
        $r = CandidatoCrearValidation::validate($req->getParsedBody());
        if(!$r->response){
          return $res->withHeader('Content-type', 'application/json')
                    ->withStatus(422)
                    ->write(json_encode($r->setResponse(false,"Campos no validados.")));
        }else{
          return $res->withHeader('Content-type', 'application/json')
                  ->write(
                    json_encode($this->model->candidato->crear($req->getParsedBody()))
                  ); 
        }
      });

My route to show the form:

$this->get('crear/form', function ($req, $res, $args) {
      return $this->renderer->render($res, "form-signin.php", $args);
    });

Ambos están dentro del mismo grupo de la ruta de slim. image

NeftaliAcosta avatar Mar 08 '20 20:03 NeftaliAcosta

+1

stakahashi avatar Jul 26 '20 15:07 stakahashi

the form must be able to access the method (to the database model),

This would break the basic MVC principles, because the "View" layer should not fetch data from somewhere. All the the data should be passed to the View renderer instead.

How do I access the slim models, so that, once the values of the form have been obtained, I will be able to send them to the model to create the user.

You can handle all these operations in the action handler or better in a use case specific (Application) Service.

odan avatar Feb 02 '21 10:02 odan