ouzo icon indicating copy to clipboard operation
ouzo copied to clipboard

Route parameters passed to controller methods.

Open danon opened this issue 7 years ago • 5 comments

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.

danon avatar Aug 10 '16 14:08 danon

Where do you want to use ReflectionParameter? I don't get it.

bbankowski avatar Aug 10 '16 14:08 bbankowski

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);
}

danon avatar Aug 10 '16 15:08 danon

For the second option to work you'd have to design routes like this

Route::get('/integration/:something', 'controller#byName');

danon avatar Aug 10 '16 15:08 danon

We could just use order of arguments, so no ReflectionParameter would be needed.

bbankowski avatar Aug 10 '16 15:08 bbankowski

:+1: for @bbankowski solution. Using order of arguments should have better performance. (Laravel uses this the way you described it :smile:)

piotrooo avatar Aug 10 '16 18:08 piotrooo