You'll have to specify the parameter. You might be able to send the extra parameter in the request body but this is a GET request so avoid that.
Why not do this:
Route::get('/categories/{format}/{id}', ['uses'=>'CategoryController@categoryById']);
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hi,
When i have the folowing in my routes:
Route::group(['prefix' => '/something'], function() {
Route::get('/categories/{id}', ['uses'=>'CategoryController@categoryById']); // returns given category as json
}
Route::group(['prefix' => '/categories'], function() {
Route::get('/{id}', ['uses'=>'CategoryController@categoryByIdView']); // returns given category as view
});
and this in my category controller:
public function categoryById ($id) { // returns given product as json
$category = \App\Category::with('product')->findOrFail($id);
return response()->json($category);
}
public function categoryByIdView ($id) { // returns given product as view
$category = \App\Category::with('product')->findOrFail($id);
return viewForDomain('category', ['category'=>$category]);
}
everything works.
But i like this more in my controller:
public function categoryById ($id, $format) { // returns given product as given format
$category = \App\Category::with('product')->findOrFail($id);
if($format == 'json'){
return response()->json($category);
}
else if($format == 'view'){
return viewForDomain('category', ['category'=>$category]);
}
}
But, if i want to keep the shorthand notation in my routes, is it possible to pass an extra parameter to the function, without adding it to the route path? I think i need something like:
Route::group(['prefix' => '/something'], function() {
Route::get('/categories/{id}', ['uses'=>'CategoryController@categoryById','format'=>'json']); // returns given category as json
}
Route::group(['prefix' => '/categories'], function() {
Route::get('/{id}', ['uses'=>'CategoryController@categoryById','format'=>'view']); // returns given category as view
});
off course this is some dummy code, that gives:
Missing argument 2 for App\Http\Controllers\CategoryController::categoryById()
How can i do something like this? or do i have to skip the shorthand at this point and add the function way of writing here?
Bart
just told at laracon, about 5.3:
Alyssa July 27, 20164:07 Pm 1 – changes to the folder structure.
Eric L. Barnes July 27, 20164:07 Pm Splitting up the routes file into web.php and api.php
looks like there actualy are thoughts about api? ;)
Please or to participate in this conversation.