GithubHelp home page GithubHelp logo

thdebay / laravel-redirects Goto Github PK

View Code? Open in Web Editor NEW

This project forked from esign/laravel-redirects

0.0 0.0 0.0 38 KB

A laravel package to control redirects from the database or other sources.

License: MIT License

PHP 100.00%

laravel-redirects's Introduction

Control redirects from the database

Latest Version on Packagist Total Downloads Github Workflow Status

This package provides an easy way to load redirects from the database instead of defining them in your route files. By default the package will only load your redirects when the original request results in a 404.

Installation

You can install the package via composer:

composer require esign/laravel-redirects

The package will automatically register a service provider.

For the redirects to be active you must register the Esign\Redirects\Http\Middleware\CheckForRedirects middleware.

Laravel 11+

// app/Http/Kernel.php

return Application::configure(basePath: dirname(__DIR__))
    ...
    ->withMiddleware(function (Middleware $middleware) {
        $middleware->web(
            prepend: [
                ...
                Esign\Redirects\Http\Middleware\CheckForRedirects::class,
            ],
        );
    })
    })->create();

Older versions (up to Laravel 10)

// bootstrap/app.php

protected $middleware = [
    ...
    Esign\Redirects\Http\Middleware\CheckForRedirects::class,
];

This package comes with a migration to store your redirects. In case you want to modify this migration you may publish it using:

php artisan vendor:publish --provider="Esign\Redirects\RedirectsServiceProvider" --tag="migrations"

Next up, you can publish the configuration file:

php artisan vendor:publish --provider="Esign\Redirects\RedirectsServiceProvider" --tag="config"

The config file will be published as config/redirects.php with the following content:

return [
    /**
     * This is the model used by the DatabaseRedirector.
     * It should implement the RedirectContract interface and extend the Model class.
     */
    'redirect_model' => Esign\Redirects\Models\Redirect::class,

    /**
     * This class provides the redicect url's to the CheckForRedirects middleware.
     * It should implement the RedirectorContract interface.
     */
    'redirector' => Esign\Redirects\Redirectors\DatabaseRedirector::class,

    /**
     * The key that will be used to cache the redirects.
     */
    'cache_key' => 'redirects',

    /**
     * The amount of seconds the redirects will be cached for.
     */
    'cache_remember' => 15,
];

Usage

Defining redirects in the database is pretty straight forward:

Redirect::create([
    'old_url' => 'my-old-url',
    'new_url' => 'my-new-url',
]);

It's also possible to define route parameters just like the way you're used to in Laravel:

Redirect::create([
    'old_url' => 'my-old-url/{slug}',
    'new_url' => 'my-new-url/{slug}',
]);

When using route parameters, the following parameters are reserved by Laravel and cannot be used: destination and status.

You may even swap the order of the route parameters

Redirect::create([
    'old_url' => 'my-old-url/{slug}/{year}',
    'new_url' => 'my-new-url/{year}/{slug}',
]);

By default a 302 status will be used, but you can also supply a custom status code

Redirect::create([
    'old_url' => 'my-old-url/{slug}/{year}',
    'new_url' => 'my-new-url/{year}/{slug}',
    'status_code' => 301,
]);

It's also possible to redirect to external urls

Redirect::create([
    'old_url' => 'my-old-url',
    'new_url' => 'https://www.esign.eu',
]);

This package also allows you to define constraints for your routes:

Redirect::create([
    'old_url' => 'user/{id}',
    'new_url' => 'users/{id}',
    'constraints' => ['id' => '[0-9]+'],
]);

Redirect::create([
    'old_url' => 'nl/{any?}',
    'new_url' => 'nl-be/{any?}',
    'constraints' => ['any' => '.*'],
]);

This package also ships with a DatabaseWildcardRedirector, which allows you to define redirects by using * as a wildcard. This will automatically apply a constraint to match any trailing url segments:

Redirect::create([
    'old_url' => 'my-old-url/*',
    'new_url' => 'my-new-url/*',
]);

Testing

composer test

License

The MIT License (MIT). Please see License File for more information.

laravel-redirects's People

Contributors

jordyvanderhaegen avatar bartdecorte avatar thdebay avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.