-1

So currently i am passing some params to a form via the route path as so:

my_module.action:
  path: '/status/{action}/{id}'
  defaults:
    _form: '\Drupal\my_module\Form\MyForm'
  requirements:
    _permission: 'access content'

And in my form:

public function buildForm(array $form, FormStateInterface $form_state, $action = null, $id = null) {}

And it works as intended, the form receives the two params. But because this way the params are exposed in the url and every user can see them. Is there a way to pass params to the router but not expose them in the url? [passing them in Session is not really best practice, so i'd like to avid it.]

5
  • What about posting these parameters ? and get these in a Request object ? Commented Dec 7, 2017 at 12:23
  • @Oulalahakabu Can you give me an example of what you mean?
    – lordZ3d
    Commented Dec 7, 2017 at 12:32
  • 2
    passing them in Session is not really best practice - that's sort of correct, but only because best practice is typically to pass parameters in the URL or request body. If you don't want to use best practice methods, eliminating non best practice methods is going to leave you without any options :) Bottom line, if you want to get data from one page to another, but not allow the user to have access to that data, you need the session. Or two-way encryption. If you're doing this as any sort of security measure it'd be best not to bother, security through obscurity is all but pointless
    – Clive
    Commented Dec 7, 2017 at 12:37
  • In which context/why don't you need params in url in this form ? I meant getting these params in post variables (instead of in query), and use hidden fields. If this is for security reason, that's not more secure... Commented Dec 7, 2017 at 12:38
  • @Clive I ended up using encoding the params and passing them via the Session, thanks.
    – lordZ3d
    Commented Dec 8, 2017 at 9:12

1 Answer 1

0

I do not know why you can not use the parameters of the url.

One way to do what you want is:

1) Create a new content type called, for example: "routing settings".

2) create two fields:

  • field_id of numerical type.
  • field_action of type text.

3) create several nodes with the corresponding actions and id.

4) use the nid of your node as a parameter

file: .routing.yml

my_module.action:
  path: '/status/{node}'
  defaults:
    _form: '\Drupal\my_module\Form\MyForm'
  requirements:
    _permission: 'access content'

your file form:

use Drupal\node\NodeInterface;
...

public function buildForm(array $form, FormStateInterface $form_state, NodeInterface $node) {

$id= $node->get("field_id")->value;
$action= $node->get("field_action")->value;

//here you put your code

}

....

1
  • Well this could work, but it seems at overkill just to pass two strings. Essentially creating a CT and having to read/write from the db is a bit to much imho. But i do appreciate the answer.
    – lordZ3d
    Commented Dec 8, 2017 at 9:11

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.