ouzo
ouzo copied to clipboard
Route parameters passed to controller methods.
Routes
Route::get('/integration/:name/:id', 'controller#byNameAndId');
Controller
public function byNameAndId($name, $id) {
$this->integration = Integration::findByNameId($name, $id);
}
instead of this
public function byNameAndId() {
$this->integration = Integration::findByNameId($this->params['name'], $this->params['id']);
}
These parameters will always be present because routes would not point to those methods otherwise. ReflectionParameter could be used to determine parameter name and assign proper param value.
Where do you want to use ReflectionParameter? I don't get it.
FrontController could lookup Controller's method's parameters' names, and assign them proper keys from $this->params
.
So this could work
Route::get('/integration/:name', 'controller#byName');
public function byName($name) {
$this->integration = Integration::findByName($name);
}
but this, would not:
public function byName($something) { // <-- exception, there's no such thing as $this->params['something']
$this->integration = Integration::findByName($something);
}
For the second option to work you'd have to design routes like this
Route::get('/integration/:something', 'controller#byName');
We could just use order of arguments, so no ReflectionParameter would be needed.
:+1: for @bbankowski solution. Using order of arguments should have better performance. (Laravel uses this the way you described it :smile:)