Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Proposal] Include route parameters as key/value pairs in Input #22

Closed
stephenfrank opened this issue Jan 13, 2013 · 4 comments
Closed

Comments

@stephenfrank
Copy link
Contributor

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).

@bencorlett
Copy link
Contributor

I'm curious about this. Though it does seem to break a fair bit of the routing that's in place now and I can't really see the advantage of using $this->input->get('productType') as opposed to $productType as a passed parameter to the controller / closure? Maybe I'm missing something.

Ben

@stephenfrank
Copy link
Contributor Author

Hi Ben, I don't want to remove route parameters as arguments. I was just looking for a unified way to access route parameters and user input because in some cases (that I'm working with) they're semantically similar.

Something like Input::getWithRouteParameters('myParameter') or Input::allWithRouteParameters() (and perhaps not so ugly).

The second half of my thought process was really the fact that the Request isn't injected into the Controller in any explicit way. The Request is held within the context of the Router and to access it one must appeal to the global App container and eg. $app['router']->getRequest()->input('myParameter') which feels a bit too much like global state for me. I almost expect the request to be injected into the Controller eg:

$this->request->input('myParameter');
$this->request->all();

Anyway I feel like I've talked myself in circles. I can see how easy this will be to add to my BaseController so I might just go back to hacking around on my app until I clear up for myself what I'm really going on about :)

@bencorlett
Copy link
Contributor

Hmm. I have noticed that too, how you have no access to the request from a controller. It has been a bit annoying before for me too. Maybe that's it's own issue

Sent from my iPhone
Please excuse my brevity

On 15/01/2013, at 7:54 PM, stephenfrank notifications@github.com wrote:

Hi Ben, I don't want to remove route parameters as arguments. I was just looking for a unified way to access route parameters and user input because in some cases (that I'm working with) they're semantically similar.

Something like Input::getWithRouteParameters('myParameter') or Input::allWithRouteParameters() (and perhaps not so ugly).

The second half of my thought process was really the fact that the Request isn't injected into the Controller in any explicit way. The Request is held within the context of the Router and to access it one must appeal to the global App container and eg. $app['router']->getRequest()->input('myParameter') which feels a bit too much like global state for me. I almost expect the request to be injected into the Controller eg:

$this->request->input('myParameter');
$this->request->all();
Anyway I feel like I've talked myself in circles. I can see how easy this will be to add to my BaseController so I might just go back to hacking around on my app until I clear up for myself what I'm really going on about :)


Reply to this email directly or view it on GitHub.

@taylorotwell
Copy link
Member

I don't see a huge need for this at the moment. Thanks for the suggestion.

taylorotwell pushed a commit that referenced this issue Mar 4, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants