File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1616use App \Traits \PostTrait ;
1717use App \Traits \SubsiteTrait ;
1818use Illuminate \Contracts \View \View ;
19+ use Illuminate \Http \RedirectResponse ;
1920
2021final class PostController extends BaseController
2122{
@@ -42,11 +43,21 @@ public function index(): View
4243 ]);
4344 }
4445
45- public function show (Post $ post ): View
46+ public function show (Post $ post, ? string $ slug = null ): View | RedirectResponse
4647 {
47- $ relatedPosts = $ this ->postRepository ->getRelatedPosts ($ post );
48-
48+ // Check if the post belongs to the correct subsite
4949 $ subdomain = $ this ->getSubdomain () === 'www ' ? 'metafilter ' : $ this ->getSubdomain ();
50+ $ postSubdomain = $ post ->subsite ->subdomain === 'www ' ? 'metafilter ' : $ post ->subsite ->subdomain ;
51+
52+ // Redirect if subdomain or slug is incorrect
53+ if ($ subdomain !== $ postSubdomain || $ slug !== $ post ->slug ) {
54+ return redirect ()->route ($ this ->getShowPostRouteName ($ postSubdomain ), [
55+ 'post ' => $ post ,
56+ 'slug ' => $ post ->slug ,
57+ ], 301 );
58+ }
59+
60+ $ relatedPosts = $ this ->postRepository ->getRelatedPosts ($ post );
5061
5162 return view ('posts.show ' , [
5263 'title ' => $ post ->title ,
Original file line number Diff line number Diff line change @@ -157,9 +157,9 @@ public function getPostIndexRouteName(): string
157157 };
158158 }
159159
160- public function getShowPostRouteName (): string
160+ public function getShowPostRouteName (? string $ subdomain = null ): string
161161 {
162- $ subdomain = $ this ->getSubdomain ();
162+ $ subdomain ?? = $ this ->getSubdomain ();
163163
164164 return match ($ subdomain ) {
165165 'ask ' => 'ask.posts.show ' ,
Original file line number Diff line number Diff line change 3333 Route::get ('' , 'index ' )
3434 ->name ('ask.posts.index ' );
3535
36- Route::get ('{post}/{slug} ' , 'show ' )
36+ Route::get ('{post}/{slug? } ' , 'show ' )
3737 ->name ('ask.posts.show ' );
3838
3939 Route::middleware ('auth ' )->group (function () {
Original file line number Diff line number Diff line change 99 Route::get ('' , 'index ' )
1010 ->name ('bestof.posts.index ' );
1111
12- Route::get ('{post}/{slug} ' , 'show ' )
12+ Route::get ('{post}/{slug? } ' , 'show ' )
1313 ->name ('bestof.posts.show ' );
1414});
Original file line number Diff line number Diff line change 3333 Route::get ('' , 'index ' )
3434 ->name ('fanfare.posts.index ' );
3535
36- Route::get ('{post}/{slug} ' , 'show ' )
36+ Route::get ('{post}/{slug? } ' , 'show ' )
3737 ->name ('fanfare.posts.show ' );
3838
3939 Route::middleware ('auth ' )->group (function () {
Original file line number Diff line number Diff line change 1515 Route::get ('' , 'index ' )
1616 ->name (RouteNameEnum::IrlMyPostsIndex);
1717
18- Route::get ('{post}/{slug} ' , 'show ' )
18+ Route::get ('{post}/{slug? } ' , 'show ' )
1919 ->name (RouteNameEnum::IrlMyPostsShow);
2020 });
2121});
3333 Route::get ('' , 'index ' )
3434 ->name ('irl.posts.index ' );
3535
36- Route::get ('{post}/{slug} ' , 'show ' )
36+ Route::get ('{post}/{slug? } ' , 'show ' )
3737 ->name ('irl.posts.show ' );
3838
3939 Route::middleware ('auth ' )->group (function () {
Original file line number Diff line number Diff line change 2525 Route::get ('' , 'index ' )
2626 ->name ('jobs.posts.index ' );
2727
28- Route::get ('{post}/{slug} ' , 'show ' )
28+ Route::get ('{post}/{slug? } ' , 'show ' )
2929 ->name ('jobs.posts.show ' );
3030
3131 Route::middleware ('auth ' )->group (function () {
Original file line number Diff line number Diff line change 122122 Route::get ('' , 'index ' )
123123 ->name ('metafilter.posts.index ' );
124124
125- Route::get ('{post}/{slug} ' , 'show ' )
125+ Route::get ('{post}/{slug? } ' , 'show ' )
126126 ->name ('metafilter.posts.show ' );
127127
128128 Route::middleware ('auth ' )->group (function () {
Original file line number Diff line number Diff line change 3535 Route::get ('' , 'index ' )
3636 ->name ('metatalk.posts.index ' );
3737
38- Route::get ('{post}/{slug} ' , 'show ' )
38+ Route::get ('{post}/{slug? } ' , 'show ' )
3939 ->name ('metatalk.posts.show ' );
4040
4141 Route::middleware ('auth ' )->group (function () {
Original file line number Diff line number Diff line change 3636 Route::get ('' , 'index ' )
3737 ->name ('music.posts.index ' );
3838
39- Route::get ('{post}/{slug} ' , 'show ' )
39+ Route::get ('{post}/{slug? } ' , 'show ' )
4040 ->name ('music.posts.show ' );
4141
4242 Route::middleware ('auth ' )->group (function () {
You can’t perform that action at this time.
0 commit comments