Skip to content

Commit a2bc027

Browse files
committed
Added more documentation, changed CRLF to LF and interface renamed from RedirectorBase to Redirector
1 parent 3c22ae5 commit a2bc027

File tree

8 files changed

+91
-20
lines changed

8 files changed

+91
-20
lines changed

README.md

Lines changed: 73 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,24 +55,89 @@ return [
5555

5656
/*
5757
|--------------------------------------------------------------------------
58-
| Default model used for redirections
58+
| Default Redirect status code (in case of not defined)
59+
|--------------------------------------------------------------------------
60+
|
61+
| Status code used by default to redirect from an old URL to a new mapped
62+
| URL.
63+
|
64+
*/
65+
'default_status_code' => (int)env('REDIRECT_DEFAULT_STATUS', 301),
66+
67+
/*
68+
|--------------------------------------------------------------------------
69+
| Redirect Driver
70+
|--------------------------------------------------------------------------
71+
|
72+
| Here you may specify the default redirect driver that you want to use.
73+
| The "config" driver is used by default when you want to code faster.
74+
| Consider database driver better for admin panel configuration backed by
75+
| a relational DB.
76+
|
77+
*/
78+
'driver' => env('REDIRECT_DRIVER', 'config'),
79+
80+
/*
81+
|--------------------------------------------------------------------------
82+
| Array containing all available drivers and its implementations and source
5983
|--------------------------------------------------------------------------
6084
|
6185
| Concrete implementation for the "redirection model".
62-
| To extend or replace this functionality, change the value below with your full "redirection model" FQN.
86+
| To extend or replace this functionality, change the value below with
87+
| your full "redirection model" FQN.
6388
|
6489
| Your class will have to (first option is recommended):
6590
| - extend the "SiroDiaz\Redirection\Models\Redirection" class
6691
| - or at least implement the "SiroDiaz\Redirection\Contracts\RedirectionModelContract" interface.
6792
|
6893
| Regardless of the concrete implementation below, you can still use it like:
69-
| - app('redirection.model') OR app('\SiroDiaz\Redirection\Models\Redirection\Contracts\RedirectsModelContract')
70-
| - or you could even use your own class as a direct implementation
94+
| - app('redirection.') OR app('\SiroDiaz\Redirection\Contracts\RedirectionModelContract')
95+
| - or you could even use your own class as a direct implementation. For this
96+
| case you must extend from "SiroDiaz\Redirection\Models\Redirection" model class and
97+
| replace in the published config file 'drivers.database.source'.
98+
|
7199
|
72100
*/
73-
'model' => SiroDiaz\Redirection\Models\Redirection::class,
101+
'drivers' => [
102+
'config' => [
103+
'driver' => SiroDiaz\Redirection\Drivers\FileRedirector::class,
104+
'source' => 'redirection.urls',
105+
],
106+
'database' => [
107+
'driver' => SiroDiaz\Redirection\Drivers\DatabaseRedirector::class,
108+
'source' => SiroDiaz\Redirection\Models\Redirection::class,
109+
],
110+
],
111+
112+
/*
113+
|--------------------------------------------------------------------------
114+
| Url list with redirections used for config driver
115+
|--------------------------------------------------------------------------
116+
|
117+
| You can use urls array of two different ways. The simple one uses the
118+
| default redirect status code ('redirection.default_status_code').
119+
| Example:
120+
| 'urls' => [
121+
| '/old/url' => '/new/url',
122+
| '/another/old/url' => '/another/new/url',
123+
| '/url/with?id=123' => '/url/with/123',
124+
| ],
125+
|
126+
| The second way to write redirect urls in your config/redirection.php
127+
| is using associative arrays. You can combine this method with the previous one.
128+
| Look at this example:
129+
| 'urls' => [
130+
| '/old/url' => ['new_url' => '/new/url', 'status_code' => 302],
131+
| '/another/old/url' => '/another/new/url',
132+
| '/url/with?id=123' => ['new_url' => '/url/with/123'],
133+
| ],
134+
|
135+
*/
136+
'urls' => [],
137+
74138
];
75139

140+
76141
```
77142

78143
You can change and extend the default `SiroDiaz\Redirection\Models\Redirection` model class.
@@ -111,7 +176,9 @@ Ey! **don't forget** to append the middleware `SiroDiaz\Redirection\RedirectRequ
111176
],
112177
```
113178

114-
## Testing
179+
## Extending and creating new redirect Drivers
180+
181+
## Testing this package for contribution
115182

116183
```bash
117184
composer test

config/redirection.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
|--------------------------------------------------------------------------
3434
|
3535
| Here you may specify the default redirect driver that you want to use.
36-
| The "config" driver is used by default when you want to go faster.
36+
| The "config" driver is used by default when you want to code faster.
3737
| Consider database driver better for admin panel configuration backed by
3838
| a relational DB.
3939
|
@@ -46,15 +46,19 @@
4646
|--------------------------------------------------------------------------
4747
|
4848
| Concrete implementation for the "redirection model".
49-
| To extend or replace this functionality, change the value below with your full "redirection model" FQN.
49+
| To extend or replace this functionality, change the value below with
50+
| your full "redirection model" FQN.
5051
|
5152
| Your class will have to (first option is recommended):
5253
| - extend the "SiroDiaz\Redirection\Models\Redirection" class
5354
| - or at least implement the "SiroDiaz\Redirection\Contracts\RedirectionModelContract" interface.
5455
|
5556
| Regardless of the concrete implementation below, you can still use it like:
56-
| - app('redirection.model') OR app('\SiroDiaz\Redirection\Models\Redirection\Contracts\RedirectsModelContract')
57-
| - or you could even use your own class as a direct implementation
57+
| - app('redirection.') OR app('\SiroDiaz\Redirection\Contracts\RedirectionModelContract')
58+
| - or you could even use your own class as a direct implementation. For this
59+
| case you must extend from "SiroDiaz\Redirection\Models\Redirection" model class and
60+
| replace in the published config file 'drivers.database.source'.
61+
|
5862
|
5963
*/
6064
'drivers' => [

src/Contracts/RedirectionModelContract.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,5 @@ public function syncOldRedirects(self $model, string $finalUrl): void;
4646
* @param string $path
4747
* @return RedirectionModelContract|null
4848
*/
49-
public static function findValidOrNull(string $path);
49+
public static function findValidOrNull(string $path): ?RedirectionModelContract;
5050
}

src/Contracts/RedirectorBase.php renamed to src/Contracts/Redirector.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use SiroDiaz\Redirection\Redirect;
66

7-
interface RedirectorBase
7+
interface Redirector
88
{
99
public function getRedirectFor(string $path): ?Redirect;
1010
}

src/Drivers/DatabaseRedirector.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
namespace SiroDiaz\Redirection\Drivers;
44

5-
use SiroDiaz\Redirection\Contracts\RedirectorBase;
5+
use SiroDiaz\Redirection\Contracts\Redirector;
66
use SiroDiaz\Redirection\Redirect;
77

8-
class DatabaseRedirector implements RedirectorBase
8+
class DatabaseRedirector implements Redirector
99
{
1010
/** @var string */
1111
protected string $driver;

src/Drivers/FileRedirector.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
namespace SiroDiaz\Redirection\Drivers;
44

5-
use SiroDiaz\Redirection\Contracts\RedirectorBase;
5+
use SiroDiaz\Redirection\Contracts\Redirector;
66
use SiroDiaz\Redirection\Redirect;
77

8-
class FileRedirector implements RedirectorBase
8+
class FileRedirector implements Redirector
99
{
1010
/** @var string */
1111
protected string $driver;

src/Models/Redirection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public function setOldUrlAttribute(string $value): void
9494
* @param string $value
9595
* @returns void
9696
*/
97-
public function setNewUrlAttribute(string $value)
97+
public function setNewUrlAttribute(string $value): void
9898
{
9999
$this->attributes['new_url'] = trim(parse_url($value)['path'], '/');
100100
}

src/RedirectRequests.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ class RedirectRequests
1818
*/
1919
public function handle(Request $request, Closure $next)
2020
{
21-
$driver = config('redirection.driver');
21+
$driverName = config('redirection.driver');
2222

2323
$redirectorDriverInstance = app()->make(
24-
config("redirection.drivers.{$driver}.driver"),
25-
[$driver],
24+
config("redirection.drivers.{$driverName}.driver"),
25+
[$driverName],
2626
);
2727
$redirection = $redirectorDriverInstance->getRedirectFor($request->path());
2828

0 commit comments

Comments
 (0)