我需要直接修改 $sf_content 什么是最好的解决方法? [英] I need to direct modify the $sf_content what is the best workaround?

查看:25
本文介绍了我需要直接修改 $sf_content 什么是最好的解决方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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....

推荐答案

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

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.

这篇关于我需要直接修改 $sf_content 什么是最好的解决方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆