Skip to content

Commit

Permalink
Adding the ability for users to delete hooks and add the middleware d…
Browse files Browse the repository at this point in the history
…ynamically
  • Loading branch information
tnylea committed Aug 4, 2017
1 parent 0892288 commit 23744bc
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 21 deletions.
7 changes: 6 additions & 1 deletion README.md
@@ -1,2 +1,7 @@
# voyager-redirects
A Hook that allows you to add redirects to your Laravel and Voyager App

<p align="center"><a href="https://the-control-group.github.io/voyager/" target="_blank"><img width="220" src="/cover.jpg?raw=true"></a></p>

A Hook that allows you to easily add and manage redirects to your Laravel and Voyager App

![Voyager Redirects Screenshot](http://i.imgur.com/hPg7en2.png)
Binary file added cover.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
41 changes: 38 additions & 3 deletions resources/views/browse.blade.php
Expand Up @@ -30,8 +30,8 @@
<tbody>
@foreach($redirects as $redirect)
<tr role="row" class="odd">
<td><a href="{{ $redirect->from }}" target="_blank">{{ $redirect->from }}</a></td>
<td><a href="{{ $redirect->to }}" target="_blank">{{ $redirect->to }}</a></td>
<td><a href="/{{ $redirect->from }}" target="_blank">{{ $redirect->from }}</a></td>
<td><a href="/{{ $redirect->to }}" target="_blank">{{ $redirect->to }}</a></td>
<td>{{ $redirect->type }}</td>
<td>{{ Carbon\Carbon::parse($redirect->created_at)->toDayDateTimeString() }}</td>
<td>{{ Carbon\Carbon::parse($redirect->updated_at)->toDayDateTimeString() }}</td>
Expand All @@ -54,6 +54,30 @@
</div>
</div>
</div>

<div class="modal modal-danger fade" tabindex="-1" id="delete_modal" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
aria-hidden="true">&times;</span></button>
<h4 class="modal-title"><i class="voyager-trash"></i> Are you sure you want to delete
this Redirect?</h4>
</div>
<div class="modal-footer">
<form action="{{ route('voyager.redirects.delete') }}" id="delete_form" method="POST">
{{ method_field("DELETE") }}
{{ csrf_field() }}
<input type="hidden" value="" id="delete_id" name="id">
<input type="submit" class="btn btn-danger pull-right delete-confirm"
value="Yes, delete this redirect">
</form>
<button type="button" class="btn btn-default pull-right" data-dismiss="modal">Cancel</button>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->

</div>
</div>

Expand All @@ -62,6 +86,17 @@

@section('javascript')
<script>
$('document').ready(function(){
$('td').on('click', '.delete', function (e) {
var form = $('#delete_form')[0];
$('#delete_id').val( $(this).data('id') );
$('#delete_modal').modal('show');
});
});
function filter_click(filter, sorting, filter_by){
if(filter == filter_by){
if(sorting.toLowerCase() == 'desc'){
Expand All @@ -72,7 +107,7 @@ function filter_click(filter, sorting, filter_by){
} else {
window.location = window.location.pathname + '?filter=' + filter_by;
}
}
}
</script>

@endsection
26 changes: 9 additions & 17 deletions src/Http/Controllers/VoyagerRedirectController.php
Expand Up @@ -16,7 +16,7 @@ class VoyagerRedirectController extends \App\Http\Controllers\Controller
// | |_) |
// |____/
//
// Browse the polls (B)READ
// Browse the redirects (B)READ
//
//****************************************

Expand Down Expand Up @@ -46,7 +46,7 @@ public function browse(Request $request, VoyagerRedirect $redirects){
// | | \ \
// |_| \_\
//
// Read a specific poll B(R)EAD
// Read a specific redirect B(R)EAD
//
//****************************************

Expand All @@ -61,7 +61,7 @@ public function browse(Request $request, VoyagerRedirect $redirects){
// | |____
// |______|
//
// Edit a poll BR(E)AD
// Edit a redirect BR(E)AD
//
//****************************************

Expand Down Expand Up @@ -95,7 +95,7 @@ public function edit_post(Request $request){
// /_/ \_\
//
//
// Add a new poll BRE(A)D
// Add a new redirect BRE(A)D
//
//****************************************

Expand Down Expand Up @@ -127,31 +127,23 @@ public function add_post(Request $request){
// | |__| |
// |_____/
//
// Delete a poll BREA(D)
// Delete a redirect BREA(D)
//
//****************************************

public function delete(Request $request){
$id = $request->id;
$data = Poll::destroy($id)
$data = VoyagerRedirect::destroy($id)
? [
'message' => "Successfully Deleted Poll",
'message' => "Successfully Deleted Redirect",
'alert-type' => 'success',
]
: [
'message' => "Sorry it appears there was a problem deleting this poll",
'message' => "Sorry it appears there was a problem deleting this redirect",
'alert-type' => 'error',
];

return redirect()->route("voyager.polls")->with($data);
return redirect()->route("voyager.redirects")->with($data);
}

public function redirect(Request $request){
$redirect = \App\VoyagerRedirect::where('from', '=', trim($request->path()))->first();
if(isset($redirect->to)){
return redirect($redirect->to, $redirect->type);
} else {
echo 'blah';
}
}
}
26 changes: 26 additions & 0 deletions src/Http/Middleware/VoyagerRedirectMiddleware.php
@@ -0,0 +1,26 @@
<?php

namespace Hooks\VoyagerRedirects\Http\Middleware;

use Closure;
use Hooks\VoyagerRedirects\Models\VoyagerRedirect;

class VoyagerRedirectMiddleware
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
$path = $request->path();
$redirect = VoyagerRedirect::where('from', '=', $path)->first();
if(isset($redirect->id)){
return redirect($redirect->to, $redirect->type);
}
return $next($request);
}
}
3 changes: 3 additions & 0 deletions src/VoyagerRedirectServiceProvider.php
Expand Up @@ -21,6 +21,9 @@ public function boot(\Illuminate\Routing\Router $router, Dispatcher $events)
$events->listen('voyager.menu.display', [$this, 'addRedirectMenuItem']);
$this->loadViewsFrom(base_path('hooks/voyager-redirects/resources/views'), 'voyager.redirects');
$this->loadModels();

// Add the redirect middleware that will handle all redirects
$this->app['Illuminate\Contracts\Http\Kernel']->prependMiddleware(\Hooks\VoyagerRedirects\Http\Middleware\VoyagerRedirectMiddleware::class);
}

public function addRedirectRoutes($router)
Expand Down

0 comments on commit 23744bc

Please sign in to comment.