本文整理汇总了PHP中Symfony\Component\Routing\Route类的典型用法代码示例。如果您正苦于以下问题:PHP Route类的具体用法?PHP Route怎么用?PHP Route使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Route类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: configureRoute
protected function configureRoute(\Symfony\Component\Routing\Route $route, \ReflectionClass $class, \ReflectionMethod $method, $annot)
{
// defines the controller
$route->setDefault('_controller', $class->getName() . '::' . $method->getName());
// verify the other callbacks
$options = $annot->getOptions();
foreach ($options as $prop => &$values) {
if (!in_array($prop, array('_after_middlewares', '_before_middlewares', '_converters'))) {
continue;
}
if (empty($values)) {
continue;
}
foreach ($values as &$value) {
if (is_string($value) && $class->hasMethod($value)) {
// call static method from class
$value = array($class->getName(), $value);
}
}
unset($value);
// clear reference
}
unset($values);
$route->setOptions($options);
}
示例2: access
/**
* Checks translation access for the entity and operation on the given route.
*
* @param \Symfony\Component\Routing\Route $route
* The route to check against.
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
* The parametrized route.
* @param \Drupal\Core\Session\AccountInterface $account
* The currently logged in account.
* @param string $source
* (optional) For a create operation, the language code of the source.
* @param string $target
* (optional) For a create operation, the language code of the translation.
* @param string $language
* (optional) For an update or delete operation, the language code of the
* translation being updated or deleted.
* @param string $entity_type_id
* (optional) The entity type ID.
*
* @return \Drupal\Core\Access\AccessResultInterface
* The access result.
*/
public function access(Route $route, RouteMatchInterface $route_match, AccountInterface $account, $source = NULL, $target = NULL, $language = NULL, $entity_type_id = NULL)
{
/* @var \Drupal\Core\Entity\ContentEntityInterface $entity */
if ($entity = $route_match->getParameter($entity_type_id)) {
if ($account->hasPermission('translate any entity')) {
return AccessResult::allowed()->cachePerRole();
}
$operation = $route->getRequirement('_access_content_translation_manage');
/* @var \Drupal\content_translation\ContentTranslationHandlerInterface $handler */
$handler = $this->entityManager->getHandler($entity->getEntityTypeId(), 'translation');
// Load translation.
$translations = $entity->getTranslationLanguages();
$languages = $this->languageManager->getLanguages();
switch ($operation) {
case 'create':
$source_language = $this->languageManager->getLanguage($source) ?: $entity->language();
$target_language = $this->languageManager->getLanguage($target) ?: $this->languageManager->getCurrentLanguage(LanguageInterface::TYPE_CONTENT);
$is_new_translation = $source_language->getId() != $target_language->getId() && isset($languages[$source_language->getId()]) && isset($languages[$target_language->getId()]) && !isset($translations[$target_language->getId()]);
return AccessResult::allowedIf($is_new_translation)->cachePerRole()->cacheUntilEntityChanges($entity)->andIf($handler->getTranslationAccess($entity, $operation));
case 'update':
case 'delete':
$language = $this->languageManager->getLanguage($language) ?: $this->languageManager->getCurrentLanguage(LanguageInterface::TYPE_CONTENT);
$has_translation = isset($languages[$language->getId()]) && $language->getId() != $entity->getUntranslated()->language()->getId() && isset($translations[$language->getId()]);
return AccessResult::allowedIf($has_translation)->cachePerRole()->cacheUntilEntityChanges($entity)->andIf($handler->getTranslationAccess($entity, $operation));
}
}
// No opinion.
return AccessResult::neutral();
}
示例3: access
/**
* Checks access for the account and route using the custom access checker.
*
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
* The route match object to be checked.
* @param \Drupal\Core\Session\AccountInterface $account
* The account being checked.
*
* @return \Drupal\Core\Access\AccessResultInterface
* The access result.
*/
public function access(Route $route, RouteMatchInterface $route_match, AccountInterface $account)
{
$callable = $this->controllerResolver->getControllerFromDefinition($route->getRequirement('_custom_access'));
$arguments_resolver = $this->argumentsResolverFactory->getArgumentsResolver($route_match, $account);
$arguments = $arguments_resolver->getArguments($callable);
return call_user_func_array($callable, $arguments);
}
示例4: processMethod
/**
* @param Method $method
* @param string $endpoint
*
* @return RpcApiDoc
*/
protected function processMethod(Method $method, $endpoint)
{
/** @var string[] $views */
$views = $method->getContext();
if ($method->includeDefaultContext()) {
$views[] = 'Default';
}
$views[] = 'default';
$request = new Request($method, [], new ParameterBag(['_controller' => $method->getController()]));
/** @var array $controller */
$controller = $this->resolver->getController($request);
$refl = new \ReflectionMethod($controller[0], $controller[1]);
/** @var RpcApiDoc $methodDoc */
$methodDoc = $this->reader->getMethodAnnotation($refl, RpcApiDoc::class);
if (null === $methodDoc) {
$methodDoc = new RpcApiDoc(['resource' => $endpoint]);
}
$methodDoc = clone $methodDoc;
$methodDoc->setEndpoint($endpoint);
$methodDoc->setRpcMethod($method);
if (null === $methodDoc->getSection()) {
$methodDoc->setSection($endpoint);
}
foreach ($views as $view) {
$methodDoc->addView($view);
}
$route = new Route($endpoint);
$route->setMethods([$endpoint]);
$route->setDefault('_controller', get_class($controller[0]) . '::' . $controller[1]);
$methodDoc->setRoute($route);
return $methodDoc;
}
示例5: access
/**
* Checks access to create an entity of any bundle for the given route.
*
* @param \Symfony\Component\Routing\Route $route
* The route to check against.
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
* The parameterized route.
* @param \Drupal\Core\Session\AccountInterface $account
* The currently logged in account.
*
* @return \Drupal\Core\Access\AccessResultInterface
* The access result.
*/
public function access(Route $route, RouteMatchInterface $route_match, AccountInterface $account)
{
$entity_type_id = $route->getRequirement($this->requirementsKey);
$entity_type = $this->entityTypeManager->getDefinition($entity_type_id);
$access_control_handler = $this->entityTypeManager->getAccessControlHandler($entity_type_id);
// In case there is no "bundle" entity key, check create access with no
// bundle specified.
if (!$entity_type->hasKey('bundle')) {
return $access_control_handler->createAccess(NULL, $account, [], TRUE);
}
$access = AccessResult::neutral();
$bundles = array_keys($this->entityTypeBundleInfo->getBundleInfo($entity_type_id));
// Include list cache tag as access might change if more bundles are added.
if ($entity_type->getBundleEntityType()) {
$access->addCacheTags($this->entityTypeManager->getDefinition($entity_type->getBundleEntityType())->getListCacheTags());
// Check if the user is allowed to create new bundles. If so, allow
// access, so the add page can show a link to create one.
// @see \Drupal\Core\Entity\Controller\EntityController::addPage()
$bundle_access_control_handler = $this->entityTypeManager->getAccessControlHandler($entity_type->getBundleEntityType());
$access = $access->orIf($bundle_access_control_handler->createAccess(NULL, $account, [], TRUE));
if ($access->isAllowed()) {
return $access;
}
}
// Check whether an entity of any bundle may be created.
foreach ($bundles as $bundle) {
$access = $access->orIf($access_control_handler->createAccess($bundle, $account, [], TRUE));
// In case there is a least one bundle user can create entities for,
// access is allowed.
if ($access->isAllowed()) {
break;
}
}
return $access;
}
示例6: __construct
/**
* Constructs a Route object.
*/
public function __construct($name, Route $route, RouteProviderInterface $route_provider)
{
$this->name = $name;
$this->route = $route;
$this->routeProvider = $route_provider ? $route_provider : \Drupal::service('router.route_provider');
$this->path = new PathUtility($route->getPath());
}
示例7: getDownloadRoute
/**
* Build the route for the actual download path.
*/
protected function getDownloadRoute(EntityTypeInterface $entity_type)
{
$entity_type_id = $entity_type->id();
$route = new Route("/rdf-export/{$entity_type_id}/{{$entity_type_id}}/{export_format}");
$route->addDefaults(['_controller' => '\\Drupal\\rdf_export\\Controller\\RdfExportController::download', '_title' => 'RDF Export'])->addRequirements(['_permission' => 'export rdf metadata'])->setOption('entity_type_id', $entity_type_id)->setOption('parameters', [$entity_type_id => ['type' => 'entity:' . $entity_type_id]]);
return $route;
}
示例8: addCollection
public function addCollection($path, array $data = array())
{
$route = new Route($path);
// $pageClass = ($entity->getResource()->getPageClass()) ? $entity->getResource()->getPageClass() : $entity->getResource()->getPageClass();
$route->setDefaults($data);
return $route;
}
示例9: access
/**
* {@inheritdoc}
*/
public function access(Route $route, AccountInterface $account, Request $request)
{
$_entity_revision = $request->attributes->get('_entity_revision');
$operation = $route->getRequirement('_entity_access_revision');
list(, $operation) = explode('.', $operation, 2);
return AccessResult::allowedIf($_entity_revision && $this->checkAccess($_entity_revision, $account, $operation))->cachePerPermissions();
}
示例10: testAppliesWithFormat
/**
* @covers ::applies
*/
public function testAppliesWithFormat()
{
$route_filter = new RequestFormatRouteFilter();
$route = new Route('/test');
$route->setRequirement('_format', 'json');
$this->assertTrue($route_filter->applies($route));
}
示例11: shouldExcludeRoute
public function shouldExcludeRoute($routeName, Route $route)
{
if ('_' === $routeName[0] || !$route->hasOption('i18n')) {
return true;
}
return false;
}
示例12: access
/**
* Checks if the user has access to underlying storage for a Panels display.
*
* @param \Symfony\Component\Routing\Route $route
* The route to check against.
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
* The parametrized route.
* @param \Drupal\Core\Session\AccountInterface $account
* The currently logged in account.
*
* @return \Drupal\Core\Access\AccessResultInterface
* The access result.
*/
public function access(Route $route, RouteMatchInterface $route_match, AccountInterface $account)
{
$panels_storage_type = $route_match->getParameter('panels_storage_type');
$panels_storage_id = $route_match->getParameter('panels_storage_id');
$op = $route->getRequirement('_panels_storage_access');
return $this->panelsStorage->access($panels_storage_type, $panels_storage_id, $op, $account);
}
示例13: handle
public function handle(ApiDoc $annotation, array $annotations, Route $route, \ReflectionMethod $method)
{
// description
if (null === $annotation->getDescription()) {
$comments = explode("\n", $annotation->getDocumentation());
// just set the first line
$comment = trim($comments[0]);
$comment = preg_replace("#\n+#", ' ', $comment);
$comment = preg_replace('#\\s+#', ' ', $comment);
$comment = preg_replace('#[_`*]+#', '', $comment);
if ('@' !== substr($comment, 0, 1)) {
$annotation->setDescription($comment);
}
}
// requirements
$requirements = $annotation->getRequirements();
foreach ($route->getRequirements() as $name => $value) {
if (!isset($requirements[$name]) && '_method' !== $name && '_scheme' !== $name) {
$requirements[$name] = array('requirement' => $value, 'dataType' => '', 'description' => '');
}
if ('_scheme' === $name) {
$https = 'https' == $value;
$annotation->setHttps($https);
}
}
if (method_exists($route, 'getSchemes')) {
$annotation->setHttps(in_array('https', $route->getSchemes()));
}
$paramDocs = array();
foreach (explode("\n", $this->commentExtractor->getDocComment($method)) as $line) {
if (preg_match('{^@param (.+)}', trim($line), $matches)) {
$paramDocs[] = $matches[1];
}
if (preg_match('{^@deprecated\\b(.*)}', trim($line), $matches)) {
$annotation->setDeprecated(true);
}
if (preg_match('{^@link\\b(.*)}', trim($line), $matches)) {
$annotation->setLink($matches[1]);
}
}
$regexp = '{(\\w*) *\\$%s\\b *(.*)}i';
foreach ($route->compile()->getVariables() as $var) {
$found = false;
foreach ($paramDocs as $paramDoc) {
if (preg_match(sprintf($regexp, preg_quote($var)), $paramDoc, $matches)) {
$requirements[$var]['dataType'] = isset($matches[1]) ? $matches[1] : '';
$requirements[$var]['description'] = $matches[2];
if (!isset($requirements[$var]['requirement'])) {
$requirements[$var]['requirement'] = '';
}
$found = true;
break;
}
}
if (!isset($requirements[$var]) && false === $found) {
$requirements[$var] = array('requirement' => '', 'dataType' => '', 'description' => '');
}
}
$annotation->setRequirements($requirements);
}
示例14: describeRoute
/**
* {@inheritdoc}
*/
protected function describeRoute(Route $route, array $options = array())
{
$requirements = $route->getRequirements();
unset($requirements['_scheme'], $requirements['_method']);
// fixme: values were originally written as raw
$description = array(
'<comment>Path</comment> '.$route->getPath(),
'<comment>Host</comment> '.('' !== $route->getHost() ? $route->getHost() : 'ANY'),
'<comment>Scheme</comment> '.($route->getSchemes() ? implode('|', $route->getSchemes()) : 'ANY'),
'<comment>Method</comment> '.($route->getMethods() ? implode('|', $route->getMethods()) : 'ANY'),
'<comment>Class</comment> '.get_class($route),
'<comment>Defaults</comment> '.$this->formatRouterConfig($route->getDefaults()),
'<comment>Requirements</comment> '.$this->formatRouterConfig($requirements) ?: 'NO CUSTOM',
'<comment>Options</comment> '.$this->formatRouterConfig($route->getOptions()),
'<comment>Path-Regex</comment> '.$route->compile()->getRegex(),
);
if (isset($options['name'])) {
array_unshift($description, '<comment>Name</comment> '.$options['name']);
array_unshift($description, $this->formatSection('router', sprintf('Route "%s"', $options['name'])));
}
if (null !== $route->compile()->getHostRegex()) {
$description[] = '<comment>Host-Regex</comment> '.$route->compile()->getHostRegex();
}
$this->writeText(implode("\n", $description)."\n", $options);
}
示例15: load
public function load($resource, $type = null)
{
if (true === $this->loaded) {
throw new \RuntimeException('Do not add this loader twice');
}
$routes = new RouteCollection();
$contextConfigLoader = $this->contextConfigLoader;
foreach ($contextConfigLoader->getRouting() as $routing) {
foreach ($routing as $id => $info) {
if (array_key_exists('pattern', $info)) {
$route = new Route($info['pattern']);
// merge options and defaults
if (array_key_exists('defaults', $info)) {
$route->addDefaults($info['defaults']);
}
if (array_key_exists('options', $info)) {
$route->addOptions($info['options']);
}
if (array_key_exists('requirements', $info)) {
$route->addRequirements($info['requirements']);
}
}
$routes->add($id, $route);
}
}
return $routes;
}