Skip to content

Commit 360ed8d

Browse files
committed
add event for when permalinks gets accessed
1 parent e67ba27 commit 360ed8d

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

src/Controllers/ModelPermalinkController.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Illuminate\Http\RedirectResponse;
77
use Illuminate\Routing\Controller;
88
use Illuminate\Support\Facades\Response;
9+
use OpenSoutheners\LaravelModelPermalink\Events\PermalinkGotAccessed;
910

1011
class ModelPermalinkController extends Controller
1112
{
@@ -26,6 +27,8 @@ public function __invoke(string $permalink): RedirectResponse
2627

2728
$this->authorize('viewModelPermalink', $permalink->model);
2829

30+
event(new PermalinkGotAccessed($permalink->model));
31+
2932
return Response::redirectTo($permalink->model->getPermalink());
3033
}
3134
}

src/Events/PermalinkGotAccessed.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace OpenSoutheners\LaravelModelPermalink\Events;
4+
5+
use Illuminate\Queue\SerializesModels;
6+
use OpenSoutheners\LaravelModelPermalink\PermalinkAccess;
7+
8+
class PermalinkGotAccessed
9+
{
10+
use SerializesModels;
11+
12+
/**
13+
* Create a new event instance.
14+
*
15+
* @return void
16+
*/
17+
public function __construct(public PermalinkAccess $model)
18+
{
19+
//
20+
}
21+
}

tests/IntegrationTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
namespace OpenSoutheners\LaravelModelPermalink\Tests;
44

55
use Illuminate\Foundation\Auth\User;
6+
use Illuminate\Support\Facades\Event;
67
use Illuminate\Support\Facades\Route;
8+
use OpenSoutheners\LaravelModelPermalink\Events\PermalinkGotAccessed;
79
use OpenSoutheners\LaravelModelPermalink\GeneratePermalink;
810
use OpenSoutheners\LaravelModelPermalink\Tests\Fixtures\Post;
911

@@ -53,8 +55,12 @@ public function testAnyUserCanAccessAnyModelPermalinkByDefault()
5355

5456
$modelPermalink = GeneratePermalink::for($post);
5557

58+
Event::fake(PermalinkGotAccessed::class);
59+
5660
$response = $this->get($modelPermalink->getModelPermalink());
5761

62+
Event::assertDispatched(PermalinkGotAccessed::class, fn (PermalinkGotAccessed $event) => $event->model->is($post));
63+
5864
$response->assertRedirectToRoute('posts.show', $post);
5965

6066
$redirectedResponse = $this->followRedirects($response->baseResponse);

0 commit comments

Comments
 (0)