freeq/redirector

Manage your redirections easily.

1.0.1 2017-08-22 08:05 UTC

This package is not auto-updated.

Last update: 2024-04-14 03:22:57 UTC


README

Redirect manager

Build Status Total Downloads Latest Stable Version

This package provides you with a simple tool to redirect users. You can easily manage everything.

Installation

Via Composer

$ composer require freeq/redirector

Using

Use package everywhere you need. The best way to manage your redirections is to run it in Observer or other event listener. You should store redirections after saving to the database, drop them from store after removing database record. Every redirect object should implement Redirectable contract.

Create manager

Make instance of your redirections manager. It allows you to store/delete/flush/get data.

use Freeq\Redirector\Manager;

$redirect_object = Redirect::findById(1); // must implement Redirectable!

// Using file driver.
$storage = new FileStorage('/path/to/store/them');
$storage->setRedirect($redirect_object);
$manager = new Manager($storage);

// Using redis driver.
$storage = new RedisStorage(new Predis\Client());
$storage->setRedirect($redirect_object);
$manager = new Manager($storage);

// Otherwise u can use StorageFactory.
// $type is storage name (file or redis)
// $source is path for FileStorage or Predis Client for RedisStorage
$storage = StorageFactory::build($type, $source);
$storage->setRedirect($redirect_object);
$manager = new Manager($storage);

Manage Redirectable object

Store object

$manager->store();

Drop it

$manager->delete();

Flush everything

$manager->flush();

Start redirection for given route

$manager->forward('my_route');

Classes

  • Freeq\Redirector\Manager - allows you to manage.
  • Freeq\Redirector\StorageFactory - is creating concrete class depending of passed 'type' to the manager constructor.
  • Freeq\Redirector\Storages\FileStorage - driver which allows you to store redirections as mini files.
  • Freeq\Redirector\Storages\RedisStorage - driver which allows you to store redirections as redis records.

Redirectable methods

Your object which implements Freeq\Redirector\Contracts\Redirectable needs methods:

  • routeFrom() - which route should be redirected.
  • routeTo() - where did you want to go by your redirection (eg. https://github.com).
  • statusHttp() - redirect using specific http statuses (301 or 302).
  • hash() - hash is string similar to hashed routeFrom() by md5. It's redis key or filename.
  • expireAt() - returns date to expire your redirection.

Contributing

Please see contributing.md for details.