2017 © Pedro Peláez
 

symfony-bundle breadcrumbs-bundle

Friendly breadcrumbs for Symfony applications.

image

yceruto/breadcrumbs-bundle

Friendly breadcrumbs for Symfony applications.

  • Tuesday, October 11, 2016
  • by yceruto
  • Repository
  • 1 Watchers
  • 4 Stars
  • 349 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 9 Versions
  • 3 % Grown

The README.md

Symfony > Bundle > Breadcrumbs

Build Status Scrutinizer Code Quality Coverage Status Packagist Version Packagist Download SensioLabsInsight SUPPORTS SYMFONY 2.x and 3.x, (*1)

A friendly way to create breadcrumbs for symfony applications., (*2)

Features * Build breadcrumbs through current request uri (default). * Customize breadcrumbs nodes. * Customize breadcrumbs template., (*3)

Installation

Step 1: Download the Bundle

$ composer require yceruto/breadcrumbs-bundle

This command requires you to have Composer installed globally, as explained in the Composer documentation., (*4)

Step 2: Enable the Bundle

<?php
// app/AppKernel.php

// ...
class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            // ...
            new Yceruto\Bundle\BreadcrumbsBundle\BreadcrumbsBundle(),
        );
    }

    // ...
}

Basic Usage

Render the breadcrumbs in your template

Render the breadcrumb through current request path info., (*5)

{# app/Resources/views/base.html.twig #}

{{ render_breadcrumbs() }}

That's it!, (*6)

How it work

Suppose you the follows routes and translation:, (*7)

# app/config/routing.yml

_index:
    path: /
    defaults: { _controller: ... }

_store:
    path: /store
    defaults: { _controller: ... }

_category:
    path: /store/{category}
    defaults: { _controller: ... }

_category_product:
    path: /store/{category}/{product}
    defaults: { _controller: ... }
# app/Resources/translations/messages.en.yml

breadcrumbs._index: Home

For this request path /store/foo/bar the render_breadcrumbs() function returns:, (*8)

<ol class="breadcrumb">
    <li><a href="/">Home</a></li>
    <li><a href="/store">Store</a></li>
    <li><a href="/store/foo">Foo</a></li>
    <li class="active">Bar</li>
</ol>

If your application does not use translation feature, you can set the label text in route definition:, (*9)

_index:
    path: /
    defaults: { _controller: ..., breadcrumbs_label: 'Home' }

Translate the Breadcrumbs Interface

The breadcrumbs uses the same language as the underlying Symfony application, which is usually configured in the locale option of the app/config/parameters.yml file., (*10)

The strings that belong to the breadcrumbs interface are translated using the default messages domain., (*11)

In addition, make sure that the translator service is enabled in the application (projects based on the Symfony Standard Edition have it disabled by default):, (*12)

# app/config/config.yml
framework:
    translator: { fallbacks: [ "%locale%" ] }

Advanced Usage

Customize the breadcrumb nodes

public function indexAction() 
{
    $breadcrumbs = $this->get('breadcrumbs_builder')->create();
    $breadcrumbs->add('/', 'home');

    // or

    $node = new BreadcrumbsNode();
    $node->setPath('/')
    $node->setLabel('home')
    $breadcrumbs->addNode($node);

    return $this->render('index.html.twig', array('custom_breadcrumbs' => $breadcrumbs))
}

Render customized breadcrumbs:, (*13)

{{ render_breadcrumbs(custom_breadcrumbs) }}

Overriding Default BreadcrumbsBundle Template

As you start to incorporate BreadcrumbsBundle into your application, you will probably find that you need to override the default template that is provided by the bundle. Although the template name is not configurable, the Symfony framework provides two ways to override the templates of a bundle., (*14)

  1. Define a new template of the same name in the app/Resources directory
  2. Create a new bundle that is defined as a child of BreadcrumbsBundle

Example: Overriding The Default breadcrumbs.html.twig

An example of overriding this breadcrumbs template is demonstrated below using first of the overriding options listed above., (*15)

Here is the default breadcrumbs.html.twig provided by the BreadcrumbsBundle:, (*16)

<ol class="breadcrumb">
    {% for node in breadcrumbs %}
        {% if not loop.last %}
            <li><a href="{{ node.path }}">{{ node.label|trans|title }}</a></li>
        {% else %}
            <li class="active">{{ node.label|trans|title }}</li>
        {% endif %}
    {% endfor %}
</ol>

The following Twig template file is an example of a breadcrumbs file that might be used to override the provided by the bundle., (*17)

<ol class="breadcrumb">
    {% for node in breadcrumbs %}
        {% set icon = loop.first ? '<i class="fa fa-home"></i>' %}

        {% if not loop.last %}
            <li><a href="{{ node.path }}">{{ icon|raw }}{{ node.label|trans|title }}</a></li>
        {% else %}
            <li class="active">{{ icon|raw }}{{ node.label|trans|title }}</li>
        {% endif %}
    {% endfor %}
</ol>

1) Define New Template In app/Resources, (*18)

The easiest way to override a bundle's template is to simply place a new one in your app/Resources folder. To override the breadcrumbs template located at Resources/views/breadcrumbs.html.twig in the BreadcrumbsBundle directory, you would place your new breadcrumbs template at app/Resources/BreadcrumbsBundle/views/breadcrumbs.html.twig., (*19)

As you can see the pattern for overriding templates in this way is to create a folder with the name of the bundle class in the app/Resources directory. Then add your new template to this folder, preserving the directory structure from the original bundle., (*20)

Resources

You can run the unit tests with the following command:, (*21)

$ cd path/to/breadcrumbs-bundle/
$ composer install
$ phpunit

License

This software is published under the MIT License, (*22)

The Versions

11/10 2016

dev-master

9999999-dev https://github.com/yceruto/breadscrumbs-bundle

Friendly breadcrumbs for Symfony applications.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Yonel Ceruto

symfony breadcrumbs

01/02 2016

1.0.7

1.0.7.0 https://github.com/yceruto/breadscrumbs-bundle

Magic breadcrumbs bundle for Symfony application

  Sources   Download

MIT

The Requires

 

The Development Requires

by Yonel Ceruto

symfony breadcrumbs

13/01 2016

1.0.6

1.0.6.0 https://github.com/yceruto/breadscrumbs-bundle

Magic breadcrumbs bundle for Symfony application

  Sources   Download

MIT

The Requires

 

The Development Requires

by Yonel Ceruto

symfony breadcrumbs

12/01 2016

1.0.5

1.0.5.0 https://github.com/yceruto/breadscrumbs-bundle

Magic breadcrumbs bundle for Symfony application

  Sources   Download

MIT

The Requires

 

The Development Requires

by Yonel Ceruto

symfony breadcrumbs

12/01 2016

1.0.4

1.0.4.0 https://github.com/yceruto/breadscrumbs-bundle

Magic breadcrumbs bundle for Symfony application

  Sources   Download

MIT

The Requires

 

The Development Requires

by Yonel Ceruto

symfony breadcrumbs

05/01 2016

1.0.3

1.0.3.0 https://github.com/yceruto/breadscrumbs-bundle

Magic breadcrumbs bundle for Symfony application

  Sources   Download

MIT

The Requires

 

The Development Requires

by Yonel Ceruto

symfony breadcrumbs

02/01 2016

1.0.2

1.0.2.0 https://github.com/yceruto/breadscrumbs-bundle

Magic breadcrumbs bundle for Symfony application

  Sources   Download

MIT

The Requires

 

The Development Requires

by Yonel Ceruto

symfony breadcrumbs

12/12 2015

1.0.1

1.0.1.0 https://github.com/yceruto/breadscrumbs-bundle

Magic breadcrumbs bundle for Symfony application

  Sources   Download

MIT

The Requires

 

The Development Requires

by Yonel Ceruto

symfony breadcrumbs

10/12 2015

1.0

1.0.0.0 https://github.com/yceruto/breadscrumbs-bundle

Magic breadcrumbs bundle for Symfony application

  Sources   Download

MIT

The Requires

 

The Development Requires

by Yonel Ceruto

symfony breadcrumbs