Blog Detail

02

May
Manage Your URL Redirects in Database with Laravel Redirection cover image

arrow_back Manage Your URL Redirects in Database with Laravel Redirection

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.

Published at : 02-05-2022

Author : Rizwan Aslam
AUTHOR
Rizwan Aslam

I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.

Launch your project

Launch project