Closed
Description
This is a technique I'm using on a project recently and and find the idea to be useful.
Consider the route:
Route::get('/products/{productType}/{category}', array('uses' => 'ProductsController@showProducts'));
and the URL requested
http://example.dev/products/tickets/conference?keyword=laracon
This is my current implementation strung together but I'd expect the final implementation to be in the Router/Route classes somewhere... just to illustrate the idea:
public function __construct()
{
$this->router = App::__get('router');
$currentRoute = $this->router->getCurrentRoute();
$keys = $currentRoute->compile()->getVariables();
$vars = $currentRoute->getVariables();
$params = array();
if (! empty($keys)) {
$params = array_combine($keys, $vars);
}
$allInput = Input::all();
$this->parameters = new Illuminate\Support\Fluent($params);
$this->input = new Illuminate\Support\Fluent(array_merge($params, $allInput));
}
public function showUser()
{
$this->input->get('productType'); // productType as part of the route URI
$this->input->get('category'); // category as part of the route URI
$this->input->get('keyword'); // keyword as query string parameter
}
The first payoff is that accessing route variables and input variables by the same method is convenient. Secondly, the controller and application becomes aware of the key/value pairs given by route (without using something ugly like func_get_args()
in every single method call).
Metadata
Metadata
Assignees
Labels
No labels