Code Monkey home page Code Monkey logo

laravel-redirection's Introduction

Laravel package for manage your URL redirects in database or other sources to get better SEO results

Latest Version on Packagist run-tests Check & fix styling Total Downloads

Requirements

You need PHP 8.0 or higher. It is tested and designed for Laravel 9 and 10. This package will receive updates for future Laravel versions. Previous Laravel versions are not contemplated so use Neurony/laravel-redirects package for older Laravel versions.

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"

This is the contents of the published config file:

<?php

return [
    /*
    |--------------------------------------------------------------------------
    | Status codes valid for redirections
    |--------------------------------------------------------------------------
    |
    | The redirect statuses that you will use in your application.
    | By default, the "301", "302" and "307" are defined.
    |
    */
    'statuses' => [
        301 => 'Permanent (301)',
        302 => 'Normal (302)',
        307 => 'Temporary (307)',
    ],

    /*
    |--------------------------------------------------------------------------
    | Default Redirect status code (in case of not defined)
    |--------------------------------------------------------------------------
    |
    | Status code used by default to redirect from an old URL to a new mapped
    | URL.
    |
    */
    'default_status_code' => (int)env('REDIRECT_DEFAULT_STATUS', 301),

    /*
    |--------------------------------------------------------------------------
    | Case sensitivity
    |--------------------------------------------------------------------------
    |
    | Whether to match URLs case sensitively or not.
    | Default to false because most URLs are not case sensitive.
    |
    */
    'case-sensitive' => (bool) env('REDIRECT_CASE_SENSITIVE', false),

    /*
    |--------------------------------------------------------------------------
    | Redirect Driver
    |--------------------------------------------------------------------------
    |
    | Here you may specify the default redirect driver that you want to use.
    | The "config" driver is used by default when you want to code faster.
    | Consider database driver better for admin panel configuration backed by
    | a relational DB.
    |
    */
    'driver' => env('REDIRECT_DRIVER', 'config'),

    /*
    |--------------------------------------------------------------------------
    | Array containing all available drivers and its implementations and source
    |--------------------------------------------------------------------------
    |
    | Concrete implementation for the "redirection model".
    | To extend or replace this functionality, change the value below with
    | your full "redirection model" FQN.
    |
    | Your class will have to (first option is recommended):
    | - extend the "SiroDiaz\Redirection\Models\Redirection" class
    | - or at least implement the "SiroDiaz\Redirection\Contracts\RedirectionModelContract" interface.
    |
    | Regardless of the concrete implementation below, you can still use it like:
    | - app('redirection.') OR app('\SiroDiaz\Redirection\Contracts\RedirectionModelContract')
    | - or you could even use your own class as a direct implementation. For this
    | case you must extend from "SiroDiaz\Redirection\Models\Redirection" model class and
    | replace in the published config file 'drivers.database.source'.
    |
    |
    */
    'drivers' => [
        'config' => [
            'driver' => SiroDiaz\Redirection\Drivers\FileRedirector::class,
            'source' => 'redirection.urls',
        ],
        'database' => [
            'driver' => SiroDiaz\Redirection\Drivers\DatabaseRedirector::class,
            'source' => SiroDiaz\Redirection\Models\Redirection::class,
        ],
    ],

    /*
    |--------------------------------------------------------------------------
    | Url list with redirections used for config driver
    |--------------------------------------------------------------------------
    |
    | You can use urls array of two different ways. The simple one uses the
    | default redirect status code ('redirection.default_status_code').
    | Example:
    | 'urls' => [
    |   '/old/url' => '/new/url',
    |   '/another/old/url' => '/another/new/url',
    |   '/url/with?id=123' => '/url/with/123',
    | ],
    |
    | The second way to write redirect urls in your config/redirection.php
    | is using associative arrays. You can combine this method with the previous one.
    | Look at this example:
    | 'urls' => [
    |   '/old/url' => ['new_url' => '/new/url', 'status_code' => 302],
    |   '/another/old/url' => '/another/new/url',
    |   '/url/with?id=123' => ['new_url' => '/url/with/123'],
    | ],
    |
    */
    'urls' => [],

];

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

Here is a basic example of how to extend the functionality of the default Redirection model. We want to include support for 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 purposes, add them to your config/redirection.php config file. By default, you have the most common redirections codes: 301, 302 and 307.

Ey! 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,
    ],

Extending and creating new redirect Drivers

TODO

Testing this package for contribution

composer test

Changelog

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

Contributing

Please see CONTRIBUTING for details.

Credits

License

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

laravel-redirection's People

Contributors

dependabot[bot] avatar macbookandrew avatar sirodiaz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

laravel-redirection's Issues

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.