Permalink
Browse files

Initial commit (cloned from movor/laravel-db-redirector v0.3.1)

  • Loading branch information...
vkovic committed Dec 5, 2018
0 parents commit c1b8005fad30bd95b886964aff5071d023403a21
@@ -0,0 +1,25 @@
#
# OS generated files and junk
#

.DS_Store
.DS_Store?
._*
Thumbs.db
Icon?
.Trashes
ehthumbs.db
*.log

#
# PhpStorm
#

.idea

#
# Project
#

vendor
composer.lock
@@ -0,0 +1,14 @@
language: php

php:
- 7.0
- 7.1
- 7.2

before_script:
- composer self-update
- composer install --prefer-source --no-interaction
- composer dump-autoload

script:
- vendor/bin/phpunit
21 LICENCE
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2018 Vladimir Ković

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
191 README.md
@@ -0,0 +1,191 @@
# Laravel DB redirector

[![Build](https://travis-ci.org/vkovic/laravel-db-redirector.svg?branch=master)](https://travis-ci.org/vkovic/laravel-db-redirector)
[![Downloads](https://poser.pugx.org/vkovic/laravel-db-redirector/downloads)](https://packagist.org/packages/vkovic/laravel-db-redirector)
[![Stable](https://poser.pugx.org/vkovic/laravel-db-redirector/v/stable)](https://packagist.org/packages/vkovic/laravel-db-redirector)
[![License](https://poser.pugx.org/vkovic/laravel-db-redirector/license)](https://packagist.org/packages/vkovic/laravel-db-redirector)

### Manage HTTP redirections in Laravel using database

Manage redirects using database rules. Rules are intended to be very similar to Laravel default routes, so syntax is pretty easy
to comprehend.

---

## Compatibility

The package is compatible with Laravel versions `>= 5.1`

## Installation

Install the package via composer:

```bash
composer require vkovic/laravel-db-redirector
```

The package needs to be registered in service providers:

```php
// File: config/app.php
// ...
/*
* Package Service Providers...
*/
// ...
Vkovic\LaravelDbRedirector\Providers\DbRedirectorServiceProvider::class,
```

Database redirector middleware needs to be added to middleware array:

```php
// File: app/Http/Kernel.php
// ...
protected $middleware = [
// ...
\Vkovic\LaravelDbRedirector\Http\Middleware\DbRedirectorMiddleware::class
];
```

Run migrations to create table which will store redirect rules:

```bash
php artisan migrate
```

## Usage: Simple Examples

Creating a redirect is easy. You just have to add db record via provided RedirectRule model.
Default status code for redirections will be 301 (Moved Permanently).

```php
use Vkovic\LaravelDbRedirector\Models\RedirectRule;
// ...
RedirectRule::create([
'origin' => '/one/two',
'destination' => '/three'
]);
```

You can also specify another redirection status code:

```php
RedirectRule::create([
'origin' => '/one/two',
'destination' => '/three',
'status_code' => 307 // Temporary Redirect
]);
```

You may use route parameters just like in native Laravel routes,
they'll be passed down the road - from origin to destination:

```php
RedirectRule::create([
'origin' => '/one/{param}',
'destination' => '/two/{param}'
]);
// If we visit: "/one/foo" we will end up at "two/foo"
```

Optional parameters can also be used:

```php
RedirectRule::create([
'origin' => '/one/{param1?}/{param2?}',
'destination' => '/four/{param1}/{param2}'
]);
// If we visit: "/one" we'll end up at "/four
// If we visit: "/one/two" we'll end up at "/four/two"
// If we visit: "/one/two/three" we'll end up at "/four/two/three"
```

Chained redirects will also work:

```php
RedirectRule::create([
'origin' => '/one',
'destination' => '/two'
]);
RedirectRule::create([
'origin' => '/two',
'destination' => '/three'
]);
RedirectRule::create([
'origin' => '/three',
'destination' => '/four'
]);
// If we visit: "/one" we'll end up at "/four"
```

We also can delete the whole chain at once
(3 previous redirect records in this example):

```php
RedirectRule::deleteChainedRecursively('/four');
```

Sometimes it's possible that you'll have more than one redirection with
the same destination. So it's smart to surround code with try catch block, because exception
will be raised in this case:

```php
RedirectRule::create(['origin' => '/one/two', 'destination' => '/three/four']);
RedirectRule::create(['origin' => '/three/four', 'destination' => '/five/six']);
// One more with same destination ("/five/six") as the previous one.
RedirectRule::create(['origin' => '/ten/eleven', 'destination' => '/five/six']);
try {
RedirectRule::deleteChainedRecursively('five/six');
} catch (\Exception $e) {
// ... handle exception
}
```

## Usage: Advanced

What about order of rules execution when given url corresponds to multiple rules.
Let's find out in this simple example:

```php
RedirectRule::create(['origin' => '/one/{param}/three', 'destination' => '/four']);
RedirectRule::create(['origin' => '/{param}/two/three', 'destination' => '/five']);
// If we visit: "/one/two/three" it corresponds to both of rules above,
// so, where should we end up: "/four" or "/five" ?
// ...
// It does not have anything to do with rule order in our rules table!
```

To solve this problem, we need to agree on simple (and logical) rule prioritizing:

**Priority 1:**
Rules without named parameters have top priority:

**Priority 2:**
If rule origin have named parameters, those with less named parameters will have higher priority

**Priority 3:**
If rule origin have same number of named parameters, those where named parameters are nearer the
end of the rule string will have priority

So lets examine our previous case, we have:
- "/one/{param}/three" => "/four"
- "/{param}/two/three" => "/five"

In this case both rules have the same number of named params, but in the first rule "{param}" is
nearer the end of the rule, so it will have priority and we'll end up at "/four".
@@ -0,0 +1,35 @@
{
"name": "vkovic/laravel-db-redirector",
"description": "Manage Laravel HTTP redirections using database",
"keywords": ["laravel", "redirect", "database"],
"license": "MIT",
"authors": [
{
"name": "Vladimir Ković",
"email": "vlada.kovic@gmail.com"
}
],
"autoload": {
"psr-4": {
"Vkovic\\LaravelDbRedirector\\": "src/package"
}
},
"autoload-dev": {
"psr-4": {
"Vkovic\\LaravelDbRedirector\\Test\\": "tests"
}
},
"require": {
"php": "^7.0",
"laravel/framework": "5.5.*|5.6.*|5.7.*"
},
"require-dev": {
"orchestra/testbench": "3.5.*|3.6.*|3.7.*",
"orchestra/database": "3.5.*|3.6.*|3.7.*",
"phpunit/phpunit": "^6.3|^7.0"
},
"scripts": {
"test": "vendor/bin/phpunit"
},
"minimumu-stability": "dev"
}
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="vendor/autoload.php"
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
verbose="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="LaravelDbRedirector Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">src/</directory>
</whitelist>
</filter>
</phpunit>
@@ -0,0 +1,35 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateRedirectRulesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('redirect_rules', function (Blueprint $table) {
$table->increments('id');
$table->string('origin', 512)->unique();
$table->text('destination');
$table->unsignedSmallInteger('status_code')->default(301);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('redirect_rules');
}
}
Oops, something went wrong.

0 comments on commit c1b8005

Please sign in to comment.