Skip to content

Instantly share code, notes, and snippets.

@b1rdex
Created April 3, 2014 04:13
Show Gist options
  • Save b1rdex/9948164 to your computer and use it in GitHub Desktop.
Save b1rdex/9948164 to your computer and use it in GitHub Desktop.
symfony1 breadcrumbs builder based on filter
<?php
class breadcrumbsFilter extends sfFilter
{
public function execute($filterChain)
{
if ($this->isFirstCall()) {
$module = $this->context->getRequest()->getParameter("module");
$action = $this->context->getRequest()->getParameter("action");
$route = $this->context->getRouting()->getCurrentRouteName();
$crumbs = [
"by-route" => [
"homepage" => function($that) use ($module, $action, $route) {
return [
"test/index" => "Отображение",
"test/index?id=2" => "Отображение 2",
];
}
],
"by-module" => [
"main" => function($that) use ($module, $action, $route) {
return [
"test/index" => "Отображение",
"test/index?id=2" => "Отображение 2",
];
}
],
"by-action" => [
"index" => function($that) use ($module, $action, $route) {
return [
"test/index" => "Отображение",
"test/index?id=2" => "Отображение 2",
];
}
],
];
function array_search_key($needle, array &$haystack) {
return isset($haystack[$needle]) && is_callable($haystack[$needle]) ? $haystack[$needle] : false;
}
$generator =
array_search_key($route, $crumbs["by-route"])
?: array_search_key($module, $crumbs["by-module"])
?: array_search_key($action, $crumbs["by-action"])
?: function($that) {
return [
"@homepage" => "Главная страница",
];
}
;
$breadcrumbs = call_user_func($generator, $this);
$this->context->getUser()->setFlash("breadcrumbs", $breadcrumbs);
}
$filterChain->execute();
}
}
<div class="row">
<div class="main container">
<ol class="breadcrumb">
<?php
$crumbs = $sf_user->getFlash("breadcrumbs")->getRawValue();
$last = array_splice($crumbs, -1);
foreach ($crumbs as $route => $name): ?>
<li><a href="<?php echo url_for($route); ?>"><?php echo $name; ?></a></li>
<?php endforeach ?>
<li class="active"><?php echo array_pop($last); ?></li>
</ol>
</div>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment