Pretty URL in POST response

I have a rule set in urlManager so that a URL which looks like example.com/products/3/4/5 will take a user to a page with all products in categories 3, 4, and 5. This is working well. That page has a sort of typical shopping sidebar, where a user can choose categories of products. If a user chooses categories 1,4,7 and hits "update selection", the page reloads with all products in categories 1, 4, and 7 showing. This is working well. When the page is reloaded however, the url appearing in the browser is example.com/products. I would prefer that that, if the form is posted after clicking "update selection" that the url that comes back from the post request is of the form example.com/products/1/4/7.

Is that possible? I’ve been looking at the Routing and URL Creation page in the Guide, but haven’t yet found anything that seems to address what I’m trying to do.

Any thoughts?

I think you can redirect to a page with the selected categories after you have updated the user selection by processing the post request.

Post/Redirect/Get pattern

Thank you, that was a very helpful response and got me on the right track. For anyone else reading this, all I did was to do a redirect if the request was a POST. Something like this:




$strCategories = "3/4/5"; // just illustrative.  string actually imploded from array


if ($request->isPost) {

    return $this->redirect(['product/' . $strCategories ]);

} else {

    return $this->render('product', [

        'model' => $model,

        'products' => $products,

    ]);

}



Since yii\web\Controller::redirect() has a default HTTP Status Code of 302, this accomplished exactly what I wanted to do.

If there is a more elegant way to do this, I’d love to hear it… but this at least works.

Thanks softark!