问题
Situation: only main page is accessible by default, all other pages needs a logged in user. When a module is loaded without user, a login template should be displayed, and no module. In other words, the $sf_content
must be emptied in layout.php
which is not 100% ok since there is logic in the layout. Is there elegant way for that? I dont think a helper is OK either....
回答1:
Check out security filters, this is one standard way security is designed in symfony. You even can implement your own SecurityFilter class with the functionality you want.
http://symfony.com/legacy/doc/reference/1_4/en/12-Filters#chapter_12_security
回答2:
It is done by default for you by the sfBasicSecurityFilter
filter. You just need a good configuration. Read this part of the Jobeet tutorial. You should use sfDoctrineGuardPlugin
(or sfGuardPlugin
if you using propell) for user authentication.
回答3:
To complete my comments above: There are different ways to override the layout. You could use the methods:
setLayout($name)
//or using foward, which forwards current action to a new one (without browser redirection)
forward($module, $action);
inside your action class. In case you wand to modify the layout inside a filter, you can use something simular to this:
class yourFilter extends sfFilter {
public function execute($filterChain) {
if($yourConditionForOverrideTheDefaultLayout) {
//here the syntax to change the layout from the filer
$actionStack = $this->getContext()->getActionStack();
$actionStack->getFirstEntry()->getActionInstance()->setLayout('yourLayout');
}
$filterChain->execute();
}
}
To avoid unnecessary duplication in the layout file you can work with Fragments and Partials.
来源:https://stackoverflow.com/questions/14182070/i-need-to-direct-modify-the-sf-content-what-is-the-best-workaround