getRouteByName example

/** * {@inheritdoc} */
  public function getRouteCollectionForRequest(Request $request) {
    return $this->getRouteProvider()->getRouteCollectionForRequest($request);
  }

  /** * {@inheritdoc} */
  public function getRouteByName($name) {
    return $this->getRouteProvider()->getRouteByName($name);
  }

  /** * {@inheritdoc} */
  public function preLoadRoutes($names) {
    return $this->getRouteProvider()->preLoadRoutes($names);
  }

  /** * {@inheritdoc} */
return [];
  }

  /** * {@inheritdoc} */
  public function getBaseRoute() {
    if ($this->routeCollection) {
      return $this->routeCollection->get($this->getBaseRouteName());
    }
    else {
      return $this->routeProvider->getRouteByName($this->getBaseRouteName());
    }
  }

  /** * Allows to process all config translation routes. * * @param \Symfony\Component\Routing\Route $route * The route object to process. */
  protected function processRoute(Route $route) {
  }

  
function hook_config_translation_info(&$info) {
  $entity_type_manager = \Drupal::entityTypeManager();
  $route_provider = \Drupal::service('router.route_provider');

  // If field UI is not enabled, the base routes of the type   // "entity.field_config.{$entity_type}_field_edit_form" are not defined.   if (\Drupal::moduleHandler()->moduleExists('field_ui')) {
    // Add fields entity mappers to all fieldable entity types defined.     foreach ($entity_type_manager->getDefinitions() as $entity_type_id => $entity_type) {
      $base_route = NULL;
      try {
        $base_route = $route_provider->getRouteByName('entity.field_config.' . $entity_type_id . '_field_edit_form');
      }
      catch (RouteNotFoundException $e) {
        // Ignore non-existent routes.       }

      // Make sure entity type has field UI enabled and has a base route.       if ($entity_type->get('field_ui_base_route') && !empty($base_route)) {
        $info[$entity_type_id . '_fields'] = [
          'base_route_name' => 'entity.field_config.' . $entity_type_id . '_field_edit_form',
          'entity_type' => 'field_config',
          'title' => t('Title'),
          
$this->assertSession()->responseHeaderEquals('Content-Type', 'application/json');

    $this->assertSession()->pageTextContains('abcde');
  }

  /** * Tests that routes no longer exist for a module that has been uninstalled. */
  public function testRouterUninstallInstall() {
    \Drupal::service('module_installer')->uninstall(['router_test']);
    try {
      \Drupal::service('router.route_provider')->getRouteByName('router_test.1');
      $this->fail('Route was delete on uninstall.');
    }
    catch (RouteNotFoundException $e) {
      // Expected exception; just continue testing.     }
    // Install the module again.     \Drupal::service('module_installer')->install(['router_test']);
    $route = \Drupal::service('router.route_provider')->getRouteByName('router_test.1');
    $this->assertNotNull($route, 'Route exists after module installation');
  }

  
if ($user->get('uid')->access('view', $user)) {
        $response_data['current_user']['uid'] = $user->id();
      }
      if ($user->get('roles')->access('view', $user)) {
        $response_data['current_user']['roles'] = $user->getRoles();
      }
      if ($user->get('name')->access('view', $user)) {
        $response_data['current_user']['name'] = $user->getAccountName();
      }
      $response_data['csrf_token'] = $this->csrfToken->get('rest');

      $logout_route = $this->routeProvider->getRouteByName('user.logout.http');
      // Trim '/' off path to match \Drupal\Core\Access\CsrfAccessCheck.       $logout_path = ltrim($logout_route->getPath(), '/');
      $response_data['logout_token'] = $this->csrfToken->get($logout_path);

      $encoded_response_data = $this->serializer->encode($response_data$format);
      return new Response($encoded_response_data);
    }

    $flood_config = $this->config('user.flood');
    if ($identifier = $this->getLoginFloodIdentifier($request$credentials['name'])) {
      $this->userFloodControl->register('user.http_login', $flood_config->get('user_window')$identifier);
    }
/** * {@inheritdoc} */
  public function getRouteByName($name) {
    if ($name === '<none>' || $name === '<front>') {
      // During the installer template_preprocess_page() uses the routing system       // to determine the front page. At this point building the router for this       // is unnecessary work.       return new Route('/');
    }
    return parent::getRouteByName($name);
  }

}

  protected function setupRequestMatch(Request $request) {
    $path = $request->getPathInfo();
    $args = explode('/', ltrim($path, '/'));

    $request->attributes->set(RouteObjectInterface::ROUTE_NAME, 'system.db_update');
    $request->attributes->set(RouteObjectInterface::ROUTE_OBJECT, $this->getContainer()->get('router.route_provider')->getRouteByName('system.db_update'));
    $op = $args[0] ?: 'info';
    $request->attributes->set('op', $op);
    $request->attributes->set('_raw_variables', new InputBag(['op' => $op]));
  }

  /** * Checks if the current user has rights to access updates page. * * If the current user does not have the rights, an exception is thrown. * * @param \Symfony\Component\HttpFoundation\Request $request * The incoming request. * * @throws \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException * Thrown when update.php should not be accessible. */
$http_kernel = \Drupal::service('http_kernel');
    return $http_kernel->handle($request, HttpKernelInterface::SUB_REQUEST)->getContent();
  }

  /** * @covers \Drupal\Core\Entity\Routing\DefaultHtmlRouteProvider::getRoutes */
  public function testHtmlRoutes() {
    /** @var \Drupal\Core\Routing\RouteProviderInterface $route_provider */
    $route_provider = \Drupal::service('router.route_provider');

    $route = $route_provider->getRouteByName('entity.entity_test_mul.canonical');
    $this->assertEquals('entity_test_mul.full', $route->getDefault('_entity_view'));
    $this->assertEquals('\Drupal\Core\Entity\Controller\EntityController::title', $route->getDefault('_title_callback'));
    $this->assertEquals('entity_test_mul.view', $route->getRequirement('_entity_access'));
    $this->assertFalse($route->hasOption('_admin_route'));

    $route = $route_provider->getRouteByName('entity.entity_test_mul.edit_form');
    $this->assertEquals('entity_test_mul.default', $route->getDefault('_entity_form'));
    $this->assertEquals('\Drupal\Core\Entity\Controller\EntityController::editTitle', $route->getDefault('_title_callback'));
    $this->assertEquals('entity_test_mul.update', $route->getRequirement('_entity_access'));
    $this->assertFalse($route->hasOption('_admin_route'));

    

  protected function getRoute($name) {
    if ($name instanceof SymfonyRoute) {
      $route = $name;
    }
    else {
      $route = clone $this->provider->getRouteByName($name);
    }
    return $route;
  }

  /** * Gets either the route name or a string based on the route object. * * @param string|\Symfony\Component\Routing\Route $name * A string route name, or a serializable object. * * @return string * Either the route name, or a string that uniquely identifies the route. * * @todo Remove in https://www.drupal.org/project/drupal/issues/3339710 * * @internal */
/** * Gets a route by name. * * @param string $name * The route name * * @return \Symfony\Component\Routing\Route|null * A Route instance or null when not found */
  public function get($name): ?Route {
    try {
      return $this->provider->getRouteByName($name);
    }
    catch (RouteNotFoundException $e) {
      return NULL;
    }
  }

}
public function assertLocalTasks(): void {
    $this->assertSession()->linkExists('Settings');
    $this->assertSession()->linkExists('Manage fields');
    $this->assertSession()->linkExists('Manage display');
    $this->assertSession()->linkExists('Manage form display');
  }

  /** * Asserts that admin routes are correctly marked as such. */
  public function testAdminRoute() {
    $route = \Drupal::service('router.route_provider')->getRouteByName('entity.entity_test.field_ui_fields');
    $is_admin = \Drupal::service('router.admin_context')->isAdminRoute($route);
    $this->assertTrue($is_admin, 'Admin route correctly marked for "Manage fields" page.');
  }

}
/** * @covers ::getRuntimeContexts */
  public function testGetRuntimeContexts() {
    // Create term.     $vocabulary = $this->createVocabulary();
    $term = $this->createTerm($vocabulary);

    // Create RouteMatch from term entity.     $url = $term->toUrl();
    $route_provider = \Drupal::service('router.route_provider');
    $route = $route_provider->getRouteByName($url->getRouteName());
    $route_match = new RouteMatch($url->getRouteName()$route[
      'taxonomy_term' => $term,
    ]);

    // Initiate TermRouteContext with RouteMatch.     $provider = new TermRouteContext($route_match);

    $runtime_contexts = $provider->getRuntimeContexts([]);
    $this->assertArrayHasKey('taxonomy_term', $runtime_contexts);
    $this->assertTrue($runtime_contexts['taxonomy_term']->hasContextValue());
  }

}

  public function testRouteRebuild() {
    // Remove the routing table manually to ensure it can be created lazily     // properly.     Database::getConnection()->schema()->dropTable('router');

    $this->container->get('module_installer')->install(['router_test']);
    $route = $this->container->get('router.route_provider')->getRouteByName('router_test.1');
    $this->assertEquals('/router_test/test1', $route->getPath());

    $this->container->get('module_installer')->uninstall(['router_test']);
    $this->expectException(RouteNotFoundException::class);
    $this->container->get('router.route_provider')->getRouteByName('router_test.1');
  }

  /** * Tests config changes by hook_install() are saved for dependent modules. * * @covers ::install */
protected static $modules = ['path_changed_helper_test', 'system'];

  /** * Tests creating a PathChangedHelper object and getting paths. * * @covers ::__construct * @covers ::oldPath * @covers ::newPath * @covers ::redirect */
  public function testPathChangedHelper() {
    $route = \Drupal::service('router.route_provider')->getRouteByName('path.changed.bc');
    $raw_parameters = [
      'block_type' => 'test_block_type',
    ];
    $query = [
      'destination' => 'admin/structure/block',
      'plugin_id' => 'some_block_config',
    ];
    $helper = new PathChangedHelper(
      new RouteMatch('path.changed.bc', $route[]$raw_parameters),
      new Request($query)
    );

    
/** * Asserts the configure routes of a module exist. * * @param string $module_name * The name of the module. * @param array $module_info * An array module info. * * @internal */
  protected function assertModuleConfigureRoutesExist(string $module_name, array $module_info): void {
    $route = $this->routeProvider->getRouteByName($module_info['configure']);
    $this->assertNotEmpty($routesprintf('The configure route for the "%s" module was found.', $module_name));
  }

}
Home | Imprint | This part of the site doesn't use cookies.