Manage Your URL Redirects in Database with Laravel Redirection

Manage Your URL Redirects in Database with Laravel Redirection cover image

Laravel Redirection is a package for Laravel 8 and 9 to manage URL redirections inside your Laravel application using different data sources. It allows better SEO support for your Laravel site. This package requires PHP ^7.4 or higher.

Installation

You can install the package via composer:

composer require SiroDiaz/laravel-redirection

You can publish and run the migrations with:

php artisan vendor:publish --provider="SiroDiaz\Redirection\RedirectionServiceProvider" --tag="redirection-migrations"
php artisan migrate

You can publish the config file with:

php artisan vendor:publish --provider="SiroDiaz\Redirection\RedirectionServiceProvider" --tag="redirection-config

Usage

After the installation, You can change and extend the default SiroDiaz\Redirection\Models\Redirection model class. The image that you want to add some methods or fields. You would need to create a new model class, for example, App\Models\Redirect.

Example

Here is a basic example of how to extend the functionality of the default Redirection model. We want to include support for the Laravel BackPack admin panel would be:

<?php

namespace App\Models;

use SiroDiaz\Redirection\Models\Redirection as RedirectionBaseModel;
use Backpack\CRUD\app\Models\Traits\CrudTrait;

class Redirect extends Redirection
{
    use CrudTrait;
}

Finally, you have to vendor:publish the sirodiaz/laravel-redirection config file to change the model class used now.

If you want to add more status codes for another type of redirect purpose, add them to your config/redirection.php config file. By default, you have the most common redirection codes: 301, 302, and 307. Don’t forget to append the middleware SiroDiaz\Redirection\RedirectRequests to your app/Http/Kernel.php file.

// app/Http/Kernel.php

    protected $middleware = [
        ...
        \SiroDiaz\Redirection\RedirectRequests::class,
    ],

For more details about this package, you can visit its complete documentation and source code on Github.

Written by

Rizwan Aslam

Founder and CEO of CodeBrisk, with years of experience building web applications with Laravel and leading a team of expert developers.

More about Rizwan
Book a free call
Book a call with CodeBrisk