In my project, I have a route to display topics that looks like this.
Route::get('/voting-topics', [PublicTopicController::class, 'index'])->name('public-topics');
The voting topics page show a list of topic pulled from my DB where they are marked as public.
The issue I'm facing is I also want to display a full list of topics for subscribers.
Currently, I'm using this route for subscribers
Route::group(['middleware' => ['subscriber']], function () {
Route::get('/dashboard/topics', [MemberTopicController::class, 'topics'])->name('dashboard.topics');
});
I don't think this is the best way, I'd prefer to have all the topics on /topics and but only show the public ones for none subscribers.
I guess my question is, is there a way to check in my route if a user is a subscriber and do something like this.
if( subscriber()->user() ) {
Route::post('/topics', [TopicController::class, 'index'])->name('topics');
} else {
Route::post('/topics', [TopicController::class, 'publicTopic'])->name('topics');
}
Instead of declaring two different routes, I would declare a single route and I would decide in the controller what to do, based on the subscriber status.
Something like:
Route::get('/topics', [TopicController::class, 'topics'])->name('topics');
in your controller:
public function topics() {
if (subscriber()->user()) {
return $this->subscriberTopics();
} else {
return $this->publicTopics();
}
}
private function publicTopics() {
// public topics stuff
}
private function subscriberTopics() {
// subscriber topics stuff
}