jidoka1902/redirecting-fallbacks-symfony

Integration to the jidoka1902/redirecting-fallbacks project for symfony

Installs: 50

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 2

Forks: 0

Type:symfony-integration

v1.0.2 2018-05-18 23:37 UTC

This package is not auto-updated.

Last update: 2024-10-27 05:20:22 UTC


README

Symfony Integration for the "redirecting-fallbacks" project.

Examples

It's all about the configuration.

Basic Configuration

First of all, load the integration and library classes as services. So you can wire them as you need.

## config/services.yaml
services:
  Jidoka1902\RedirectingFallbacks\:
    resource: '%kernel.project_dir%/vendor/jidoka1902/redirecting-fallbacks/src'
  Jidoka1902\RedirectingFallbacksSymfony\:
    resource: '%kernel.project_dir%/vendor/jidoka1902/redirecting-fallbacks-symfony/src'

After that you can decide what type of routes you want to specify in your configuration files. Either plain paths like "/" and "/blog" or symfony named routes like "app_index".

For Symfony Routing - use the provided FrameworkUrlGeneratorAdapter:

## config/services.yaml
services:
  Jidoka1902\RedirectingFallbacks\UrlGenerator\UrlGenerator:
    alias: Jidoka1902\RedirectingFallbacksSymfony\UrlGenerator\FrameworkUrlGeneratorAdapter

If you prefer plain paths to redirect to use this config:

## config/services.yaml
services:
  Jidoka1902\RedirectingFallbacks\UrlGenerator\UrlGenerator:
    alias: Jidoka1902\RedirectingFallbacks\UrlGenerator\PassthroughUrlGenerator

Gloal Redirect on 404

Now that you know how to configure your prefered path-type - let's have a basic example about one redirect path for all occuring 404 Status Reasons (NotFoundHttpException or plain ResponseCode 404).

  • you have to wire the SingleRedirectResolver as alias for the RedirectResolver interface.
## config/services.yaml
services:
  Jidoka1902\RedirectingFallbacks\Resolver\RedirectResolver:
    alias: Jidoka1902\RedirectingFallbacks\Resolver\SingleRedirectResolver
  • configure the route to redirect to
## config/services.yaml
services:
  Jidoka1902\RedirectingFallbacks\Resolver\SingleRedirectResolver:
    arguments:
      $target: "/"

รจ voila! every request resulting in a 404 state will be redirected to "/"

More Precise Redirects depending on Paths

After that basic configuration, lets have a look at how to create more individual 404-redirects. E.g. if someone enters an not found blog url because of a wrong slug which was not found in your storage, should that be redirected to the start-page or do you have a fancy blog post search site? Same here:

  • link your desired RedirectResolver:
## config/services.yaml
services:
  Jidoka1902\RedirectingFallbacks\Resolver\RedirectResolver:
    alias: Jidoka1902\RedirectingFallbacks\Resolver\MultipleRedirectResolver
  • configure it:
## config/services.yaml
services:
  Jidoka1902\RedirectingFallbacks\Resolver\MultipleRedirectResolver:
    arguments:
      $mapping:
        - { path: '/blog/', target: 'app_blog_search'}
        - { path: '/', target: 'app_index'} 

But beware! the order of your mappings plays a role.