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 d4d4d5b
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 71 deletions.
2 changes: 1 addition & 1 deletion docs/index.md
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
6 changes: 3 additions & 3 deletions docs/introduction/faq-and-common-issues.md
Expand Up @@ -22,7 +22,7 @@ For more detailed information about the Shopsys Framework, please see [Shopsys F
- [How to change the behavior of the product search on the front-end?](#how-to-change-the-behavior-of-the-product-search-on-the-front-end)
- [Why are e-mails not sent immediately but at the end of the script](#why-are-e-mails-sent-before-end-of-the-script-and-not-immediately)
- [Where does the business logic belong?](#where-does-the-business-logic-belong)
- [How can I create a friendly URL for my entity?](#how-can-i-create-a-friendly-url-for-my-entity)
- [How can I create a friendly URL or Breadcrumb navigation for my entity?](#how-can-i-create-a-friendly-url-or-breadcrumb-navigation-for-my-entity)

## What are the phing targets?
Every phing target is a task that can be executed simply by `php phing <target-name>` command.
Expand Down 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.
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 the top of the page to ease the navigation for your customers across your e-commerce site.
When adding a new page on the frontend, you need to implement new `BreadcrumbGenerator` for the new routes to tell the application how the navigation should be displayed.

## 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 the routes that you want to include in the navigation
- visit some URL matching your route and check if everything works fine
17 changes: 17 additions & 0 deletions docs/upgrade/UPGRADE-v8.0.0.md
Expand Up @@ -100,6 +100,23 @@ 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']
```
- remove all usages of `BreadcrumbResolver::registerGenerator` as it no longer exists because all `BreadcrumbGenerators` are registered automatically

### 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
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
@@ -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
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
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 d4d4d5b

Please sign in to comment.