Skip to content

Commit

Permalink
BreadcrumbGenerators are now automatically registered in BreadcrumbRe…
Browse files Browse the repository at this point in the history
…solver

- documentation about breadcrumb has been added
  • Loading branch information
TomasLudvik committed Jun 28, 2019
1 parent c943c53 commit 661632f
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 70 deletions.
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
* [Directories](./introduction/directories.md)
* [Cron](./introduction/cron.md)
* [Using Form Types](./introduction/using-form-types.md)
* [Friendly URL](./introduction/friendly-url.md)
* [Friendly URL, Breadcrumb navigation](introduction/friendly-url-breadcrumb-navigation.md)

## Model
* [Introduction to Model Architecture](./model/introduction-to-model-architecture.md)
Expand Down
4 changes: 2 additions & 2 deletions docs/introduction/faq-and-common-issues.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,5 +120,5 @@ It is also possible to turn the spool off by removing it from [swiftmailer.yml](
The business logic should be implemented directly in an entity every time when there is no need for external services.
Otherwise, the logic is in facades (resp. the facades are used as delegates to other services, e.g. another *Facade*, *Repository*, *Calculation*, etc.). You can read more about the model architecture in [Introduction to model architecture](/docs/model/introduction-to-model-architecture.md).

## How can I create a friendly URL for my entity?
See [Friendly URL](/docs/introduction/friendly-url.md#friendly-url) article.
## How can I create a friendly URL or Breadcrumb navigation for my entity?
See [Friendly URL, Breadcrumb navigation](/docs/introduction/friendly-url-breadcrumb-navigation.md) article.
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,18 @@ The rest of the work is done automatically and URLs provided by these providers
- if the name of the entity will be changed, the old URLs will now redirect to your new URLs
- run `php phing friendly-urls-generate` to generate new URLs
- visit some of provided URLs and check if everything works fine

# Breadcrumb navigation

All frontend routes include breadcrumb navigation in top of content page to ease navigation for your customers across your e-commerce platform.
You might want to implement new `BreadcrumbGenerator` for your new frontend routes to tell application what navigation you want to display.

## How to create new `BreadcrumbGenerator`

- create new class with name ending with `BreadcrumbGenerator`
- this class has to implement `BreadcrumbGeneratorInterface`
- this interface requires you to implement two methods *(see `ArticleBreadcrumbGenerator` class as an example of the implementation)*:
- `getBreadcrumbItems` method that generates `BreadcrumbItems`
- these include displayed name, route and route parameters
- `getRouteNames` method where you have to provide names of routes where you want this navigation to appear
- visit some URL matching your route and check if everything works fine
16 changes: 16 additions & 0 deletions docs/upgrade/UPGRADE-v8.0.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,22 @@ There you can find links to upgrade notes for other versions too.
+ Shopsys\FrameworkBundle\Component\Router\FriendlyUrl\CompilerPass\FriendlyUrlDataProviderInterface:
+ tags: ['shopsys.friendly_url_provider']
```
- if you have extended `BreadcrumbResolverFactory` then you have to delete it, because all `BreadcrumbGenerator` classes implementing `BreadcrumbGeneratorInterface` are now automatically registered in `BreadcrumbResolver` ([#1141](https://github.com/shopsys/shopsys/pull/1140))
- update your `services.yml` accordingly:
```diff
services:
_defaults:
autowire: true
autoconfigure: true
public: false

_instanceof:
Shopsys\FrameworkBundle\Component\Router\FriendlyUrl\FriendlyUrlDataProviderInterface:
tags: ['shopsys.friendly_url_provider']
+
+ Shopsys\FrameworkBundle\Component\Breadcrumb\BreadcrumbGeneratorInterface:
+ tags: ['shopsys.breadcrumb_generator']
```

### Configuration
- simplify local configuration ([#1004](https://github.com/shopsys/shopsys/pull/1004))
Expand Down
21 changes: 15 additions & 6 deletions packages/framework/src/Component/Breadcrumb/BreadcrumbResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,27 @@ class BreadcrumbResolver
*/
protected $breadcrumbGeneratorsByRouteName;

public function __construct()
/**
* @param \Shopsys\FrameworkBundle\Component\Breadcrumb\BreadcrumbGeneratorInterface[] $breadcrumbGenerators
*/
public function __construct(iterable $breadcrumbGenerators)
{
$this->breadcrumbGeneratorsByRouteName = [];
$this->registerGenerators($breadcrumbGenerators);
}

/**
* @param \Shopsys\FrameworkBundle\Component\Breadcrumb\BreadcrumbGeneratorInterface $breadcrumbGenerator
* @param \Shopsys\FrameworkBundle\Component\Breadcrumb\BreadcrumbGeneratorInterface[] $breadcrumbGenerators
*/
public function registerGenerator(BreadcrumbGeneratorInterface $breadcrumbGenerator)
protected function registerGenerators(iterable $breadcrumbGenerators)
{
foreach ($breadcrumbGenerator->getRouteNames() as $routeName) {
$this->breadcrumbGeneratorsByRouteName[$routeName] = $breadcrumbGenerator;
foreach ($breadcrumbGenerators as $breadcrumbGenerator) {
if (!$breadcrumbGenerator instanceof BreadcrumbGeneratorInterface) {
throw new \Shopsys\FrameworkBundle\Component\Breadcrumb\Exception\BreadcrumbGeneratorMustImplementBreadcrumbGeneratorInterfaceException();
}

foreach ($breadcrumbGenerator->getRouteNames() as $routeName) {
$this->breadcrumbGeneratorsByRouteName[$routeName] = $breadcrumbGenerator;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Shopsys\FrameworkBundle\Component\Breadcrumb\Exception;

use Exception;

class BreadcrumbGeneratorMustImplementBreadcrumbGeneratorInterfaceException extends Exception implements BreadcrumbException
{
public function __construct()
{
parent::__construct('BreadcrumbGenerator must implement BreadcrumbGeneratorInterface');
}
}

This file was deleted.

5 changes: 4 additions & 1 deletion packages/framework/src/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ services:
Shopsys\FrameworkBundle\Component\Router\FriendlyUrl\FriendlyUrlDataProviderInterface:
tags: ['shopsys.friendly_url_provider']

Shopsys\FrameworkBundle\Component\Breadcrumb\BreadcrumbGeneratorInterface:
tags: ['shopsys.breadcrumb_generator']

Shopsys\FrameworkBundle\:
resource: '../../**/*{Calculation,Facade,Factory,Generator,Handler,InlineEdit,Listener,Loader,Mapper,Parser,Provider,Recalculator,Registry,Repository,Resolver,Service,Scheduler,Subscriber,Transformer}.php'
exclude: '../../{Command,Controller,DependencyInjection,Form,Migrations,Resources,Twig}'
Expand Down Expand Up @@ -135,7 +138,7 @@ services:
Shopsys\FrameworkBundle\Model\Module\ModuleList: ~

Shopsys\FrameworkBundle\Component\Breadcrumb\BreadcrumbResolver:
factory: ['@Shopsys\FrameworkBundle\Model\Breadcrumb\FrontBreadcrumbResolverFactory', create]
arguments: [!tagged shopsys.breadcrumb_generator]

Shopsys\FrameworkBundle\Model\Cart\CartMigrationFacade:
tags:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ services:
Shopsys\FrameworkBundle\Component\Router\FriendlyUrl\FriendlyUrlDataProviderInterface:
tags: ['shopsys.friendly_url_provider']

Shopsys\FrameworkBundle\Component\Breadcrumb\BreadcrumbGeneratorInterface:
tags: ['shopsys.breadcrumb_generator']

Shopsys\ShopBundle\Controller\:
resource: '../../Controller/'
public: true
Expand Down

0 comments on commit 661632f

Please sign in to comment.