Symfony 1.4 is already a legacy framework. But I think a lot of web system still running on it.
In this post, I will show you some quick tips for accessing infromation by using sfContext.
sfContext provides almost everything you want access.
But you should not use context everywhere too much because sfContext is a kind of global objeect and if a lot of codes depends on context, your code will be hard to be tested or maintained, I think.
Basic
You can access sfContext by following code.sfContext::getInstance();In sfFilter, you can access sfContext by the following code.
$this->getContext();
What Kind of Fields You can access?
Routing name
$context->getRouting()->getCurrentRouteName();
User
$context->getUser();
Request
$context->getRequest();
Response
$context->getResponse();
Controller
$context->getController();
Logger
sfContext::getInstance()->getLogger();
Advanced Use
Check if http request is secure (e.g. https request).$context->getRequest()->isSecure();
Set session attribute.
$context->getUser()->setAttribute('name', 'value');
Redirect.
$context->getController()->redirect('@route_name');
Set cookie.
$context->getResponse()->setCookie('cookie', '', time()-3600, '/', '', false, true);
コメント