Problem/Motivation

We kind of discourage people from using the Request object directly, we don't make it easy to get the value into the controllers.

Proposed resolution

Add the possibility to magically get the route match object.

Remaining tasks

User interface changes

API changes

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

dawehner’s picture

Status: Active » Needs review
FileSize
6.73 KB

Here is a patch.

Status: Needs review » Needs work

The last submitted patch, 1: route_match-2330363-1.patch, failed testing.

dawehner’s picture

Status: Needs work » Needs review
FileSize
11.15 KB

Fixed.

Crell’s picture

+++ b/core/lib/Drupal/Core/Controller/ControllerResolver.php
@@ -136,7 +137,29 @@ protected function createController($controller) {
+      } elseif ($param->getClass() && ($param->getClass()->name == 'Drupal\Core\Routing\RouteMatchInterface' || is_subclass_of($param->getClass()->name, 'Drupal\Core\Routing\RouteMatchInterface'))) {
+        $arguments[] = RouteMatch::createFromRequest($request);

This is the only part being added, correct?

Also, this block should be using Drupal coding standards if it's in one of our classes.

How much of a use case does this have? The most common thing to inject is a parameter, which is already handled. The next most common would be query parameters, but I don't believe that's handled by RouteMatch, is it?

dawehner’s picture

How much of a use case does this have? The most common thing to inject is a parameter, which is already handled. The next most common would be query parameters, but I don't believe that's handled by RouteMatch, is it?

Yeah the next usecase is to fetch the raw unconverted values. The query parameters aren't part of the route match.

Crell’s picture

How often are we doing that? The patch doesn't show it as a common thing here.

(Sorry, just trying to avoid loading more features than we need to into something that we could very easily add post-8.0.0.)

dawehner’s picture

FileSize
14.19 KB
3.04 KB

mh, yeah this was primarily to scratch my own itches.

Status: Needs review » Needs work

The last submitted patch, 7: route_match-2330363-7.patch, failed testing.

tim.plunkett’s picture

There are still places we use the request to get the _raw_variables, they could use this.
Bumping to critical as its part of a critical meta.

dawehner’s picture

Status: Needs review » Needs work

The last submitted patch, 10: controller-2330363-10.patch, failed testing.

dawehner’s picture

Status: Needs work » Needs review
FileSize
19.72 KB
4.63 KB

Fixes the failures.

tim.plunkett’s picture

Title: Exhance the controller resolver to get a route match class » Enhance the controller resolver to get a route match class
Status: Needs review » Reviewed & tested by the community
+++ b/core/modules/rest/src/RequestHandler.php
@@ -87,7 +90,7 @@ public function handle(Request $request) {
-    $format = $request->attributes->get(RouteObjectInterface::ROUTE_OBJECT)->getRequirement('_format') ?: 'json';
+    $format = $route_match->getRouteObject()->getRequirement('_format') ?: 'json';

Much nicer.

I think this is a welcome improvement. RTBC if it still passes.

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed 86922ac and pushed to 8.0.x. Thanks!

diff --git a/core/lib/Drupal/Core/Controller/DialogController.php b/core/lib/Drupal/Core/Controller/DialogController.php
index 575b41a..48279e5 100644
--- a/core/lib/Drupal/Core/Controller/DialogController.php
+++ b/core/lib/Drupal/Core/Controller/DialogController.php
@@ -10,9 +10,7 @@
 use Drupal\Core\Ajax\AjaxResponse;
 use Drupal\Core\Ajax\OpenDialogCommand;
 use Drupal\Core\Page\HtmlPage;
-use Drupal\Core\Routing\RouteMatch;
 use Drupal\Core\Routing\RouteMatchInterface;
-use Symfony\Cmf\Component\Routing\RouteObjectInterface;
 use Symfony\Component\HttpFoundation\Request;
 use Symfony\Component\HttpFoundation\Response;
 
@@ -53,6 +51,8 @@ public function __construct(ControllerResolverInterface $controller_resolver, Ti
    *
    * @param \Symfony\Component\HttpFoundation\Request $request
    *   The request object.
+   * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
+   *   The route match.
    * @param mixed $_content
    *   A controller definition string, or a callable object/closure.
    *
diff --git a/core/modules/rest/src/RequestHandler.php b/core/modules/rest/src/RequestHandler.php
index 505337c..c31600d 100644
--- a/core/modules/rest/src/RequestHandler.php
+++ b/core/modules/rest/src/RequestHandler.php
@@ -8,7 +8,6 @@
 namespace Drupal\rest;
 
 use Drupal\Core\Routing\RouteMatchInterface;
-use Symfony\Cmf\Component\Routing\RouteObjectInterface;
 use Symfony\Component\DependencyInjection\ContainerAwareInterface;
 use Symfony\Component\DependencyInjection\ContainerAwareTrait;
 use Symfony\Component\HttpFoundation\Request;
diff --git a/core/modules/user/src/Controller/UserController.php b/core/modules/user/src/Controller/UserController.php
index 739a365..be1b03d 100644
--- a/core/modules/user/src/Controller/UserController.php
+++ b/core/modules/user/src/Controller/UserController.php
@@ -10,7 +10,6 @@
 use Drupal\Component\Utility\Xss;
 use Drupal\Core\Controller\ControllerBase;
 use Drupal\user\UserInterface;
-use Symfony\Component\HttpFoundation\Request;
 use Symfony\Component\DependencyInjection\ContainerInterface;
 use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
 use Drupal\Core\Datetime\DateFormatter;
@@ -132,9 +131,6 @@ public function resetPass($uid, $timestamp, $hash) {
    * Displays user profile if user is logged in, or login form for anonymous
    * users.
    *
-   * @param \Symfony\Component\HttpFoundation\Request $request
-   *   The current request.
-   *
    * @return \Symfony\Component\HttpFoundation\RedirectResponse|array
    *   Returns either a redirect to the user page or the render
    *   array of the login form.
diff --git a/core/modules/views/src/Routing/ViewPageController.php b/core/modules/views/src/Routing/ViewPageController.php
index 2eec5b5..0bfa9a1 100644
--- a/core/modules/views/src/Routing/ViewPageController.php
+++ b/core/modules/views/src/Routing/ViewPageController.php
@@ -12,7 +12,6 @@
 use Drupal\Core\Entity\EntityStorageInterface;
 use Drupal\Core\Routing\RouteMatchInterface;
 use Drupal\views\ViewExecutableFactory;
-use Symfony\Cmf\Component\Routing\RouteObjectInterface;
 use Symfony\Component\DependencyInjection\ContainerInterface;
 use Symfony\Component\HttpFoundation\Request;
 use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

Fixed on commit

  • alexpott committed 86922ac on 8.0.x
    Issue #2330363 by dawehner: Enhance the controller resolver to get a...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.