- get request parameter:
$request->getParameter('id')
$this->redirect($this->generateUrl('job_show_user', $job));
public function indexSuccess($request) {
$this->getResponse()->setHttpHeader('Content-Type',' text/plain');
$this->getResponse()->setContent("OK");
return sfView::NONE;
}
Custom filters can be added to lib/filters
directory.
An example filter definitions:
class sfSecureFilter extends sfFilter
{
public function execute($filterChain)
{
$context = $this->getContext();
$request = $context->getRequest();
if (!$request->isSecure())
{
$secure_url = str_replace('http', 'https', $request->getUri());
return $context->getController()->redirect($secure_url);
// We don't continue the filter chain
}
else
{
// The request is already secure, so we can continue
$filterChain->execute();
}
}
}
class sfGoogleAnalyticsFilter extends sfFilter
{
public function execute($filterChain)
{
// Nothing to do before the action
$filterChain->execute();
// Decorate the response with the tracker code
$googleCode = '
<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct="UA-'.$this->getParameter('google_id').'";urchinTracker();
</script>';
$response = $this->getContext()->getResponse();
$response->setContent(str_ireplace('</body>', $googleCode.'</body>',$response->getContent()));
}
}
As an action can forward or redirect to another action and consequently relaunch the full chain of filters, you might want to restrict the execution of your own filters to the first action call of the request. The
isFirstCall()
method of thesfFilter
class returns a Boolean for this purpose. This call only makes sense before the action execution.
Example:
class rememberFilter extends sfFilter
{
public function execute($filterChain)
{
// Execute this filter only once
if ($this->isFirstCall())
{
// Filters don't have direct access to the request and user objects.
// You will need to use the context object to get them
$request = $this->getContext()->getRequest();
$user = $this->getContext()->getUser();
if ($request->getCookie('MyWebSite'))
{
// sign in
$user->setAuthenticated(true);
}
}
// Execute next filter
$filterChain->execute();
}
}
Forwarding:
return $this->getContext()->getController()->forward('mymodule', 'myAction');`
Custom filters can be enabled in config/filters.yml
.
Example config/filters.yml
:
rendering: ~
security: ~
remember: # Filters need a unique name
class: rememberFilter
param:
cookie_name: MyWebSite
condition: %APP_ENABLE_REMEMBER_ME%
cache: ~
execution: ~
Getting parameters from filters.yml
configuration:
class rememberFilter extends sfFilter
{
public function execute($filterChain)
{
// ...
if ($request->getCookie($this->getParameter('cookie_name')))
{
// ...
}
// ...
}
}