Skip to content

Commit fe7f1c7

Browse files
committed
config,views, controller
1 parent 0e1fb5a commit fe7f1c7

File tree

11 files changed

+181
-20
lines changed

11 files changed

+181
-20
lines changed

composer.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
{
22
"name": "kmlpandey77/laravel-redirection",
33
"description": "This package design for redirect 404 page to new link",
4+
"type": "library",
5+
"license": "MIT",
46
"authors": [
57
{
68
"name": "Kamal Pandey",
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
2+
23
return [
34
'prefix' => 'admin',
4-
'middleware' => ['web', 'auth']
5+
'middleware' => ['web'],
56
];

database/migrations/2021_01_28_114124_create_redirections_table.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public function up()
1515
{
1616
Schema::create('redirections', function (Blueprint $table) {
1717
$table->id();
18-
$table->text('form_url');
18+
$table->text('from_url');
1919
$table->text('to_url');
2020
$table->timestamps();
2121
});

resources/views/create.blade.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<h1>Redirections</h1>
2+
3+
<form action="{{ route('redirection.store') }}" accept-charset="utf-8" method="post">
4+
{!! csrf_field() !!}
5+
@include('redirection::form')
6+
7+
<button type="submit">Save</button>
8+
9+
</form>
10+
11+
<a href="{{ route('redirection.index') }}">Back</a>

resources/views/form.blade.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
@if ($errors->any())
2+
<div class="alert alert-danger">
3+
<ul>
4+
@foreach ($errors->all() as $error)
5+
<li>{{ $error }}</li>
6+
@endforeach
7+
</ul>
8+
</div>
9+
@endif
10+
11+
<p>
12+
<b>From url</b> <br>
13+
<input name="from_url" value="{{ old('from_url', isset($redirect) ? $redirect->from_url : null) }}" type="text">
14+
</p>
15+
<p>
16+
<b>To url</b> <br>
17+
<input name="to_url" value="{{ old('to_url', isset($redirect) ? $redirect->to_url : null) }}" type="text">
18+
</p>

resources/views/index.blade.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<h1>Redirections</h1>
2+
3+
@forelse ($redirections as $key=>$redirect)
4+
<form action="{{ route('redirection.destroy', $redirect->id) }}" method="POST" onsubmit="confirm('Are you sure?');">
5+
@method('DELETE')
6+
@csrf
7+
<a href="{{ route('redirection.edit', $redirect) }}">Edit </a> |
8+
<button type="submit">Delete</button>
9+
{{-- <a href="#" onclick="confirmMessage('Are you sure?');return false">Delete </a> &nbsp; --}}
10+
{{ $key+1 }}. {{ $redirect->from_url }} => {{ $redirect->to_url }}
11+
</form>
12+
@empty
13+
<p>No records found</p>
14+
@endforelse
15+
16+
<a href="{{ route('redirection.create') }}">Create</a>
17+
18+
{{-- <script>
19+
function confirmMessage(msg) {
20+
if (confirm(msg)) {
21+
var form = document.getElementById('deleteform');
22+
form.submit()
23+
}
24+
}
25+
</script> --}}
26+

routes/web.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
<?php
22

33
use Illuminate\Support\Facades\Route;
4-
54
use Kmlpandey77\Redirection\Http\Controllers\RedirectionController;
65

7-
Route::resource('redirection', 'RedirectionController');
8-
6+
Route::resource('redirection', RedirectionController::class);

src/Facades/Redirection.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
namespace Kmlpandey77\Redirection\Facades;
34

45
use Illuminate\Support\Facades\Facade;
Lines changed: 101 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,108 @@
11
<?php
22

33
namespace Kmlpandey77\Redirection\Http\Controllers;
4+
5+
use Illuminate\Http\Request;
46
use Illuminate\Routing\Controller;
7+
use Kmlpandey77\Redirection\Models\Redirection;
8+
9+
class RedirectionController extends Controller
10+
{
11+
/**
12+
* Display a listing of the resource.
13+
*
14+
* @return \Illuminate\Http\Response
15+
*/
16+
public function index()
17+
{
18+
$redirections = Redirection::paginate(10);
19+
20+
return view('redirection::index', compact('redirections'));
21+
}
22+
23+
/**
24+
* Show the form for creating a new resource.
25+
*
26+
* @return \Illuminate\Http\Response
27+
*/
28+
public function create()
29+
{
30+
return view('redirection::create');
31+
}
32+
33+
/**
34+
* Store a newly created resource in storage.
35+
*
36+
* @return \Illuminate\Http\Response
37+
*/
38+
public function store(Request $request)
39+
{
40+
$request->validate([
41+
'from_url' => 'required|unique:redirections',
42+
'to_url' => 'required',
43+
]);
44+
45+
$from_url = parse_url($request->from_url, PHP_URL_PATH);
46+
$to_url = parse_url($request->to_url, PHP_URL_PATH);
47+
48+
$redirection = new Redirection();
49+
$redirection->from_url = ltrim($from_url, '/');
50+
$redirection->to_url = ltrim($to_url, '/');
51+
$redirection->save();
52+
53+
return redirect()->route('redirection.index')->with('success', 'Redirect added.');
54+
}
55+
56+
/**
57+
* Display the specified resource.
58+
*
59+
* @return \Illuminate\Http\Response
60+
*/
61+
public function show(Redirection $redirection)
62+
{
63+
}
64+
65+
/**
66+
* Show the form for editing the specified resource.
67+
*
68+
* @return \Illuminate\Http\Response
69+
*/
70+
public function edit(Redirection $redirection)
71+
{
72+
return view('admin.redirect.edit', compact('redirect'));
73+
}
74+
75+
/**
76+
* Update the specified resource in storage.
77+
*
78+
* @return \Illuminate\Http\Response
79+
*/
80+
public function update(Request $request, Redirection $redirection)
81+
{
82+
$this->validate($request, [
83+
'from_url' => 'required',
84+
'to_url' => 'required',
85+
]);
86+
87+
$from_url = parse_url($request->from_url, PHP_URL_PATH);
88+
$to_url = parse_url($request->to_url, PHP_URL_PATH);
89+
90+
$redirection->from_url = ltrim($from_url, '/');
91+
$redirection->to_url = ltrim($to_url, '/');
92+
$redirection->save();
93+
94+
return redirect()->route('redirects.index')->with('success', 'Redirect saved.');
95+
}
596

6-
class RedirectionController extends Controller{
97+
/**
98+
* Remove the specified resource from storage.
99+
*
100+
* @return \Illuminate\Http\Response
101+
*/
102+
public function destroy(Redirection $redirection)
103+
{
104+
$redirection->delete();
7105

106+
return redirect()->route('redirection.index')->with('success', 'Redirect deleted.');
107+
}
8108
}

src/Models/Redirection.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
<?php
22

3-
namespace Kmlpandey77\Redirection\Model;
3+
namespace Kmlpandey77\Redirection\Models;
44

55
use Illuminate\Database\Eloquent\Model;
66

7-
87
class Redirection extends Model
98
{
10-
9+
protected $guarded = [];
1110
}

0 commit comments

Comments
 (0)