Skip to content

Commit

Permalink
Merge pull request #14 from Hexanet/master
Browse files Browse the repository at this point in the history
Allow more flexible matches between action and breadcrumb route
  • Loading branch information
Petit Yoann committed Apr 8, 2014
2 parents ce649e6 + e8acb56 commit 54ed1b8
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 25 deletions.
10 changes: 7 additions & 3 deletions BreadcrumbTrail/Trail.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ public function add($breadcrumb_or_title, $routeName = null, $routeParameters =
if (!is_string($breadcrumb_or_title)) {
throw new \InvalidArgumentException('The title of a breadcrumb must be a string.');
}

$request = $this->container->get('request', ContainerInterface::NULL_ON_INVALID_REFERENCE);

if ($request !== null) {
preg_match_all('#\{(?P<variable>\w+).?(?P<function>\w*):?(?P<parameters>(\w|,| )*)\}#', $breadcrumb_or_title, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER);
foreach ($matches as $match) {
Expand Down Expand Up @@ -118,10 +118,14 @@ public function add($breadcrumb_or_title, $routeName = null, $routeParameters =
if (is_numeric($key)) {
$routeParameters[$value] = $request->get($value);
unset($routeParameters[$key]);
} else {
if (preg_match('#^\{(?P<parameter>\w+)\}$#', $value, $matches)) {
$routeParameters[$key] = $request->get($matches['parameter']);
}
}
}
}

$url = null;
if ( !is_null($routeName) ) {
$url = $this->router->generate($routeName, $routeParameters, $routeAbsolute);
Expand Down
60 changes: 38 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,80 +64,96 @@ class MyController extends Controller
*/
}
/**
* With route parameters dynamically fetched in the Request
* @Breadcrumb("Level 3b")
* @Breadcrumb("Level 4b", route={"name"="level_4b", "parameters"={"var1","var2"=2}})
* @Breadcrumb("Level 3c")
* @Breadcrumb("Level 4c", route={"name"="level_4c", "parameters"={"var1","var2"=2}})
*/
public function bRequestAction()
public function cRequestAction()
{
/*
This action will show the following breacrumb trail:
Level 1 > Level 2 > Level 3b > Level 4b
Level 1 > Level 2 > Level 3c > Level 4c
var1 will have the value given by $request->get("var1")
*/
}
/**
* With an associative array of route parameters dynamically fetched in the Request
* @Breadcrumb("Level 3d")
* @Breadcrumb("Level 4d", route={"name"="level_4d", "parameters"={"var1"="{var2}"}})
*/
public function dRequestAction()
{
/*
This action will show the following breacrumb trail:
Level 1 > Level 2 > Level 3d > Level 4d
var1 will have the value given by $request->get("var2")
*/
}
/**
* With position (position=0 will put the breacrumb to the end of the trail)
* @Breadcrumb("Level 3c", route="level_3c")
* @Breadcrumb("Level 4c", position=2)
* @Breadcrumb("Level 3e", route="level_3e")
* @Breadcrumb("Level 4e", position=2)
*/
public function cAction()
public function eAction()
{
/*
This action will show the following breacrumb trail:
Level 1 > Level 4c > Level 2 > Level 3c
Level 1 > Level 4e > Level 2 > Level 3e
*/
}
/**
* With negative position
* @Breadcrumb("Level 3d", route="level_3d")
* @Breadcrumb("Level 4d", position=-1)
* @Breadcrumb("Level 3f", route="level_3f")
* @Breadcrumb("Level 4f", position=-1)
*/
public function dAction()
public function fAction()
{
/*
This action will show the following breacrumb trail:
Level 1 > Level 2 > Level 4d > Level 3d
Level 1 > Level 2 > Level 4f > Level 3f
*/
}
/**
* Reset the trail
* @Breadcrumb("Level 3d", route="level_3d")
* @Breadcrumb("Level 3f", route="level_3f")
* @Breadcrumb()
* @Breadcrumb("Level 1e", route="level_1e")
* @Breadcrumb("Level 2e", route="level_2e")
* @Breadcrumb("Level 1g", route="level_1g")
* @Breadcrumb("Level 2g", route="level_2g")
*/
public function eAction()
public function gAction()
{
/*
This action will show the following breacrumb trail:
Level 1e > Level 2e
Level 1g > Level 2g
*/
}
/**
* Add extra attributes for the breadcrumb
* @Breadcrumb("Level 2e", route="level_2e", attributes={"class" : "yellow", "title" : "Hello world !"})
* @Breadcrumb("Level 2h", route="level_2h", attributes={"class" : "yellow", "title" : "Hello world !"})
*/
public function fAction()
public function hAction()
{
/*
This action will show the following breacrumb trail:
Level 1e > Level 2e
Level 1h > Level 2h
Level 2e will have additional attributes in the template
Level 2h will have additional attributes in the template
*/
}
Expand Down

0 comments on commit 54ed1b8

Please sign in to comment.