當前位置: 首頁>>代碼示例>>PHP>>正文


PHP RouteCollection::addDefaults方法代碼示例

本文整理匯總了PHP中Symfony\Component\Routing\RouteCollection::addDefaults方法的典型用法代碼示例。如果您正苦於以下問題:PHP RouteCollection::addDefaults方法的具體用法?PHP RouteCollection::addDefaults怎麽用?PHP RouteCollection::addDefaults使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在Symfony\Component\Routing\RouteCollection的用法示例。


在下文中一共展示了RouteCollection::addDefaults方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。

示例1: testAddDefaultsAndRequirementsAndOptions

 public function testAddDefaultsAndRequirementsAndOptions()
 {
     $collection = new RouteCollection();
     $collection->add('foo', new Route('/{placeholder}'));
     $collection1 = new RouteCollection();
     $collection1->add('bar', new Route('/{placeholder}', array('_controller' => 'fixed', 'placeholder' => 'default'), array('placeholder' => '.+'), array('option' => 'value')));
     $collection->addCollection($collection1);
     $collection->addDefaults(array('placeholder' => 'new-default'));
     $this->assertEquals(array('placeholder' => 'new-default'), $collection->get('foo')->getDefaults(), '->addDefaults() adds defaults to all routes');
     $this->assertEquals(array('_controller' => 'fixed', 'placeholder' => 'new-default'), $collection->get('bar')->getDefaults(), '->addDefaults() adds defaults to all routes and overwrites existing ones');
     $collection->addRequirements(array('placeholder' => '\\d+'));
     $this->assertEquals(array('placeholder' => '\\d+'), $collection->get('foo')->getRequirements(), '->addRequirements() adds requirements to all routes');
     $this->assertEquals(array('placeholder' => '\\d+'), $collection->get('bar')->getRequirements(), '->addRequirements() adds requirements to all routes and overwrites existing ones');
     $collection->addOptions(array('option' => 'new-value'));
     $this->assertEquals(array('option' => 'new-value', 'compiler_class' => 'Symfony\\Component\\Routing\\RouteCompiler'), $collection->get('bar')->getOptions(), '->addOptions() adds options to all routes and overwrites existing ones');
 }
開發者ID:mawaha,項目名稱:tracker,代碼行數:16,代碼來源:RouteCollectionTest.php

示例2: addCollection

    /**
     * Adds a route collection at the end of the current set by appending all
     * routes of the added collection.
     *
     * @param RouteCollection $collection      A RouteCollection instance
     *
     * @api
     */
    public function addCollection(RouteCollection $collection)
    {
        // This is to keep BC for getParent() and getRoot(). It does not prevent
        // infinite loops by recursive referencing. But we don't need that logic
        // anymore as the tree logic has been deprecated and we are just widening
        // the accepted range.
        $collection->parent = $this;

        // this is to keep BC
        $numargs = func_num_args();
        if ($numargs > 1) {
            $collection->addPrefix($this->prefix . func_get_arg(1));
            if ($numargs > 2) {
                $collection->addDefaults(func_get_arg(2));
                if ($numargs > 3) {
                    $collection->addRequirements(func_get_arg(3));
                    if ($numargs > 4) {
                        $collection->addOptions(func_get_arg(4));
                    }
                }
            }
        } else {
            // the sub-collection must have the prefix of the parent (current instance) prepended because it does not
            // necessarily already have it applied (depending on the order RouteCollections are added to each other)
            // this will be removed when the BC layer for getPrefix() is removed
            $collection->addPrefix($this->prefix);
        }

        // we need to remove all routes with the same names first because just replacing them
        // would not place the new route at the end of the merged array
        foreach ($collection->all() as $name => $route) {
            unset($this->routes[$name]);
            $this->routes[$name] = $route;
        }

        $this->resources = array_merge($this->resources, $collection->getResources());
    }
開發者ID:Robert-Xie,項目名稱:php-framework-benchmark,代碼行數:45,代碼來源:RouteCollection.php

示例3: initRouting

/**
 * @param ContainerInterface $container
 * @return UrlMatcher
 */
function initRouting(ContainerInterface $container)
{
    $routeCollection = new RouteCollection();
    $viewRoute = new Route('/view', ['_controller' => 'Crud', '_action' => 'view']);
    $routeCollection->add('crud_view', $viewRoute);
    $addRoute = new Route('/add', ['_controller' => 'Crud', '_action' => 'add']);
    $routeCollection->add('crud_add', $addRoute);
    $editRoute = new Route('/edit/{id}', ['_controller' => 'Crud', '_action' => 'edit'], ['id' => '.+']);
    $routeCollection->add('crud_edit', $editRoute);
    $routeCollection->addPrefix('/{_locale}/crud');
    $routeCollection->addDefaults(['_locale' => 'ru']);
    $routeCollection->addRequirements(['_locale' => 'ru|en']);
    $locator = new FileLocator([SRC_PATH . 'Resources/config']);
    $loader = new YamlFileLoader($locator);
    $collection = $loader->load('routes.yml');
    $routeCollection->addCollection($collection);
    $closureLoader = new ClosureLoader();
    $collection = $closureLoader->load(function () {
        return new RouteCollection();
    });
    $requestContext = new RequestContext();
    $requestContext->fromRequest(Request::createFromGlobals());
    $matcher = new UrlMatcher($routeCollection, $requestContext);
    $container->set('url_matcher', $matcher);
    $urlGenerator = new UrlGenerator($routeCollection, $requestContext);
    $container->set('url_generator', $urlGenerator);
    return $matcher;
}
開發者ID:GrizliK1988,項目名稱:symfony-certification-prepare-project,代碼行數:32,代碼來源:init.php


注:本文中的Symfony\Component\Routing\RouteCollection::addDefaults方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。