sudeep cv
sudeep cv

Reputation: 1027

how to check session in zend layout

layout.phtml:

<?php echo $this->doctype() ?>
<html>
<head>
    <?php echo $this->headTitle()."\n"; ?>
    <?php echo $this->headLink() ."\n"; ?>    
    <?php echo $this->headScript(); ?>
</head>
<body>
<div id="wrap">

       <div class="header">
            <div class="logo"><a href="index.html"><img src="../../images/logo.gif" alt="" title="" border="0" /></a></div>            
        <div id="menu">
            <ul>                                                                       
            <li class="selected"><a href="index.html">home</a></li>
            <li><a href="about.phtml">about us</a></li>
            <li><a href="category.phtml">books</a></li>
            <li><a href="specials.phtml">specials books</a></li>
            <li><a href="myaccount.phtml">my accout</a></li>
            <li><a href="register.phtml">register</a></li>
            <li><a href="details.phtml">prices</a></li>
            <li><a href="contact.phtml">contact</a></li>
            <li><a href="addbook.phtml">adddbook</a> </li>
            </ul>
        </div>     


       </div> 


       <div class="center_content">
        <div class="left_content">
       <?php echo $this->layout()->content ?>

I am new to zend framework here i am facing problem with displaying navigations. in pure php i can serve my navigation by checking sessions. like this:

<?php 
if($_SESSION['usertype']=='admin')
{
echo "<li><a href="addbook.phtml">adddbook</a> </li>";
}

?>

My question is how can i implement this in zend framework. Thanks

Upvotes: 1

Views: 2441

Answers (2)

Lobo
Lobo

Reputation: 566

You can do this in a myriad of ways like the Zend_Auth, but the closes to the $_SESSION method is using Zend_Session.

First you need to start Zend_Session before any output is sent to the browser, just like a normal php session. I do it in my index.php right after setting all my paths and autoloaders.

Zend_Session::start();

Next step is creating a namespace for the userinformation and adding the relevant information to it, preferably when you authenticate the user.

$userInfo = new Zend_Session_Namespace('userInfo');
$userInfo->userType = 'admin';

This is the equivalent to setting a $_SESSION['userInfo']['userType'] = 'admin';

Last, get the information in your layout:

<?php 
$userInfo = new Zend_Session_Namespace('userInfo');
if($userInfo->userType=='admin')
{
    echo "<li><a href="addbook.phtml">addbook</a> </li>";
}
?>

Read this link for more information http://framework.zend.com/manual/en/zend.session.html

Upvotes: 2

Jerec TheSith
Jerec TheSith

Reputation: 1942

If you want to check an user role, you could use the class Zend_Auth provided by the framework. You can check the credential of a user and then affect it a role.

To retrieve this role you can check the identity of the user with the Zend_Auth instance :

$identity = Zend_Auth::getInstance()->getIdentity();
if (strcmp($identity->role, "admin") == 0) {
    echo '<li><a href="addbook.phtml">adddbook</a> </li>';
}

Upvotes: 1

Related Questions