inovak's avatar

With known url in request payload, get parameters

I have a form request payload as follows:

_token: 4xwqi40hwNsiL3yPeZuCXxwH08h4TrPhJsu0KYq5
name: some name
slug: some slug
description: some longer description
display_order:1
active_date:2013-04-26
action: https://app.dev/admin/collection/9/update
class: App/Http/Requests/Admin/Collection/UpdateCollectionRequest

I have a couple routes in route.php with something like the following:

Route::post('admin/collection/{collection}/update', 'CollectionController@updateCollection');
Route::post('admin/validate', 'AjaxValidation@validate');

The form data above is being POSTed to the validation route. The logic behind the validation route knows that I'm going to submit my data to action and uses the provided request class to validate the input prior to form submission to said route. I'm trying to grab the {collection} parameter in a reliable way that doesn't rely on knowing the segment index.

I'd like to match the value of action against the known routes in the application and have it (at least) return something like within the App/Http/Requests/Admin/Collection/UpdateCollectionRequest class:

array(
    collection => `9`
)

I have a feeling that something like this is possible, but after poking around in UrlGenerator and Http\Request I'm not finding anything of use.

What do you guys think? Any help would be greatly appreciated.

--- Edited to provide more clarity on what we're after ---

0 likes
6 replies
tisuchi's avatar

Try with that-

In your getCollectionById() method-


public function getCollectionById(Request $request){
    
    // https://app.dev/admin/collection/9
    $original_action = $request->input('original_action');  

    //getting last value of current url
    // example www.exmaple.com/admin/collection/{collection}
    // in example url, the last one is 3
    $lastValue = Request::segment(3);

    $explodeData = explode('/', $original_action);
    $lastElement = $explodeData[count($explodeData)];


    if($lastValue == $lastElement){
        return 'match';
    } 

    return 'didnt match';

}

inovak's avatar

Hm - you know what, that's a solid simple solution @tisuchi . Though I was hoping to find a solution where I wouldn't necessarily be tied to the structure of the url... perhaps even leveraging route model bindings and supporting N parameters in the url.

Snapey's avatar

it depends if you can pass just the value to the form, then you could have it in the URL for the form POSTing.

Snapey's avatar

So your validation route is multi-purpose and will use the class: parameter to determine the type?

Why not pass the entity id in a similar manner (in the post payload), or pass as part of the validation string?

Route::post('admin/validate/{id}', 'AjaxValidation@validate');

Sorry, I still struggling to understand the complication. You have framed it as how to extract the id from the action, but why not duplicate it somewhere else? You must know it at some point?

inovak's avatar
inovak
OP
Best Answer
Level 2

Thanks for your attention guys, I appreciate it. I've found a way through to get exactly what I wanted:

// create a new request
$request = \Illuminate\Http\Request::create($this->input('action'), $this->input('method'));
// feed the route collection
$matchingRoute = \App::make('Illuminate\Routing\Router')->getRoutes()->match($request);

Then I can grab the parameters via: $matchingRoute->parameters()

The call returns the following:

[
     "collection" => "9",
]

Edit: Oh, to allow this to work, I've also had to specify the form action in the request. In this test case I would have added method: PUT

Please or to participate in this conversation.