I'd like to pass a predefined value from my route as an argument to controller.
Assuming this is my controller file:
class PagesController extends Controller
{
public function display($page)
{
return view("pages.$page");
}
}
And this is the routes file:
Route::get('/', 'PagesController@display'); // this must pass "home" as controller argument $page
Route::get('/about', 'PagesController@display'); // this must pass "about" as controller argument $page
I wonder what is the smart laravel way to make the first route pass "home" as controller argument (so that pages.home view is rendered) and the second route pass "about" as controller argument (so that pages.about view is rendered).
I am sure this question is pretty trivial and should happen frequently, but somehow I couldn't find a quick solution. What do I overlook? Thanks!
Route::get('about', ['uses' => 'PagesController@display', 'page' => 'about']);
public function display(\Illuminate\Http\Request $request)
{
$page = $request->route()->getAction()['page'];
...
}
One way to pass that hardcoded data via the route.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community