我需要直接修改$sf_content什么是最好的解决方案


I need to direct modify the $sf_content what is the best workaround?

情况:默认情况下只有主页可以访问,其他所有页面都需要用户登录。当没有user加载模块时,应该显示登录模板,而不是模块。换句话说,$sf_content必须在layout.php中清空,这不是100% ok,因为布局中存在逻辑。有什么优雅的方法吗?我不认为一个助手是可以的....

查看安全过滤器,这是symfony中安全设计的一种标准方式。你甚至可以用你想要的功能实现你自己的SecurityFilter类。

http://symfony.com/legacy/doc/reference/1_4/en/12-Filters chapter_12_security

默认情况下由sfBasicSecurityFilter过滤器为您完成。你只需要一个好的配置。阅读Jobeet教程的这一部分。您应该使用sfDoctrineGuardPlugin(或sfGuardPlugin,如果您使用propell)进行用户身份验证。

要完成我上面的评论:有不同的方法来覆盖布局。您可以使用以下方法:

 setLayout($name) 
 //or using foward, which forwards current action to a new one (without browser redirection)    
 forward($module, $action);

在你的动作类中。如果你想修改过滤器内部的布局,你可以使用类似这样的东西:

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();
    }
}

为了避免布局文件中不必要的重复,你可以使用Fragments和Partials。