本文整理汇总了PHP中Symfony\Bundle\FrameworkBundle\Routing\Router::getRouteCollection方法的典型用法代码示例。如果您正苦于以下问题:PHP Router::getRouteCollection方法的具体用法?PHP Router::getRouteCollection怎么用?PHP Router::getRouteCollection使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Symfony\Bundle\FrameworkBundle\Routing\Router
的用法示例。
在下文中一共展示了Router::getRouteCollection方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: collectCrudRoutes
private function collectCrudRoutes()
{
/** @var $collection \Symfony\Component\Routing\RouteCollection */
$collection = $this->router->getRouteCollection();
$allRoutes = $collection->all();
/** @var $route \Symfony\Component\Routing\Route */
foreach ($allRoutes as $routeName => $route) {
$defaults = $route->getDefaults();
if (!isset($defaults[self::TAG_CRUD])) {
continue;
}
if (!isset($defaults['_controller'])) {
throw new GeneralException(sprintf('Route %s doesn\'t contain \'_controller\' argument.', $routeName));
}
$routeNameParts = $this->explodeRouteNameParts($defaults['_controller']);
$crudRoute = new CrudRoute();
$crudRoute->setRouteName($routeName);
$crudRoute->setCrudName($defaults[self::TAG_CRUD]);
$crudRoute->setControllerFullName($routeNameParts[0]);
$crudRoute->setBundleName($routeNameParts[1] . $routeNameParts[2]);
//TODO: check long namespaces
$crudRoute->setControllerName($routeNameParts[3]);
$crudRoute->setActionName($routeNameParts[4]);
$this->crudRoutes[$crudRoute->getControllerFullName()] = $crudRoute;
}
}
示例2: onKernelRequest
/**
* @param GetResponseEvent $event
*/
public function onKernelRequest(GetResponseEvent $event)
{
if (!$this->installed) {
return;
}
$request = $event->getRequest();
if ($request->attributes->has('_controller') || $event->getRequestType() !== HttpKernelInterface::MASTER_REQUEST) {
return;
}
$slugUrl = $request->getPathInfo();
if ($slugUrl !== '/') {
$slugUrl = rtrim($slugUrl, '/');
}
/** @var EntityManager $em */
$em = $this->registry->getManagerForClass('OroB2BRedirectBundle:Slug');
$slug = $em->getRepository('OroB2BRedirectBundle:Slug')->findOneBy(['url' => $slugUrl]);
if ($slug) {
$routeName = $slug->getRouteName();
$controller = $this->router->getRouteCollection()->get($routeName)->getDefault('_controller');
$parameters = [];
$parameters['_route'] = $routeName;
$parameters['_controller'] = $controller;
$redirectRouteParameters = $slug->getRouteParameters();
$parameters = array_merge($parameters, $redirectRouteParameters);
$parameters['_route_params'] = $redirectRouteParameters;
$request->attributes->add($parameters);
}
}
示例3: getRouteCollection
/**
* @return \Symfony\Component\Routing\RouteCollection
*/
public function getRouteCollection()
{
if (null !== $this->parent) {
return $this->parent->getRouteCollection();
}
return parent::getRouteCollection();
}
示例4: onKernelRequest
/**
* Check request to decide if user has access to specific route
*
* @param GetResponseEvent $event
* @throws AccessDeniedException
* @throws InvalidRouteException
* @throws UserNotFoundException
*/
public function onKernelRequest(GetResponseEvent $event)
{
$routeName = $event->getRequest()->get("_route");
if (strpos($routeName, "app_default_") === 0) {
throw new InvalidRouteException();
}
$routeCollection = $this->router->getRouteCollection();
$route = $routeCollection->get($routeName);
if ($route instanceof Route) {
//Check if need to validate route
//Sometime we want to allow access without validation: index page, login page
$accessValidation = $route->getOption('access_validation');
if ($accessValidation === false) {
return;
}
//Validate current user access to route
$this->authentication->setCurrentUser($this->request->get("token"));
$user = $this->authentication->getCurrentUser();
if (!$user instanceof User) {
throw new UserNotFoundException();
}
$access = $this->accessService->checkPermissions($user, $routeName);
if ($access === false) {
throw new AccessDeniedException($user, $routeName);
}
}
}
示例5: generateDistantUrl
protected function generateDistantUrl($routeName, $parameters = [])
{
$url = $this->router->generate($routeName, $parameters, UrlGeneratorInterface::ABSOLUTE_PATH);
//if (strpos($url, '/app_dev.php') === 0) {
// $url = '/app.php' . substr($url, 12);
//}
$url = 'http://' . $this->router->getRouteCollection()->get($routeName)->getDefault('_http_host') . $url;
// TODO: manage SSL mode (HTTP/HTTPS), host by host
return $url;
}
示例6: buildForForward
public function buildForForward(Request $request)
{
$currentRoute = $this->router->getRouteCollection()->get($request->attributes->get('_route'));
$forwardRoute = $this->router->getRouteCollection()->get($currentRoute->getOption('forward_http_route'));
$rmqClientConnection = $currentRoute->getOption('forward_rmq_producer');
// Localhost case
if ($forwardRoute->getDefault('_http_host') === $request->getHttpHost()) {
$this->logger->info("InternalForwarder built for forward: Localhost forwarder.", ['host' => $request->getHttpHost()]);
return $this->container->get('prestashop.public_writer.protocol.internal_forwarder.localhost');
}
// Error case: localhost case was not matching, but there is no other forwarder available.
$this->logger->error("InternalForwarder built for forward: NO forwarder found to reach distant host.", ['host' => $request->getHttpHost()]);
throw new \ErrorException("InternalForwarder building for forward: NO forwarder found to reach distant host: " . $request->getHttpHost());
}
示例7: testPlaceholders
public function testPlaceholders()
{
$routes = new RouteCollection();
$routes->add('foo', new Route('/foo', array('foo' => '%foo%', 'bar' => '%bar%', 'foobar' => 'foobar', 'foo1' => '%foo', 'foo2' => 'foo%', 'foo3' => 'f%o%o'), array('foo' => '%foo%', 'bar' => '%bar%', 'foobar' => 'foobar', 'foo1' => '%foo', 'foo2' => 'foo%', 'foo3' => 'f%o%o')));
$sc = $this->getServiceContainer($routes);
$sc->expects($this->at(1))->method('hasParameter')->will($this->returnValue(false));
$sc->expects($this->at(2))->method('hasParameter')->will($this->returnValue(true));
$sc->expects($this->at(3))->method('getParameter')->will($this->returnValue('bar'));
$sc->expects($this->at(4))->method('hasParameter')->will($this->returnValue(false));
$sc->expects($this->at(5))->method('hasParameter')->will($this->returnValue(true));
$sc->expects($this->at(6))->method('getParameter')->will($this->returnValue('bar'));
$router = new Router($sc, 'foo');
$route = $router->getRouteCollection()->get('foo');
$this->assertEquals('%foo%', $route->getDefault('foo'));
$this->assertEquals('bar', $route->getDefault('bar'));
$this->assertEquals('foobar', $route->getDefault('foobar'));
$this->assertEquals('%foo', $route->getDefault('foo1'));
$this->assertEquals('foo%', $route->getDefault('foo2'));
$this->assertEquals('f%o%o', $route->getDefault('foo3'));
$this->assertEquals('%foo%', $route->getRequirement('foo'));
$this->assertEquals('bar', $route->getRequirement('bar'));
$this->assertEquals('foobar', $route->getRequirement('foobar'));
$this->assertEquals('%foo', $route->getRequirement('foo1'));
$this->assertEquals('foo%', $route->getRequirement('foo2'));
$this->assertEquals('f%o%o', $route->getRequirement('foo3'));
}
示例8: getLabel
/**
* Get label
*
* @param $path
* @param $parent
* @param $name
*
* @return string
*/
private function getLabel($path, $parent, $name)
{
$route = $this->router->getRouteCollection()->get($name);
// get label through settings
$label = $route->getDefault('breadcrumbs_label');
if (empty($label)) {
// get label through path
$compiledRoute = $route->compile();
$vars = $compiledRoute->getVariables();
if (empty($vars)) {
$label = substr($path, strlen($parent));
} elseif (preg_match($compiledRoute->getRegex(), $path, $match)) {
$label = $match[end($vars)];
}
$label = trim(preg_replace('[\\W|_]', ' ', $label));
if (is_numeric($label)) {
$label = null;
}
}
if (empty($label)) {
// get label through route name
$label = 'breadcrumbs.' . $name;
return $label;
}
return $label;
}
示例9: onKernelRequest
public function onKernelRequest(FilterControllerEvent $event)
{
$this->event = $event;
$this->request = $event->getRequest();
$rc = $this->router->getRouteCollection();
/* @var $rc \Symfony\Component\Routing\RouteCollection */
$route = $rc->get($this->request->get('_route'));
if (!$route) {
return false;
}
$acl = $route->getOption('ACL');
try {
// Verifico che sia stata richiesta la memorizzazione delle statistiche
if ($acl && is_array($acl)) {
if (!is_object($this->user)) {
throw new \Exception('User not logged');
}
// Opzioni default in caso di assenza
$options = array_merge(array('in_role' => array(), 'out_role' => array()), $acl);
// Trasformo i parametri in un array
if (!is_array($options['in_role'])) {
$options['in_role'] = array($options['in_role']);
}
if (!is_array($options['out_role'])) {
$options['out_role'] = array($options['out_role']);
}
// Verifico che l'utente abbia il ruolo necessario per visualizzare la pagina
$test_in = count($options['in_role']) == 0;
foreach ($options['in_role'] as $role) {
$test_in |= $this->user->hasRole($role);
}
if (!$test_in) {
throw new \Exception("User doesn't have permission");
}
$test_out = true;
foreach ($options['out_role'] as $role) {
$test_out &= !$this->user->hasRole($role);
}
if (!$test_out) {
throw new \Exception("User doesn't have permission");
}
}
} catch (\Exception $e) {
throw new \Symfony\Component\HttpKernel\Exception\NotFoundHttpException($e->getMessage());
}
}
示例10: getRouteCollection
public function getRouteCollection()
{
$collection = parent::getRouteCollection();
// Remove any page controller routes
if (!$this->stored) {
$this->storeRoutes($collection);
}
return $collection;
}
示例11: testExceptionOnNonStringParameter
/**
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
* @expectedExceptionMessage A string value must be composed of strings and/or numbers,but found parameter "object" of type object inside string value "/%object%".
*/
public function testExceptionOnNonStringParameter()
{
$routes = new RouteCollection();
$routes->add('foo', new Route('/%object%'));
$sc = $this->getServiceContainer($routes);
$sc->expects($this->at(1))->method('hasParameter')->with('object')->will($this->returnValue(true));
$sc->expects($this->at(2))->method('getParameter')->with('object')->will($this->returnValue(new \stdClass()));
$router = new Router($sc, 'foo');
$router->getRouteCollection()->get('foo');
}
示例12: onKernelRequest
/**
* @param GetResponseEvent $event
*
* @throws RpcException
*/
public function onKernelRequest(GetResponseEvent $event)
{
$request = $event->getRequest();
$requestType = $request->headers->get('Content-Type');
$responseType = $request->headers->get('Accept');
if (!$this->factory->validate($requestType) or !$this->factory->validate($responseType)) {
return;
}
$rpcRequest = $this->factory->createFrom($request);
$rpcRoute = $this->getRouteName($rpcRequest);
$route = $this->router->getRouteCollection()->get($rpcRoute);
if (!$route) {
$message = sprintf('Route %s not found', $rpcRoute);
throw new RpcException($message);
}
$controller = $route->getDefault('_controller');
$request->attributes->set('_controller', $controller);
$request->attributes->set('_route', $rpcRoute);
$request->attributes->set('rpcRequest', $rpcRequest);
}
示例13: getRouteCollection
/**
* {@inheritdoc}
*/
public function getRouteCollection()
{
$collection = parent::getRouteCollection();
$routes = $collection->all();
$newCollection = new RouteCollection();
$routes = $this->sortRoutes($routes);
foreach ($routes as $name => $route) {
$newCollection->add($name, $route);
}
return $newCollection;
}
示例14: dumpTranslations
/**
* @param array $locales
*
* @return bool
* @throws \Symfony\Component\Filesystem\Exception\IOException
* @throws \RuntimeException
*/
public function dumpTranslations($locales = [])
{
if (empty($locales)) {
$locales[] = $this->defaultLocale;
}
$targetPattern = realpath($this->kernelRootDir . '/../web') . $this->router->getRouteCollection()->get($this->jsTranslationRoute)->getPath();
foreach ($locales as $locale) {
$target = strtr($targetPattern, array('{_locale}' => $locale));
$this->logger->info(sprintf('<comment>%s</comment> <info>[file+]</info> %s', date('H:i:s'), basename($target)));
$content = $this->translationController->renderJsTranslationContent($this->translationDomains, $locale);
$dirName = dirname($target);
if (!is_dir($dirName) && true !== @mkdir($dirName, 0777, true)) {
throw new IOException(sprintf('Failed to create %s', $dirName));
}
if (false === @file_put_contents($target, $content)) {
throw new \RuntimeException('Unable to write file ' . $target);
}
}
return true;
}
示例15: getRouteCollection
/**
* {@inheritdoc}
*/
public function getRouteCollection()
{
$key = 'route_collection';
if (null === $this->collection) {
if ($this->cache->hasItem($key)) {
$collection = $this->cache->getItem($key)->get();
if ($collection !== null) {
$this->collection = $collection;
return $this->collection;
}
}
$this->collection = parent::getRouteCollection();
$item = $this->cache->getItem($key);
$item->set($this->collection)->expiresAfter(self::CACHE_LIFETIME);
}
return $this->collection;
}