By default, adding the Common component to your AppController will make sure your POST and query params are trimmed. This is needed to make - not only notEmpty - validation working properly.
You can skip for certain actions using 'DataPreparation.notrim'
config key per use case.
A convenience method can quickly check on a form being posted:
if ($this->Common->isPosted()) {}
Saves you the trouble of checking for post
, patch
, put
etc together, and in most cases this is not necessary. It is only important it wasn't a get
request.
Sometimes you want to post to an edit or delete form and make sure you get redirected back to the correct action including query strings (e.g. for filtering).
Then you can pass redirect
key as either as part of POST payload or as query string.
// In your action
$redirectUrl = $this->Common->getSafeRedirectUrl(['action' => 'default']);
return $this->redirect($redirectUrl);
It is important to not use the payload data without sanitation for security reasons (redirect forgery to external websites).
CommonComponent::defaultUrlParams()
will give you the default params you might want to combine with your URL
in order to always generate the right URLs even if inside plugins or prefixes.
$this->Common->currentUrl()
returns current url (with all missing params automatically added).
A shortcut convenience wrapper for referrer redirecting with fallback:
return $this->Common->autoRedirect($defaultUrl);
Set the 2nd param to true to allow redirecting to itself (if that was the referer).
Automatically also adds the query string into the redirect. Useful when you want to keep the filters and pass them around.
return $this->Common->completeRedirect($redirectUrl);
Convenience method to get passed params:
$param = $this->Common->getPassedParam('x', $default);
Check if a certain referer is a non local one:
// using env referer
$result = $this->Common->isForeignReferer();
// or explicit value
$result = $this->Common->isForeignReferer($urlString);
If you need all current (direct) actions of a controller, call
$this->Common->listActions()