Skip to content

Fluid interface for building routes in PHP #15778

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 34 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
51f60fc
Adding a new framework-specific Route class
weaverryan Sep 9, 2015
6891ec8
Adding a class to make adding/importing routes easier and more fluid
weaverryan Sep 9, 2015
4e430a0
Maintaining all the RouteCollection abilities to RouteCollectionBuilder
weaverryan Sep 12, 2015
0ddadc5
Moving the prefix to the builder, so that it's consistent with other …
weaverryan Sep 13, 2015
06ea900
Adding phpdoc
weaverryan Sep 13, 2015
6b922a6
Using InvalidArgumentException
weaverryan Sep 13, 2015
14518ed
No change - renaming variable
weaverryan Sep 13, 2015
7972fc9
Adding many more tests, which included a few small bug fixes with val…
weaverryan Sep 13, 2015
e11b7e0
Removed prefix argument from mount() - and added it instead to create…
weaverryan Sep 13, 2015
4d90916
fabbot!
weaverryan Sep 14, 2015
729ccbb
Renaming flush() to build()
weaverryan Sep 15, 2015
e509953
Not clearing everything on build - unnecessary, and the RouteCollecti…
weaverryan Sep 15, 2015
01e1329
Fixing phpdoc
weaverryan Sep 15, 2015
e39e0c4
Removing the FrameworkBundle Route and the ability to call Route::set…
weaverryan Sep 16, 2015
df1849f
Simplifying by transforming RouteCollection's into RouteCollectionBui…
weaverryan Sep 16, 2015
97b1eea
Fixing a bug with knowing which keys should be auto-generated
weaverryan Sep 16, 2015
e1ecde4
Minor code improvement to centralize things
weaverryan Sep 16, 2015
ecf4346
Renaming methods for clarity and consistency
weaverryan Sep 16, 2015
0dce55d
fabbot and possible test fixes
weaverryan Sep 16, 2015
f3d71ad
Allowing LoaderInterface instead of Loader
weaverryan Sep 17, 2015
8132fcb
Updating setRequirements to avoid deprecated calls
weaverryan Sep 17, 2015
b2676ec
phpdoc typo
weaverryan Sep 26, 2015
bf6790b
Making RouteCollectionBuilder's LoaderInteface optional
weaverryan Sep 30, 2015
61e4bf7
removing extra spaces
weaverryan Sep 30, 2015
8f0b956
moving into the component
weaverryan Sep 30, 2015
fbab6d4
Removing the ability to set a prefix on a builder: that only happens …
weaverryan Sep 30, 2015
012cb92
Removing the prefix from import, and making the user actually put tha…
weaverryan Sep 30, 2015
33255dd
Fixing a bug with route name collission because the prefix wasn't acc…
weaverryan Sep 30, 2015
356b114
Refactoring into private method
weaverryan Sep 30, 2015
7cb8996
fabbot
weaverryan Sep 30, 2015
573a7f1
Small tweaks suggested by fabpot, including removal of addResource(),…
weaverryan Sep 30, 2015
83f3194
stretching out logic into multiple lines: there's some discussion abo…
weaverryan Sep 30, 2015
26d656b
Fabbot
weaverryan Sep 30, 2015
42e73a2
Adding throws
weaverryan Oct 1, 2015
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Removing the ability to set a prefix on a builder: that only happens …
…when you mount

addBuilder was also renamed to mount
  • Loading branch information
weaverryan committed Sep 30, 2015
commit fbab6d437e382b4fa18dec7d6165948c6fe256d7
28 changes: 6 additions & 22 deletions src/Symfony/Component/Routing/RouteCollectionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,16 +89,13 @@ public function add($path, $controller, $name = null)
}

/**
* Returns a RouteCollectionBuilder that can be configured and then added with addBuilder().
*
* @param string $prefix A prefix to apply to all routes added to this collection
* Returns a RouteCollectionBuilder that can be configured and then added with mount().
*
* @return RouteCollectionBuilder
*/
public function createBuilder($prefix = null)
public function createBuilder()
{
$builder = new self($this->loader);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just return here, no need for a variable.

$builder->setPrefix($prefix);

return $builder;
}
Expand All @@ -108,8 +105,9 @@ public function createBuilder($prefix = null)
*
* @param RouteCollectionBuilder $builder
*/
public function addBuilder(RouteCollectionBuilder $builder)
public function mount($prefix, RouteCollectionBuilder $builder)
{
$builder->prefix = trim(trim($prefix), '/');
$this->routes[] = $builder;
}

Expand All @@ -124,7 +122,7 @@ public function addBuilder(RouteCollectionBuilder $builder)
public function addRouteCollection(RouteCollection $collection, $prefix = null)
{
// create a builder from the RouteCollection
$builder = $this->createBuilder($prefix);
$builder = $this->createBuilder();
foreach ($collection->all() as $name => $route) {
$builder->addRoute($route, $name);
}
Expand All @@ -133,7 +131,7 @@ public function addRouteCollection(RouteCollection $collection, $prefix = null)
$builder->addResource($resource);
}

$this->addBuilder($builder);
$this->mount($prefix, $builder);

return $builder;
}
Expand All @@ -158,20 +156,6 @@ public function addRoute(Route $route, $name = null)
return $this;
}

/**
* Sets a prefix (e.g. /admin) to be used with all embedded routes.
*
* @param string $prefix
*
* @return $this
*/
public function setPrefix($prefix)
{
$this->prefix = trim(trim($prefix), '/');

return $this;
}

/**
* Sets the host on all embedded routes (unless already set).
*
Expand Down
20 changes: 11 additions & 9 deletions src/Symfony/Component/Routing/Tests/RouteCollectionBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,10 +239,13 @@ public function testFlushSetsDetailsOnChildrenRoutes()
public function testFlushPrefixesPaths($collectionPrefix, $routePath, $expectedPath)
{
$routes = new RouteCollectionBuilder();
$routes->setPrefix($collectionPrefix);

$routes->add($routePath, 'someController', 'test_route');
$collection = $routes->build();

$outerRoutes = new RouteCollectionBuilder();
$outerRoutes->mount($collectionPrefix, $routes);

$collection = $outerRoutes->build();

$this->assertEquals($expectedPath, $collection->get('test_route')->getPath());
}
Expand All @@ -255,7 +258,6 @@ public function providePrefixTests()
// normal prefix - does not matter if it's a wildcard
$tests[] = array('/{admin}', '/foo', '/{admin}/foo');
// shows that a prefix will always be given the starting slash
$tests = array();
$tests[] = array('0', '/foo', '/0/foo');

// spaces are ok, and double slahses at the end are cleaned
Expand All @@ -271,24 +273,24 @@ public function testFlushSetsPrefixedWithMultipleLevels()

$routes->add('homepage', 'MainController::homepageAction', 'homepage');

$adminRoutes = $routes->createBuilder('/admin');
$adminRoutes = $routes->createBuilder();
$adminRoutes->add('/dashboard', 'AdminController::dashboardAction', 'admin_dashboard');

// embedded collection under /admin
$adminBlogRoutes = $routes->createBuilder('/blog');
$adminBlogRoutes = $routes->createBuilder();
$adminBlogRoutes->add('/new', 'BlogController::newAction', 'admin_blog_new');
// mount into admin, but before the parent collection has been mounted
$adminRoutes->addBuilder($adminBlogRoutes);
$adminRoutes->mount('/blog', $adminBlogRoutes);

// now mount the /admin routes, above should all still be /blog/admin
$routes->addBuilder($adminRoutes);
$routes->mount('/admin', $adminRoutes);
// add a route after mounting
$adminRoutes->add('/users', 'AdminController::userAction', 'admin_users');

// add another sub-collection after the mount
$otherAdminRoutes = $routes->createBuilder('/stats');
$otherAdminRoutes = $routes->createBuilder();
$otherAdminRoutes->add('/sales', 'StatsController::indexAction', 'admin_stats_sales');
$adminRoutes->addBuilder($otherAdminRoutes);
$adminRoutes->mount('/stats', $otherAdminRoutes);

// add a normal collection and see that it is also prefixed
$importedCollection = new RouteCollection();
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy