Skip to content

Commit f8661ef

Browse files
committed
docs, middleware, license
1 parent c8ffd47 commit f8661ef

File tree

9 files changed

+147
-10
lines changed

9 files changed

+147
-10
lines changed

LICENSE.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
MIT License
2+
3+
Copyright (c) 2021 Kamal Pandey <kml.pandey77@gmail.com>
4+
5+
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:
6+
7+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8+
9+
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.
10+

README.md

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,79 @@
11
# Laravel Redirection
2-
This package design for redirect 404 page to new links e.g. `example.com/old-link` to `example.com/new-link`
2+
3+
This package design for redirects missing (404) URLs to new URLs.
4+
5+
When redesigning an old site into a new one your URLs may change. If your old site URLs were popular you probably want to retain your SEO worth. One way of doing this is by providing permanent redirects from your old URLs to your new URLs.
6+
7+
A package for handling redirects via database.
8+
9+
10+
### Example
11+
From `example.com/non-existing-page` to `example.com/existing-page`
12+
From `example.com/old-page` to `example.com/new-page`
13+
From `example.com/old/about-us` to `example.com/new/about-us`
14+
15+
16+
## Installation
17+
You can install the package via composer.
18+
19+
`composer require kmlpandey77/laravel-redirection`
20+
21+
The package will automatically register itself.
22+
23+
Next you must register the `\Kmlpandey77\Redirection\Http\Middleware\Redirector` middleware:
24+
25+
26+
```php
27+
// app/Http/Kernel.php
28+
29+
protected $middleware = [
30+
...
31+
\Kmlpandey77\Redirection\Http\Middleware\Redirector::class,
32+
];
33+
```
34+
35+
Publish the config file with:
36+
37+
`php artisan vendor:publish --provider="Kmlpandey77\Redirection\RedirectionServiceProvider" --tag="config"`
38+
39+
You can create the `redirections` table by running:
40+
41+
php artisan migrate
42+
43+
## Usage
44+
You just have to add an entry to the redirects links in the database by using `example.com\admin\redirects`. And you can change route link, prifix, and middleware in config
45+
46+
```php
47+
48+
return [
49+
/**
50+
* By defult route is `admin`
51+
*
52+
*/
53+
'prefix' => 'admin',
54+
55+
/**
56+
* Defining middleware for route
57+
*/
58+
59+
'middleware' => [
60+
'web',
61+
// 'auth' // or admin
62+
],
63+
64+
/**
65+
*
66+
*/
67+
'route_link' => 'redirects'
68+
];
69+
70+
```
71+
72+
## Changelog
73+
74+
## Credits
75+
76+
## License
77+
78+
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
79+

config/redirection.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,23 @@
11
<?php
22

33
return [
4+
/**
5+
* By defult route is `admin`
6+
*
7+
*/
48
'prefix' => 'admin',
5-
'middleware' => ['web'],
9+
10+
/**
11+
* Defining middleware for route
12+
*/
13+
14+
'middleware' => [
15+
'web',
16+
// 'auth' // or admin
17+
],
18+
19+
/**
20+
*
21+
*/
22+
'route_link' => 'redirects'
623
];

resources/views/index.blade.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
<h1>Redirections</h1>
22

33
@forelse ($redirections as $key=>$redirect)
4-
<form action="{{ route('redirection.destroy', $redirect->id) }}" method="POST" onsubmit="confirm('Are you sure?');">
4+
<form action="{{ route(config('redirection.route_link').'.destroy', $redirect->id) }}" method="POST" onsubmit="confirm('Are you sure?');">
55
{!! method_field('DELETE') !!}
66
{!! csrf_field() !!}
7-
<a href="{{ route('redirection.edit', $redirect) }}">Edit </a> |
7+
<a href="{{ route(config('redirection.route_link').'.edit', $redirect) }}">Edit </a> |
88
<button type="submit">Delete</button>
99
{{ $key+1 }}. {{ $redirect->from_url }} => {{ $redirect->to_url }}
1010
</form>
1111
@empty
1212
<p>No records found</p>
1313
@endforelse
1414

15-
<a href="{{ route('redirection.create') }}">Create</a>
15+
<a href="{{ route(config('redirection.route_link').'.create') }}">Create</a>
1616

routes/web.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
use Illuminate\Support\Facades\Route;
44
use Kmlpandey77\Redirection\Http\Controllers\RedirectionController;
55

6-
Route::resource('redirection', RedirectionController::class);
6+
Route::resource(config('redirection.route_link'), RedirectionController::class);

src/Http/Controllers/RedirectionController.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function store(Request $request)
5050
$redirection->to_url = ltrim($to_url, '/');
5151
$redirection->save();
5252

53-
return redirect()->route('redirection.index')->with('success', 'Redirect added.');
53+
return redirect()->route(config('redirection.route_link') .'.index')->with('success', 'Redirect added.');
5454
}
5555

5656
/**
@@ -91,7 +91,7 @@ public function update(Request $request, Redirection $redirection)
9191
$redirection->to_url = ltrim($to_url, '/');
9292
$redirection->save();
9393

94-
return redirect()->route('redirection.index')->with('success', 'Redirect saved.');
94+
return redirect()->route(config('redirection.route_link') .'.index')->with('success', 'Redirect saved.');
9595
}
9696

9797
/**
@@ -103,6 +103,6 @@ public function destroy(Redirection $redirection)
103103
{
104104
$redirection->delete();
105105

106-
return redirect()->route('redirection.index')->with('success', 'Redirect deleted.');
106+
return redirect()->route(config('redirection.route_link') .'.index')->with('success', 'Redirect deleted.');
107107
}
108108
}

src/Http/Middleware/Redirector.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Kmlpandey77\Redirection\Http\Middleware;
4+
5+
use Closure;
6+
use Illuminate\Http\Request;
7+
use Kmlpandey77\Redirection\Facades\Redirection;
8+
9+
class Redirector
10+
{
11+
/**
12+
* Handle an incoming request.
13+
*
14+
* @param \Illuminate\Http\Request $request
15+
* @param \Closure $next
16+
* @return mixed
17+
*/
18+
public function handle(Request $request, Closure $next)
19+
{
20+
21+
if($redirect_path = Redirection::check($request->path()))
22+
return redirect($redirect_path);
23+
24+
return $next($request);
25+
}
26+
}

src/Redirection.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ public function check($url)
1010
{
1111
$redirect = RedirectionModel::where('from_url',$url)->first();
1212
if($redirect){
13-
return redirect($redirect->to_url);
13+
return $redirect->to_url;
1414
}
15+
16+
return false;
1517
}
1618
}

src/RedirectionServiceProvider.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ public function boot()
1616
$this->publishes([
1717
__DIR__.'/../config/redirection.php' => config_path('redirection.php'),
1818
], 'config');
19+
20+
$this->publishes([
21+
__DIR__.'/../resources/views' => resource_path('views/vendor/redirection'),
22+
], 'views');
23+
1924
}
2025

2126
public function register()

0 commit comments

Comments
 (0)