phpeveryday.com

The best tutorial of php, php framework, php strategies, object oriented oriented,


Zend Framework Session: Using Namespace

Tag: framework, zend, zend framework, session, namespace   Category: PHP Framework
post: 23 Apr 2008 read: 888


Zend Framework Session Step By Step Tutorial - Part 2: In this post, we will learn to implement namespace into session at Zend Framework. If you are not familiar with namespace, don't worry. It is simple. Let's do it.

Simply, namespace is like name of groups of associative arrays, keyed by strings. For practice, we use our previous practice.

Ok, we want to count number of a particular page request form a visitor. See this picture:

count number of page request

To do this, open your UserController.php within application/controllers. Include Zend/Session/Namespace.php at first line after delimeter.


require_once 'Zend/Session/Namespace.php';

Edit loginFormAction() method become like this:


  public function loginFormAction()
  {
    $request = $this->getRequest();  
	
    $ns = new Zend_Session_Namespace('HelloWorld');
    
	if(!isset($ns->yourLoginRequest)){
	  $ns->yourLoginRequest = 1;
	}else{
	  $ns->yourLoginRequest++;
	}
    
	$this->view->assign('request', $ns->yourLoginRequest);
	$this->view->assign('action', $request->getBaseURL()."/user/auth");  
    $this->view->assign('title', 'Login Form');
    $this->view->assign('username', 'User Name');	
    $this->view->assign('password', 'Password');	
	    
  }

At line 5, we define object of namespace. The namespace, we named "HelloWorld". It is like $_SESSION['HelloWorld']. You can put name such as mynamespace, myapplication, etc.

Next, at line 7, we check value of $ns->yourLoginRequest. It is like $_SESSION['HelloWorld']['yourLoginRequest']. Next, modify loginform.phtml within application/views/scripts/user. Update become like this:


<? include "header.phtml"; ?>
  <h1><?=$this->escape($this->title);?></h1>
  You have entered this page: <?=$this->escape($this->request);?> time(s).
  <form method='post' action='<?=$this->escape($this->action);?>'>
  <table>
    <tr>
	  <td><?=$this->escape($this->username);?></td>
	  <td><input type='text' name='username'></td>
	</tr>
    <tr>
	  <td><?=$this->escape($this->password);?></td>
	  <td><input type='password' name='password'></td>
	</tr>	
  </table>
  <input type='submit' name='login' value='Login'>
  </form>
<? include "footer.phtml"; ?>

Now, point your browser to http://localhost/test/zend/helloworld/web_root/user/loginform. You may get screen like picture above.

Ok, try again for userPageAction() at controller. Update become like this:


  public function loginFormAction()
  {
    $request = $this->getRequest();  
	
    $ns = new Zend_Session_Namespace('HelloWorld');
    
	if(!isset($ns->yourLoginRequest)){
	  $ns->yourLoginRequest = 1;
	}else{
	  $ns->yourLoginRequest++;
	}
    
	$this->view->assign('request', $ns->yourLoginRequest);
	$this->view->assign('action', $request->getBaseURL()."/user/auth");  
    $this->view->assign('title', 'Login Form');
    $this->view->assign('username', 'User Name');	
    $this->view->assign('password', 'Password');	
	    
  }

Then, update its view at application/views/scripts/user/userpage.phtml:


<? include "header.phtml"; ?>
<h1>Hello, <?=$this->escape($this->real_name);?></h1>
You have entered this page: <?=$this->escape($this->request);?> time(s).
<a href='<?=$this->escape($this->urllogout);?>'>Logout</a>
<? include "footer.phtml"; ?>


Series this article:
Zend Framework Session: Introduction
Zend Framework Session: Using Namespace
Zend Framework Session: Accessing Session Data
Zend Framework Session: Seing All Values at Namespace
Zend Framework Session: Locking and Unlocking Namespace
Zend Framework Session: Automatic Expiration

| Give Your Opinion | Recommend
Share and Bookmark to: These icons link to social bookmarking sites where readers can share and discover new web pages.
digg del.icio.us technorati Ma.gnolia BlinkList

Recommended articles by other readers:
Web Services: How PHP Kiss VB.NET? (Part 1)
Chart: How to Build Cool Animation Real Time Chart
Joomla: Fast Road to Understand Component Programming
Email: Send Attachement Mail
mod_rewrite - Part 1: create your "fantasy" URL

What do You Think?
Your Name *:
Email *:
(Will not be published)
Website/URL:
Your Comment *:
* Required


615
posting