|
1 | 1 | <?php |
2 | 2 |
|
3 | 3 | namespace Kmlpandey77\Redirection\Http\Controllers; |
| 4 | + |
| 5 | +use Illuminate\Http\Request; |
4 | 6 | 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 | + } |
5 | 96 |
|
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(); |
7 | 105 |
|
| 106 | + return redirect()->route('redirection.index')->with('success', 'Redirect deleted.'); |
| 107 | + } |
8 | 108 | } |
0 commit comments