开发者

creating a dynamic sidebar with zend framework

开发者 https://www.devze.com 2023-01-24 01:56 出处:网络
I am trying to create a dynamic sidebar in zend framework. I have googled some articles, browsed even the stackoverflow archive but i can\'t seem to get it so please help me figure this out.

I am trying to create a dynamic sidebar in zend framework. I have googled some articles, browsed even the stackoverflow archive but i can't seem to get it so please help me figure this out. Here is some code from my layout.phtml file:

<div id="contentWrapper">
<div id="contentArea">
<?php echo $this->layout()->content;
?>
</div>
<div id="sidebar">
    <div id="user-authentication">
        <?php if (Zend_Auth::getInstance()->hasIdentity()) {
        ?>Logged In as<br />
        <?php
            echo Zend_Auth::getInstance()->getIdentity();
        } else {
        ?>

            <input type="text" name="login" class="loginInput" /><br/>    
            <input type="password" name="pas开发者_如何学编程sword" class="loginInput" /><br/>    

            <input type="submit" name="submit" value="Log In" class="loginButton" />    
<?php } ?>
    </div>
    <div id="sidebar-content">
                            <? echo $this->layout()->sidebar; ?>
    </div>
</div>

</div>

I could use this Best practice creating dynamic sidebar with zend framework but that means I would need to have redundant code for displaying the login box/logged in as.


Are you just worried about the repetition of hasIdentity and getIdentity in the sidebar-content div?

If so, maybe you'd prefer this:

<?php
    $auth = Zend_Auth::getInstance();
    $user = $auth->hasIdentity() ? $auth->getIdentity : NULL;
?>
<div id="user-authentication">
    <?php if ($user) { ?>
        User stuff.
    <?php } else { ?>
        Public stuff.
    <?php } ?>
</div>
<div id="user-authentication">
    <?php if ($user) { ?>
        User stuff.
    <?php } else { ?>
        Public stuff.
    <?php } ?>
</div>

However, that's just a matter of coding style. There's nothing wrong with checking a users logged in state more than once. It's necessary with modular code to do so; that's probably why Zend_Auth is a Singleton.


Looks like you need a widget:

  • Using Action Helpers To Implement Re-Usable Widgets - phly, boy, phly

For displaying or not some of the content, look for integration of Zend_Acl and Zend_Navigation.
It is as easy as: $container->setAcl($acl)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号