Skip to main content
added 583 characters in body
Source Link
sonfd
  • 8.1k
  • 1
  • 20
  • 40

I think you may be unsetting some things in your alterRoutes method doing it the way you are. (However, I can't tell if or why that's happening after looking at Route::addDefaults().)

I'm doing the same thing for a media entity's route. My alterRoutes method is a little different - I'm using Route::setDefault().

public function alterRoutes(RouteCollection $collection) {
  if ($route = $collection->get('entity.media.canonical')) {
    $route->setDefault('_controller', '\Drupal\my_module\Controller\MyController::view');
  }
}

Additionally, the variable names in your controller methods are important - they must match the variable name from the route definition. See the documentation for Using parameters in routes

In most PHP code the name of the variable does not matter but here it does: the name of the method argument must match the slug. Conversely, if the method argument name matches the name of the slug, the parameter will be passed in, irrespective of the order of arguments.

I think you may be unsetting some things in your alterRoutes method doing it the way you are. (However, I can't tell if or why that's happening after looking at Route::addDefaults().)

I'm doing the same thing for a media entity's route. My alterRoutes method is a little different - I'm using Route::setDefault().

public function alterRoutes(RouteCollection $collection) {
  if ($route = $collection->get('entity.media.canonical')) {
    $route->setDefault('_controller', '\Drupal\my_module\Controller\MyController::view');
  }
}

I think you may be unsetting some things in your alterRoutes method doing it the way you are. (However, I can't tell if or why that's happening after looking at Route::addDefaults().)

I'm doing the same thing for a media entity's route. My alterRoutes method is a little different - I'm using Route::setDefault().

public function alterRoutes(RouteCollection $collection) {
  if ($route = $collection->get('entity.media.canonical')) {
    $route->setDefault('_controller', '\Drupal\my_module\Controller\MyController::view');
  }
}

Additionally, the variable names in your controller methods are important - they must match the variable name from the route definition. See the documentation for Using parameters in routes

In most PHP code the name of the variable does not matter but here it does: the name of the method argument must match the slug. Conversely, if the method argument name matches the name of the slug, the parameter will be passed in, irrespective of the order of arguments.

added 30 characters in body
Source Link
sonfd
  • 8.1k
  • 1
  • 20
  • 40

I think you may be unsetting some things in your alterRoutes method doing it the way you are. (However, I can't tell if or why that's happening after looking at Route::addDefaults().)

I'm doing the same thing for a media entity's route. My alterRoutes method is a little different - I'm using the Route::setDefault() method:.

public function alterRoutes(RouteCollection $collection) {
  if ($route = $collection->get('entity.media.canonical')) {
    $route->setDefault('_controller', '\Drupal\my_module\Controller\MyController::view');
  }
}

I think you may be unsetting the other defaults by passing the array like you are, but I'm not positive after looking at Route::addDefaults().

I'm doing the same thing for a media entity's route. My alterRoutes method is a little different - I'm using the Route::setDefault() method:

public function alterRoutes(RouteCollection $collection) {
  if ($route = $collection->get('entity.media.canonical')) {
    $route->setDefault('_controller', '\Drupal\my_module\Controller\MyController::view');
  }
}

I think you may be unsetting the other defaults by passing the array like you are, but I'm not positive after looking at Route::addDefaults().

I think you may be unsetting some things in your alterRoutes method doing it the way you are. (However, I can't tell if or why that's happening after looking at Route::addDefaults().)

I'm doing the same thing for a media entity's route. My alterRoutes method is a little different - I'm using Route::setDefault().

public function alterRoutes(RouteCollection $collection) {
  if ($route = $collection->get('entity.media.canonical')) {
    $route->setDefault('_controller', '\Drupal\my_module\Controller\MyController::view');
  }
}
Source Link
sonfd
  • 8.1k
  • 1
  • 20
  • 40

I'm doing the same thing for a media entity's route. My alterRoutes method is a little different - I'm using the Route::setDefault() method:

public function alterRoutes(RouteCollection $collection) {
  if ($route = $collection->get('entity.media.canonical')) {
    $route->setDefault('_controller', '\Drupal\my_module\Controller\MyController::view');
  }
}

I think you may be unsetting the other defaults by passing the array like you are, but I'm not positive after looking at Route::addDefaults().