zenstruck/redirect-bundle

Store redirects for your site and keeps statistics on redirects and 404 errors

Installs: 1 094

Dependents: 0

Stars: 10

Watchers: 2

Forks: 2

Open Issues: 2

Language: PHP

v2.0.0 2015-06-08 15:56 UTC

README

Build Status Scrutinizer Code Quality Code Coverage SensioLabs Insight Latest Stable Version License

This bundle adds a database table that stores redirects for your site. 404 exceptions are intercepted and the requested uri is looked up. If a match is found it redirects to the found redirects destination. The count and last accessed date are stored as well.

In addition, 404 errors are logged as well. Their count and last accessed date will also be stored. This can be useful for determining bad links.

Installation

  1. Install with composer:

    $ composer require zenstruck/redirect-bundle
    
  2. Enable the bundle in the kernel:

    // app/AppKernel.php
    
    public function registerBundles()
    {
        $bundles = array(
            // ...
            new Zenstruck\RedirectBundle\ZenstruckRedirectBundle(),
        );
    }
  3. Create your redirect class inheriting the MappedSuperClass this bundle provides:

    namespace Acme\DemoBundle\Entity;
    
    use Zenstruck\RedirectBundle\Model\Redirect as BaseRedirect;
    use Doctrine\ORM\Mapping as ORM;
    
    /**
     * @ORM\Entity
     * @ORM\Table(name="redirects")
     */
    class Redirect extends BaseRedirect
    {
        /**
         * @ORM\Id
         * @ORM\Column(type="integer")
         * @ORM\GeneratedValue(strategy="AUTO")
         */
        private $id;
    }
  4. Set this class in your config.yml:

    zenstruck_redirect:
        redirect_class: Acme\DemoBundle\Entity\Redirect
  5. Update your schema:

    $ app/console doctrine:schema:update --force
    

Form Type

This bundle provides a form type (zenstruck_redirect) for creating/editing redirects.

$redirect = // ...
$form = $this->createForm('zenstruck_redirect', $redirect);

By default, a redirect with a destination has a status code of 301, to give the option to set a 302 status code a status code choice field can be enabled when creating the form:

$redirect = // ...
$form = $this->createForm('zenstruck_redirect', $redirect, array('status_code' => true));

You may want to disable the source field for already created redirects:

// new action
$redirect = new Redirect();
$form = $this->createForm('zenstruck_redirect', $redirect);

// edit action
$redirect = // get from database
$form = $this->createForm('zenstruck_redirect', $redirect, array('disable_source' => true));

Full Default Configuration

zenstruck_redirect:
    redirect_class:     ~ # Required
    model_manager_name: ~