I'd revert the logic and because of that I deleted the previous edit. You can delete yours too cause it's not referring to a question at this moment.
So I followed your logic and wrote this route:
Route::get('dictionary/{language}/{id}', 'WordController@show');
which I assign to the existing controller rather than creating a new one.
I also managed to get the language in between the dictionary and the id. See code below.
Controller
public function show($language, $id)
{
$word = Word::query()->findOrFail($id);
$lang = Word::getLang($id, $language);
return view('word.show', compact('word', 'lang', $word, $lang));
}
Model
public static function getLang($id, $language)
{
// Finds the language belonging to the word by id from the database.
$toFindLang = static::query()->select('language')->where([
[
'id',
$id
],
[
'language',
$language
]
])->value('language');
// Checks if the language in the URL doesn't match the language
// belonging to the word by id from database, if so redirects back to previous page.
if ($language !== $toFindLang) {
return redirect()->back();
}
}
Basically what I'm trying to do is simply to check if the language in the url does not match the one associated with the id. This to prevent people from changing the url.
Also, is it better to split the route property in separate controller or will this do the job?
Thank you!