File tree Expand file tree Collapse file tree 3 files changed +30
-0
lines changed Expand file tree Collapse file tree 3 files changed +30
-0
lines changed Original file line number Diff line number Diff line change 6
6
use Illuminate \Http \RedirectResponse ;
7
7
use Illuminate \Routing \Controller ;
8
8
use Illuminate \Support \Facades \Response ;
9
+ use OpenSoutheners \LaravelModelPermalink \Events \PermalinkGotAccessed ;
9
10
10
11
class ModelPermalinkController extends Controller
11
12
{
@@ -26,6 +27,8 @@ public function __invoke(string $permalink): RedirectResponse
26
27
27
28
$ this ->authorize ('viewModelPermalink ' , $ permalink ->model );
28
29
30
+ event (new PermalinkGotAccessed ($ permalink ->model ));
31
+
29
32
return Response::redirectTo ($ permalink ->model ->getPermalink ());
30
33
}
31
34
}
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 3
3
namespace OpenSoutheners \LaravelModelPermalink \Tests ;
4
4
5
5
use Illuminate \Foundation \Auth \User ;
6
+ use Illuminate \Support \Facades \Event ;
6
7
use Illuminate \Support \Facades \Route ;
8
+ use OpenSoutheners \LaravelModelPermalink \Events \PermalinkGotAccessed ;
7
9
use OpenSoutheners \LaravelModelPermalink \GeneratePermalink ;
8
10
use OpenSoutheners \LaravelModelPermalink \Tests \Fixtures \Post ;
9
11
@@ -53,8 +55,12 @@ public function testAnyUserCanAccessAnyModelPermalinkByDefault()
53
55
54
56
$ modelPermalink = GeneratePermalink::for ($ post );
55
57
58
+ Event::fake (PermalinkGotAccessed::class);
59
+
56
60
$ response = $ this ->get ($ modelPermalink ->getModelPermalink ());
57
61
62
+ Event::assertDispatched (PermalinkGotAccessed::class, fn (PermalinkGotAccessed $ event ) => $ event ->model ->is ($ post ));
63
+
58
64
$ response ->assertRedirectToRoute ('posts.show ' , $ post );
59
65
60
66
$ redirectedResponse = $ this ->followRedirects ($ response ->baseResponse );
You can’t perform that action at this time.
0 commit comments