pavelzanek/redirections-laravel

Manage your redirects easily.

1.0.2 2022-08-15 22:43 UTC

This package is auto-updated.

Last update: 2024-05-16 02:22:12 UTC


README

Manage your redirects easily. This package provides an easy way to integrate a redirection tool to your Laravel application.

  1. How it works
  2. Installations
  3. Optional
  4. Pruning Table
  5. Events
  6. Todos
  7. Testing
  8. Security
  9. Chnagelog
  10. License

How it works

You can manage redirects in your Laravel application. Once installed, you can create, edit, and delete redirects without the need to install additional packages.

There is a lot of consideration for application performance. The tool caches the redirect information, specifically the source url. With each request to the server, the middleware will run, which will determine whether the requested url address is not listed in the cache. If it is found here, a query will be made to the database, which will find the target url address. Meanwhile, an event is dispatched and the associated queued event listener takes care of storing the additional redirection information.

Don't forget to run php artisan queue:work, php artisan horizon or something else what you use.

It may happen that the redirection will be unused over time, or completely unused - and records will also remain in the database. For these cases, you can schedule a command that will automatically take care of cleaning the database from unused records.

Furthermore, the tool includes functions such as easy import and export of redirects, graphs of the usability of redirects, etc.

Installations

Source URLs are cached. Check if your project has cache settings configured. You can use from several prepared drivers (file, Redis, ...). Package also requires PHP 8.1 at least.

Migrations

First of all, generate and customize migration files. It can be useful, if you want to add someting (for example - some realationships).

php artisan vendor:publish --tag=redirections-migrations

Config file

php artisan vendor:publish --tag=redirections-config

Optional

Views

If you want to change design of the app, you can generate views. The tool contains several pre-made templates for different CSS frameworks. Now available:

  • Bootstrap 4
  • Bootstrap 5
  • Tailwind CSS
php artisan vendor:publish --tag=redirections-views

Translations

Also you can generate translations files.

php artisan vendor:publish --tag=redirections-translations

Pruning table with redirects

You can prune your table with redirects. There is the command for do that:

php artisan redirections:prune-database

Alternatively you can schedule this command in your kernel file (app/Console/Kernel.php):

protected function schedule(Schedule $schedule)
{
    $schedule->command('redirections:prune')->dailyAt('8:00');
}

Events

If someone uses a redirect (that is, goes to the source URL), the RedirectWasUsedEvent event is fired. So you can hook your own listener if needed.

Name Class
RedirectWasUsedEvent PavelZanek\RedirectionsLaravel\Events\RedirectWasUsedEvent
Event::listen(\PavelZanek\RedirectionsLaravel\Events\RedirectWasUsedEvent::class, function ($event) {
    dd($event->redirect);
});

Todos

  • ✅ Publish the package
  • ✅ List of redirects - fulltext
  • ✅ List of redirects - pagination
  • ✅ Disable editing Source URL
  • ✅ Factory
  • ✅ Add more tests
  • ✅ Import / Export redirects
  • ✅ Toasts / Flash Messages
  • ⬜️ Regex Support
  • ✅ Bootstrap 5 CSS Support
  • ✅ Tailwind CSS Support
  • ⬜️ More info about redirects (referer, ...)

Testing

The tool includes several tests for easier scalability.

Security

If you've found a bug regarding security please mail zanek.pavel@gmail.com instead of using the issue tracker.

Changelog

Please see CHANGELOG for more information on what has changed recently.

License

Copyright (c) Pavel Zaněk. MIT Licensed, see LICENSE for details.