6

With GET method we can do something like this

example.user
  path: 'example/{user}'
  defaults: 
    _controller: '\Drupal\example\Controller\ExampleController::content' 
  requirements: 
    _permission: 'access content' 
    user: ^[a-zA-Z0-9_]+

so it sets user param as required and also limits its format.

But I'd like to do something similar with POST method. Tried this, but not working:

example.user
  path: 'example'
  methods:  [POST]
  defaults: 
    _controller: '\Drupal\example\Controller\ExampleController::content' 
  requirements: 
    _permission: 'access content' 
    user: ^[a-zA-Z0-9_]+

EDIT:

Sure I can set some logic inside controller like this, but it doesn't feel right:

public function store(Request $request) {
   $params = json_decode($request->getContent(), TRUE);
   //I'm trying to avoid using these conditions inside controller methods
   if(empty($params['user'])){
      //return error response
   }else{
     if (!preg_match('/^[a-zA-Z0-9_]+/', $params['user'])) {
       //return error response
     }
   }
  //return success response
}

So what's drupal way to do it?

  • 1
    The Drupal way to do a restful interface (noticed "rest" tag in question) is to use RestResource (see EntityResource::post). In other words, use Serializer component to denormalize. If this isn't rest, then maybe we should remove the rest tag? – mradcliffe Aug 29 '16 at 15:24
  • @mradcliffe Yeah thanks for heads up about the tag. Now when I read it few times again I think it doesn't fit. – Trupal Aug 29 '16 at 15:51
0

I did something like this before ( take a look at helper link ),it not required to tell router about type

just post your data and in your controller get them with

$value = \Drupal::request()->request->get('your_posted_variable'); 

Also you can get all $_POST variables with

  \Drupal::request()->request->all(); // equal to $_POST
  • It's true what you are saying, thanks, but I know how to get params. I'm just trying to set them as required. Please see my update, I'm sorry I wasn't more clear. – Trupal Aug 29 '16 at 12:49
0

You don't need to set anything on the route if you have a default parameter for POST you can add the value directly in the controller:

\Drupal\example\Controller\ExampleController::content

Then you check if you have receive some value in the POST method and if you have values replace the default value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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