Just
Route::get('/categories/{id}/{format}', ['uses'=>'CategoryController@categoryById']);
@voyowsky yeah but i dont want the format in the url, thats why i gave the example of the two diferent routes ;)
thats why the title is "Passing parameters outside url to shorthand routes" ;)
I'm sorry @BartHuis. My bad. :)
Is there a reason to why you want to do this?
I can only think of one way to do this:
Route::get('/', [function (Illuminate\Http\Request $request) {
$controller = new \App\Http\Controllers\SomeController();
return $controller->getAction($request, 'my format');
}]);
Sorry if i wasted your time. This probably isn't what you've been looking for.
haha thanks @voyowsky that was the alternative i thought about. but it looks better in my routes file if i could handle it by shorthand routes. thats why i wanted to know if its possible to pass parameters to the function without adding it to the url path... ;)
@voyowsky dont know why you included the iluminate request in and let the function in an array, but this edited one from your suggestion is indeed working:
Route::group(['prefix' => '/something'], function() {
Route::get('/categories/{id}', function ($id) {
$controller = new \App\Http\Controllers\CategoryController();
return $controller->categoryById($id, 'json');
}); // returns given category as json
}
Route::group(['prefix' => '/categories'], function() {
Route::get('/{id}', function ($id) {
$controller = new \App\Http\Controllers\CategoryController();
return $controller->categoryById($id, 'view');
}); // returns given category as view); // returns given category as view
});
but it would be nice if someone still can come up with a shorthand method to do this, this way it would almost be more efficient to put everything from the controller function directly to this function, while my intension is to put as much as possible outside the controllers :)
Oh, i put the request there in case you needed to use additional parameters in the controller, without using the request helper, like so:
public function categoryById (Request $request, $id, $format) {
$category = \App\Category::with('product')->findOrFail($id);
if($format == 'json'){
return response()->json($category);
}
else if($format == 'view'){
return viewForDomain('category', ['category'=>$category]);
}
$request->input('another_query_or_form_parameter');
// instead of using request()->input('another_param');
}
Normally, the request would have been injected into the controller method/constructor, but in your case, you need to add it yourself.
About the array, i just forgot to remove it. It is redundant, so just ignore it. :)
for who's interested, it was to make the api part of my app, at laracasts forum there more discussion on this part and how to handle that in other ways ;)
https://laracasts.com/discuss/channels/laravel/passing-parameters-outside-url-to-shorthand-routes
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community