2

For example, I have a path on my site.

Let it be www.mysite.com/year/{year}/month/{month}/day/{day}

Could you clarify me, what is the best way to avoid getting of wrong arguments, such as year/3000/month/15/day/32 ?

Should I verify them inside the function called with the controller? Or is there a separated function or method to verify them, which wouldn't let invoke the controller function in case of wrong arguments?

1

2 Answers 2

2

Here my working example:

.routing.yml

forcontu_pages.calculator:
  path: '/forcontu/pages/calculator/{num1}/{num2}'
  defaults:
    _controller: '\Drupal\forcontu_pages\Controller\ForcontuPagesController::calculator'
    num1: 20
    num2: 10
    _title: 'Calculator Page'
  requirements:
    _permission: 'access calculator'
    _custom_access:  '\Drupal\forcontu_pages\Controller\ForcontuPagesController::access'

ForcontuPagesController.php

use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Access\AccessResult;
...
  public function access(AccountInterface $account, $num1, $num2) {
    // Check permissions and combine that with any custom access checking needed. Pass forward
    // parameters from the route and/or request as needed.
    return AccessResult::allowedIf($num1 > $num2);
  }
...

I just verify that $num1 is greater than $num2, but you can add your other params and your validations.

See that you can add more params:

The access method arguments are resolved similar to normal routing. The following arguments are optionally available; they will be populated if they are properly type hinted:

  • The slugs, like with normal routing.
  • \Symfony\Component\HttpFoundation\Request $request
  • \Symfony\Component\Routing\Route $route
  • \Drupal\Core\Routing\RouteMatch $route_match
  • \Drupal\Core\Session\AccountProxy $account

Read: What is a slug?

Reference: Access checking on routes

2
  • @4k4 This is the way that I found Commented Aug 23, 2017 at 20:19
  • Great answer, also nice reference for slug.
    – 4uk4
    Commented Aug 23, 2017 at 20:40
1

Depends on what is expected to happen if the parameters are wrong, if it should return a page not found you can define constraints using regular expressions to validate the parameters in the route definition:

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

https://www.drupal.org/docs/8/api/routing-system/parameters-values-validation-in-routes

10
  • Here is an example of validation using the regular expressions, but I need valudation using a function which returns true or false. Commented Aug 23, 2017 at 9:38
  • This is a custom access drupal.org/node/2122195, which gets the same route arguments as the controller.
    – 4uk4
    Commented Aug 23, 2017 at 10:11
  • @4k4 I can't get the point on how to use $request Commented Aug 23, 2017 at 18:24
  • @AdrianCidAlmaguer, the argument has to be type hinted public function access(Request $request) and the use statement use Symfony\Component\HttpFoundation\Request;.
    – 4uk4
    Commented Aug 23, 2017 at 18:41
  • @4k4 yes I tried this public function access(AccountInterface $account, Request $request) { but it doens't works, can you share a working example? Commented Aug 23, 2017 at 19:03

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.