zend_toturial | Html | Php

Zend Framework Intro

Zend Framework Step By Step Tutorial - Part 1: Zend framework is one of popular framework at PHP World. This framework is released by Zend, a big vendor for PHP. In this post, We learn Zend framework from zero. We will build a simple application named "helloworld". In this framework, we use MVC (Model View Controller). We ever talk about this pattern at Joomla MVC. Our target is create simple application like this:

become like this:

Next post, I will explain meaning of the folders. Zend Framework Step By Step Tutorial - Part 2: We have created folder structure for helloword applicaton use zend framework. What we created is standard for Model View Controller pattern at zend framework application. In this post, we will see what function each folders. First, create folder structure like this at your web server folder (I create under www/test/zend. Up to you. You can create under www/test or just www). There are 4 top level directories within application's folder: 1. Application 2. library 3. test 4. web_root

Then, download zend framework from here. Extract compressed file. You may get structure like this: The Application Folder The application folder contains all the code required to run the application. User can not accessed directly. This is separation between display, business, and control logic modele. Under this application, there are models, views, and controllers folder. These folders contain the model, view, and controller files. Other folders still may be created, for example for configuration files. The Library Folder Copy zend library (folder named "zend" under library) to helloworld/library. Thus, your application

All applications use zend library. We place zend framework here. but, substantively, we can store library at anywhere. But make sure the application can find them. You can store at a global include directory that accessible for php application on the server, such as /usr/php_include or c:\code\php_include. Make sure set php.ini configuration file (or you can use set_include_path() function). The Test folder This folder is used to store all unit tests that are written. If you still don't know about unit test, you can read at this. many PHP programmers do not place unit test as special step. How about you? The web_root folder All web request from user are channeled through a single file, usually called index.php. This file is the only php file that needs to be accessible by web server. This file is placed in the web_root. Other common files that can be accessed directly are images, css, and JavaScript files. Each of them has own sub-directory within web_root directory. Next, we will write code for simple application named helloworld. Zend Framework Step By Step Tutorial - Part 3: As we know, at Zend Framework, index.php is a file that needed in the web root directory. This file is used for all page request. It is used for setting up the application's environtment, Zend framework's controller system, then running the application itself. This is Front Controller pattern. Create a file named "index.php" within helloworld/web_root. Enter following code:

This is the bootstrap file. Bootstrapping is the term used to describe starting the application up. The core of this code at line 9-10. This will instantiate and dispatch the front controller. It will route request to action controllers. Zend Framework Step By Step Tutorial - Part 4: We need route any requests to the front controller. We can use module from apache named "mod_rewrite". About this module, you can read at this post. Then, we will work with .htaccess file. This file will do routing job. Create a file named ".htaccess" within web_root. Then enter following code:

1 RewriteEngine On 2 RewriteRule .* index.php

At the line 2, we can see, Apache will route all request to index.php. Simple code, isn't it? Another option, you can configure it directly in Apache's httpd.conf file. We know, it is not easy way if we don't have own server. Thus, configure in a local Apache configuration file named .htaccess is better option. Zend Framework Step By Step Tutorial - Part 5: The front controller pattern maps the URL requested by the user to a particular member function within a specific controller class. We called as routing and dispatching. The controller class have a strict naming convention requirement. The router calls a function named {actionname}Action() within the {ControllerName}Controller class. This class must be within a file called {ControllerName}.php. If not provide, index will be used. Still confuse? look at this example: Create a file named "IndexController.php" within application/controllers. Enter following code:

01 <?php 02 error_reporting(E_ALL|E_STRICT); 03 ini_set('display_errors', true); 04 date_default_timezone_set('Europe/London'); 05 06 $rootDir = dirname(dirname(__FILE__)); 07 set_include_path($rootDir . '/library' . PATH_SEPARATOR . get_include_path()); 08 09 require_once 'Zend/Controller/Front.php'; Zend_Controller_Front::run 10 ('../application/controllers'); 11 12 ?> 04 class IndexController extends Zend_Controller_Action 05 { 06 07 08 09 10 11 } 12 ?> public function indexAction() { $this->view->assign('title', 'Hello, World!'); } 01 <?php 02 require_once 'Zend/Controller/Action.php'; 03

Ok, let's look at this file in more detail. Line 2-4 is used for setup environtment. Line 3 to ensure that all errors or notices are displayed. Line 4 for setup default time zone. include_path() specifies a list of directories where the require(), include() and fopen_with_path() functions look for files. You can set at php.ini. But, we don't have to do it. We can use set_include_path(). You can see at line 7.

Within the front controller system, the dispatcher expects to find a file called IndexController.php within the application/controllers directory. This file must contain a class called Indexcontroller and, as a minimum, contain a function called indexAction(). Zend Framework Step By Step Tutorial - Part 6: Now, we need to provide a view template for displaying. We will create a index.phtml file. This file is stored within the views/scripts/index. We

have a separate directory for each controllers view templates. Create a file named "index.phtml" within views/scripts/index. Enter following code:

10 11 } 12 ?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 01 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1transitional.dtd"> 02 <html xmlns="http://www.w3.org/1999/xhtml"> 03 <head> <meta http-equiv="Content-Type" 04 content="text/html; charset=utf-8" /> 05 <title><? echo $this->escape($this->title); ? ></title> 06 </head> 07 08 <body> 09 <h1><? echo $this->escape($this->title); ? ></h1>

View will catch with like this:

1 <? echo $this->escape($this->title); ?>

If you still don't know what we talk about, please read this series about Zend Framework Introduction. Ok, now, add one or more parameter such as:

01 <?php 02 require_once 'Zend/Controller/Action.php'; 03 04 class IndexController extends Zend_Controller_Action 05 { 06 public function indexAction() { $this->view->assign('title', 'Hello, World!');

10 </body> 11 </html>

The template file is executed within a member function of Zend_View and so $this is available within the template file wich is the gateway to Zend_View's functionality. All variables that have been assigned to the view from within the controller are availabel directly as properties of $this. You can see sample at above, $this->title.

07 08

Zend Framework Action
Zend Framework Action Step By Step Tutorial - Part 1: Previous post, we ever talk little about assign parameter to view. This value is sent from controller. This can happen because Action Controller. I will not talk much detail about Action Controller, but, we will learn implementation of Action Controller in web development. First, just remembering about passing value from controller to view. We wrote code like this:

$this->view->assign('wellcome','Wellcome 09 to my site. This site is built using Zend Framework. Enjoy it!'); 10 11 12 13 } 14 ?> } $this->view->assign('webmaster','Wiwit');

Then, change you "index.phtml" within application/views/scripts/index.

01 <?php 02 require_once 'Zend/Controller/Action.php'; 03 04 class IndexController extends Zend_Controller_Action 05 { 06 07 public function indexAction() {

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 01 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1transitional.dtd"> 02 <html xmlns="http://www.w3.org/1999/xhtml"> 03 <head> 04 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

$this->view->assign('title', 'Hello, 08 World!'); 09 }

05 <title><? echo $this->escape($this->title); ? ></title> 06 </head> 07

phtml" within user. $this->view->assign('title'. 03 04 class UserController extends Zend_Controller_Action 05 { 06 public function indexAction() { .Part 3: Just remember.?>. <? =$this->escape($this->name). First. http://localhost/test/zend/helloworld/web_root/ will access index controller. For simply. Zend Framework breaks URL into pieces. Now. Look at our url that we used to access hello page: http://localhost/test/zend/helloworld/web_root/. Enter following code: 05 <title><? echo $this->escape($this->title). Create a file named "index. 'Wiwit').org/TR/xhtml1/DTD/xhtml1transitional. 13 </body> 14 </html> Point your browser to http://localhost/test/zend/helloworld/web_root/. Why? as default. We have talked about controller.php'. action. Look at our code: 01 <?php 01 <?php 02 require_once 'Zend/Controller/Action.w3.w3.org/1999/xhtml"> 03 <head> 04 <meta http-equiv="Content-Type" content="text/html. ? ></h1> <?=$this->escape($this->wellcome). Next path is controller. create a folder named "user" within helloworld\application\views\scripts. create controller name "UserController". Zend Framework Action Step By Step Tutorial . We use them for jump between pages.</p> <?=$this->escape($this->webmaster). 'Hello').?> 07 08 09 10 11 12 } 13 ?> } { $this->view->assign('name'. ? ></title> 06 </head> 07 08 <body> 09 <h1><?=$this->escape($this->title). Enter following code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.?></h1> 10 </body> 11 </html> Now.0 01 Transitional//EN" "http://www. we will talk rule at Zend Framework. As we know. It is a key to understanding how the framework works. Create a file named "UserController" within application/controllers. every framework have rule about URL. we will access with http://localhost/test/zend/helloworld/web_root/user. How about we use other controller named "user"? Never mind.Part 2: URL is important part when we develop web application.dtd"> 02 <html xmlns="http://www.php'. Try to point your browser to http://localhost/test/zend/helloworld/web_root/index. charset=utf-8" /> Zend Framework Action Step By Step Tutorial . Assume http://hostname is same with http://localhost/test/zend/helloworld/web_root/. Now. We get same with http://localhost/test/zend/helloworld/web_root/. 03 04 class UserController extends Zend_Controller_Action 05 { 07 06 02 require_once 'Zend/Controller/Action. Those pieces are laid out as follows: http://hostname/controller/action/parametes. action is method in our class at controller. you don't patient to test with other controller. point your browser to http://localhost/test/zend/helloworld/web_root/user. we discuss next part. Can you catch idea? I know. Let's do it. May you get like this: Next.?> <p>&nbsp.public function indexAction() 08 <body> 09 10 11 12 <h1><? echo $this->escape($this->title). From theory above. we have rule like this: http://hostname/controller/action/parametes.

Ok. When we call this controller (such as user).phtml" within views/scrips/user.Part 4: Now.php'. 16 17 18 } 19 ?> } How to catch the parameters? Ok.0 01 Transitional//EN" "http://www.w3. <? =$this->escape($this->name).dtd"> 02 <html xmlns="http://www. 4.php within application/controller. parameters. it will call indexAction as default.?></h1> 10 </body> 11 </html> Now. 'Wiwit'). what the rule in Zend Framework? Look this URL sample: Below. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1. we can learn from sample. ? ></title> 06 </head> 07 08 <body> 1 public function nameAction() 2{ 3 4 5} $this->view->assign('name'. add with Action.?>. we talk last part or URL in Zend Framework. Naming in Zend Framework. example "nameAction" such as: 05 <title><? echo $this->escape($this->title). we have action name "index". 'Wiwit'). Thus. we have action "indexAction". $this->view->assign('title'. 'User 15 Name'). Update became like this: 01 <?php 02 require_once 'Zend/Controller/Action. but we don't specify action. $this->view->assign('title'. Controller = user Action = name username = wiwit gender = man What is your conclusion? Yeah. $this->view->assign('title'. Open UserController. 2.php'.. 09 <h1><?=$this->escape($this->title). Enter following code: .org/1999/xhtml"> 03 <head> 04 <meta http-equiv="Content-Type" content="text/html. 'Hello').w3. 1 http://hostname/user/name/username/wiwit/gender/man We can interpret like this: 1. 3. 03 04 class UserController extends Zend_Controller_Action 05 { Next. create a file named "name. 'Wiwit'). point your browser to http://localhost/test/zend/helloworld/web_root/user/name Zend Framework Action Step By Step Tutorial .org/TR/xhtml1/DTD/xhtml1transitional. charset=utf-8" /> Above. we have general formula like this: 1 http://hostname/controller/action/var1/value1/var2/value2/. complete our userController: 01 <?php 02 require_once 'Zend/Controller/Action. 03 04 class UserController extends Zend_Controller_Action 05 { 06 07 08 09 10 11 12 13 14 public function nameAction() { $this->view->assign('name'. 'Hello'). 'Wiwit'). Now.. $this->view->assign('title'. add a method in there.08 09 10 11 12 } 13 ?> } $this->view->assign('name'. } public function indexAction() { $this->view->assign('name'. 'User Name').

update your view: "name. More benefit using this class and its related classes is features that offer simple SQL database interface.Part 1: Zend Framework provides classes for supporting database.w3.org/TR/xhtml1/DTD/xhtml1transitional. 'User Name').0 01 Transitional//EN" "http://www. If we talk about database in Zend Framework.?></h1> <h2>Gender: <?=$this->escape($this->gender).?></h2> 4 <? include "footer.w3. but. $request>getParam('username')). charset=utf-8" /> public function nameAction() { $request = $this->getRequest().com 3 </div> 4 </body> 5 </html> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.phtml" within views/scripts/user. } Create a file named "header. It is clear. charset=utf-8" /> Last. Enter following code: 22 ?> Give attention at line 15-17.phtml" within views/scripts/user.?>. ? ></title> 06 </head> 07 <body> 08 <div id="header"> 09 My Zend Framework $this->view->assign('title'.? ></h2> 05 <title><? echo $this->escape($this->title).org/1999/xhtml"> 03 <head> 04 <meta http-equiv="Content-Type" content="text/html. This is basic 11 </body> 12 </html> Zend Framework Action Step By Step Tutorial . $this->view->assign('name'.dtd"> 02 <html xmlns="http://www. ?> Now. $this->view->assign('title'. 05 <title><? echo $this->escape($this->title).org/TR/xhtml1/DTD/xhtml1transitional.06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 } public function indexAction() { $this->view->assign('name'. Enter following code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1. update "name. may be this knowledge become useful for you to build web site now.phtml": 1 <div id="footer"> 2 By PHPEveryday. 'Hello'). ?> 2 3 <h1><?=$this->escape($this->title). create a file named "footer.Part 5: Last posting from this series. isn't it? Last. we talk about how to include header and footer for template. we may talk what is Zend_Db_Adapter.phtml". } 10 </div> Now.?></h1> <h2>Gender: <?=$this->escape($this>gender).phtml".0 01 Transitional//EN" "http://www.w3. I know. . Name of that class is Zend_Db.org/1999/xhtml"> 03 <head> 04 <meta http-equiv="Content-Type" content="text/html.dtd"> 02 <html xmlns="http://www. may be this is not any relation with Zend Framework Action. 'Wiwit').?>. <? =$this->escape($this->name).phtml" within views/scripts/user with this: 1 <? include "header. $this->view->assign('gender'.w3. $request>getParam('gender')). ? ></title> 06 </head> 07 08 <body> 09 10 <h1><?=$this->escape($this->title). <? =$this->escape($this->name). try to point your browser to http://localhost/test/zend/helloworld/web_root/user/ name/username/wiwit/gender/man Zend Framework Database Zend Framework Database Step By Step Tutorial .

we will create a form for inputing data. As we know. you can write PHP application for multi brands of RDBMS (with little effort). `last_name` varchar(50) NOT NULL. $this->view->assign('description'.'First 07 Name'). Then create a table named "user". PDO provide interface for multi database at PHP 5. 09 $this->view->assign('label_uname'. if we create action name "registerAction". in this practice.Part 2: First step.'User Name').?></h1> <div id="description"> <?=$this->escape($this->description).'Last 08 Name').?>"> </form> 1 CREATE TABLE `user` ( 2 3 4 5 6 7 8 `id` int(11) NOT NULL auto_increment. You can use this query: 10 11 12 $this->view->assign ('label_pass'. `first_name` varchar(50) NOT NULL. Open "UserController. create a database named "zend".dll. PHP Data Object (PDO). Zend Framework Database Step By Step Tutorial .phtml". this practise base on our previous practice at here and here. uncomment extension=php_pdo. $this->view->assign('label_fname'.php" within application/controllers.phtml". $request = $this->getRequest(). $this->view->assign ('label_submit'.phtml" within application/views/scripts/user. ?> 02 03 04 05 <h1><?=$this->escape($this->title).'Password'). Ok."process"). Ok. you can read this post. Enter following code: 01 <? include "header.dll. we must create a view named "register. then restart your apache. 18 19 20 21 22 23 24 25 26 . $this->view->assign('title'. ADOdb. Add following method: 14 15 16 17 01 public function registerAction() 02 { 03 04 05 $this->view->assign('action'. May be you remember there is extension at PHP that have function like Zend_Db_Adapter.'Member 06 Registration').extension=php_pdo_mysql.?> </div> <form name="register" method="post" action="<?=$this->escape($this->action)?>"> <table> <tr> <td><?=$this->escape($this>label_fname)?></td> <td><input type="text" name="first_name"></td> </tr> <tr> <td><?=$this->escape($this>label_lname)?></td> <td><input type="text" name="last_name"></td> </tr> <tr> <td><?=$this->escape($this>label_uname)?></td> <td><input type="text" name="user_name"></td> </tr> <tr> <td><?=$this->escape($this->label_pass)? ></td> <td><input type="password" name="password"></td> </tr> </table> <input type="submit" name="submit" value="<? =$this->escape($this->label_submit). Check in your php. 13 } As we know.'Please enter this form completely:'). We create this form at User Controller. So. We just add method named "registerAction" as action for this controller. For this practice. At PHP 4. `user_name` varchar(50) NOT NULL.ini. PRIMARY KEY (`id`). make sure that you have followed that practice. UNIQUE KEY `user_name` (`user_name`) 06 07 08 09 10 11 12 13 9 ). create a file named "register. we know a library. just remmembering. The Zend_Db_Adapters is a bridge from the vendorspecific PHP extensions to a common interface. With this class.class you use to connect your PHP application to and RDBMS.'Register'). we will use PDO for mysql (pdo_mysql). `password` varchar(255) NOT NULL. there is a different adapter class for each brand of RDBMS. First try to check availabelity pdo_mysql in your system. About PHP ADOdb. $this->view->assign('label_lname'.

phtml"."'.phtml" within application/views/scripts/user . ?> 2 <h1><?=$this->escape($this->title). ?> Step Tutorial . point your browser to http://localhost:8050/test/zend/helloworld/web_root/user/register."'))".php" within application/controllers. You will get like this: ('".`user_name` . . '". $sql = "INSERT INTO `user` (`first_name` .'Registration Process').`password`) VALUES $params = array('host' =>'localhost'. Point your browser to http://localhost/test/zend/helloworld/web_root/user/register 21 } It is simple to understand it.phtml".Part 3: After create form.? 3 ></h2> 4 <a href="list">Member List</a> 5 <? include "footer. Enter following code: 1 <? include "header.$request->getParam 15 ('last_name'). 'username' =>'root'.'Registration succes'). For this job."'. Add a method named "processAction".$request->getParam ('user_name'). enter a user information such as: 01 public function processAction() 02 { 03 04 05 06 07 08 09 10 11 12 13 14 $request = $this->getRequest(). $DB = new Zend_Db_Adapter_Pdo_Mysql($params). open "UserController.?></h1> <h2><?=$this->escape($this->description).phtml".27 <? include "footer.$request->getParam ('first_name'). `last_name` . 16 17 $DB->query($sql). we create action to input data to database. isn't it? Next. MD5('"."'. '". Then. create a file named "process. 'password' =>'admin'. 'dbname' ). create view for this processAction. =>'zend' Click registration button. $this->view->assign ('description'. now. ?> 18 19 20 $this->view->assign('title'. with following code: Now.$request->getParam ('password').

$request->getParam ('first_name'). replace our processAction become like this: 01 public function processAction() 02 { 03 04 10 11 12 $sql = "SELECT * FROM `user` ORDER BY . 'password' =>'admin'. Add following method: 1 $sql = "INSERT INTO `user` 2 3 (`first_name` . 'user_name' => $request>getParam('user_name').$request->getParam ('user_name')."'))". Then. we use ordinary query for input data. $DB = new Zend_Db_Adapter_Pdo_Mysql($params). ('". 'password' => md5($request>getParam('password')) ).$request->getParam 4 ('last_name').$params = array('host' 05 06 07 08 09 10 11 12 =>'localhost'. 'user_name' => $request->getParam ('user_name'). 'username' =>'root'. For this job. '". Zend Framework Database Step By Step Tutorial .'Registration Process'). we create a methode named "listAction" within controller.$request->getParam ('password'). $data = array('first_name' => $request>getParam('first_name'). $data). 'password' => md5($request4 >getParam('password')) 5 )."'.`user_name` . You can use like this: 13 14 15 1 $data = array('first_name' => $request->getParam ('first_name'). 5 $DB->query($sql).`password`) VALUES 01 public function listAction() 02 { 03 04 05 06 07 08 09 $params = array('host' =>'localhost'. '". $DB->setFetchMode(Zend_Db::FETCH_OBJ). 'password' =>'admin'. $this->view->assign ('description'.'Registration succes'). 'dbname' ). 'dbname' ). Open "UserController. $DB->insert('user'. MD5('". we create table of data that we have inputed. =>'zend' $request = $this->getRequest(). 'username' =>'root'. 'last_name' => $request>getParam('last_name').php" within application/controllers. 6 $DB->insert('user'. 2 3 'last_name' => $request->getParam ('last_name'). 21 22 } old query like this: Zend Framework Database Step By Step Tutorial . `last_name` . $data). Zend framework have simple way for insert query.Part 5: Now. =>'zend' Ok. $DB = new Zend_Db_Adapter_Pdo_Mysql($params). It is good alternative because simple and more neat. create view for that list.Part 4: At previous post. 16 17 18 19 20 $this->view->assign('title'."'."'.

In this practice. $request>getBaseURL(). $this->view->assign('label_fname'.'Member Editing'). Zend Framework Database Step By Step Tutorial . $DB = new Zend_Db_Adapter_Pdo_Mysql($params). ?> </td> </tr> <? } ?> </table> .phtml" within application/views/scripts/user. Enter following code: 01 <? include "header. Point your browser to http://localhost/test/zend/helloworld/ web_root/user/list Then. 01 public function editAction() 02 { 03 04 05 06 07 08 $params = array('host' =>'localhost'.?></h1> <h2><?=$this->escape($this->description).user_name ASC". $sql = "SELECT * FROM `user` WHERE id='". for($i = 1. $this->view->assign('description'."'". Open "UserController.$result). add a method in controller. our members:'). ?> 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 <h1><?=$this->escape($this->title). $this->view->assign('action'.phtml". 'password' =>'admin'. $result = $DB->fetchAssoc($sql).'First Name').$i++){ ?> <tr> <td><?=$datas[$i]['id']?></td> <td><?=$datas[$i]['user_name']?></td> <td><?=$datas[$i]['first_name']? ></td> <td><?=$datas[$i]['last_name']? ></td> <td> <a href="edit/id/<?=$datas[$i]['id']? >">Edit</a> | 09 10 11 12 13 14 15 16 17 18 19 $this->view->assign('data'.$id.Part 6: To create editing form.php" within application/controllers. 'dbname' ).$result). we add a action named "editAction". create named "list. 13 14 15 16 17 18 19 } $this->view->assign('title'.phtml". $request = $this->getRequest().? ></h2> <a href="register">Register</a> <table border="1"> <tr> <th>ID</th> <th>User Name</th> <th>First Name</th> <th>Last Name</th> <th>Action</th> </tr> <? $datas = $this->datas. $this->view->assign('datas'. $id = $request->getParam("id")."/user/processedit"). Add following method: 'username' =>'root'.'Below. $this->view->assign('title'. =>'zend' <a href="del/id/<?=$datas[$i]['id']? 23 >">Delete</a> 24 25 26 27 28 29 <? include "footer. $result = $DB->fetchRow($sql). $i<= count($datas).'Member List'). This action will show individual data that filtered by GET http parameter.

Part 7: In this post. 14 $sql = "UPDATE `user` SET `first_name` = '". 'dbname' )."' WHERE id = '".'Please update this form completely:').20 21 22 23 24 $this->view->assign('label_lname'. ?> 02 03 04 05 06 07 08 09 10 11 12 13 14 <h1><?=$this->escape($this->title). The page will jump to edit form such as: 25 } Then."'".'Editing Process'). ?> .$request->getParam('user_name'). we will see action for updating data.'Password').'User Name'). =>'zend' Zend Framework Database Step By Step Tutorial .php" within application/controller. Enter following new action: 01 public function processeditAction() 02 { <td><input type="text" name="last_name" 15 value="<?=$this->data['last_name']?>"></td> 16 17 18 </tr> <tr> <td><?=$this->escape($this>label_uname)?></td> 24 <? include "footer. $DB = new Zend_Db_Adapter_Pdo_Mysql($params). 'username' =>'root'.?> </div> <form name="edit" method="post" action="<? =$this->escape($this->action)?>"> <input type="hidden" name="id" value="<? =$this->data['id']?>"> <table> <tr> <td><?=$this->escape($this>label_fname)?></td> <td><input type="text" name="first_name" value="<?=$this->data['first_name']?>"></td> </tr> <tr> <td><?=$this->escape($this>label_lname)?></td> 08 09 10 11 12 13 19 20 21 22 23 <td><input type="text" name="user_name" value="<?=$this->data['user_name']?>"></td> </tr> </table> <input type="submit" name="submit" value="<? =$this->escape($this->label_submit).$request->getParam ('id').'Last Name'). $this->view->assign ('label_pass'.$request->getParam('first_name'). $this->view->assign('description'. $request = $this->getRequest().'Editing succes'). $this->view->assign ('label_submit'."'. Open your "UserController. `user_name` = '". $this->view->assign('description'. $DB->query($sql).?>"> </form> 18 19 15 16 17 $this->view->assign('title'.$request->getParam('last_name'). $this->view->assign('label_uname'. create a file named "edit. Then open your table data again at http://localhost/test/zend/helloworld/web_root/user/list. 03 04 05 06 07 $params = array('host' =>'localhost'.phtml". `last_name` = '"."'.phtml".phtml" within application/views/scripts/user. Enter following code: 01 <? include "header. 'password' =>'admin'. Klik a edit link at one of rows.?></h1> <div id="description"> <?=$this->escape($this->description).'Edit').

'Deleting succes'). =>'zend' Next.13 20 } 14 'user_name' => $request>getParam('user_name'). 'password' =>'admin'.?></h1> <h2><?=$this->escape($this->description).Part 8: Like insert query. $sql = "DELETE FROM `user` WHERE id='". Enter following code: 1 <? include "header. 'user_name' => $request>getParam('user_name').phtml".'id = '. ?> .'Editing Process').php" within application/controllers. $request = $this->getRequest(). 'dbname' ). 'username' =>'root'.$request->getParam('id').phtml". $DB->query($sql). For this job.'Delete Data')."'". Enter following code: 15 1 <? include "header.'id = 1'). $this->view->assign('list'.phtml". 10 11 12 13 14 15 16 $this->view->assign('title'. ?> $DB->update('user'. 'password' => md5($request>getParam('password')) ). ?> 2 <h1><?=$this->escape($this->title)."/user/list").? 3 ></h2> 4 <a href="list">Member List</a> 5 <? include "footer. create a view for this action. 01 public function delAction() 02 { 03 04 05 06 07 $params = array('host' =>'localhost'. 2 3 4 5 'last_name' => $request>getParam('last_name'). =>'zend' 6 $DB->update('user'. Open your "UserController.phtml" within views/scripts/user. $DB = new Zend_Db_Adapter_Pdo_Mysql($params).phtml" within application/views/scripts/user. 'last_name' => $request>getParam('last_name'). Create a file named "del. It is more simple and neat. Zend Framework Database Step By Step Tutorial . $params = array('host' =>'localhost'. $data. $this->view->assign('description'. Ok. Add new method for delete action (i give name "delAction"): 1 $data = array('first_name' => $request->getParam ('first_name'). try to update data from edit form. 17 18 19 20 21 } $this->view->assign('title'. 'dbname' ).$request>getBaseURL(). 'password' => md5($request>getParam('password')) ). You can use like this: Zend Framework Database Step By Step Tutorial . create a file named "processedit. Then. $data. create new method in controller. Following complete code for processeditAction method: 08 09 01 public function processeditAction() 02 { 03 04 05 06 07 08 09 10 11 12 $data = array('first_name' => $request>getParam('first_name').$request16 >getParam('id')). Zend Framework have another option to create update query. 'username' =>'root'. 'password' =>'admin'. $request = $this->getRequest().'Editing succes').Part 9: Last action from this series is delete data. $DB = new Zend_Db_Adapter_Pdo_Mysql($params). $this->view->assign('description'.

try to test from table data.php'. 003 require_once 'Zend/Controller/Action.? 3 ></h2> 4 <a href="<?=$this->escape($this->list).php'. try to update your delAction become like this: 014 015 01 public function delAction() 02 { 03 04 05 06 07 08 09 10 11 12 13 $this->view->assign('title'.'First Name'). $this->view->assign('description'. 016 017 $request = $this->getRequest(). now. Oke. $this->view->assign('label_uname'. public function indexAction() { $this->view->assign('name'.'Deleting 14 succes'). 'User Name').'Please enter this form completely:'). } $DB = new Zend_Db_Adapter_Pdo_Mysql($params). } public function registerAction() { $request = $this->getRequest(). $params = array('host' =>'localhost'. You can use this style. 022 023 024 025 026 027 028 029 030 031 $this->view->assign('action'. ?> Now.? >">Member List</a> 5 <? include "footer.'Last Name'). you are ready to read more about Zend Framework query style at Zend Framework manual guide. Zend framework have other option for delete query. 032 033 034 035 001 <?php 036 . 'Hello').$request>getParam('id')). $this->view->assign('title'. $this->view->assign('label_fname'. In this post. 'dbname' ). $request>getParam('username')).phtml".'Delete Data'). 004 005 class UserController extends Zend_Controller_Action 006 { 007 008 009 010 011 } 012 013 public function nameAction() { <h2><?=$this->escape($this->description). $request>getParam('gender')). 'id = '.$request>getBaseURL(). $request = $this->getRequest(). $DB->delete('user'.'User Name'). $this->view->assign ('label_submit'. 'password' =>'admin'. =>'zend' 018 019 020 021 $this->view->assign('title'.'Register'). and i am sure you will be like. $this->view->assign ('label_pass'. you can look complete action that we had talked. $this->view->assign('name'. 1 $DB->delete('user'. $this->view->assign('title'. Ok. Zend Framework Database Step By Step Tutorial .Part 11: This is last post from Zend Framework Database Tutorial series. $this->view->assign('gender'. 15 16 17 } $this->view->assign('list'.'Password').'Member Registration'). $this->view->assign('description'."process")."/user/list"). 'Wiwit'). $this->view->assign('label_lname'.Part 10: Like insert and update.2 <h1><?=$this->escape($this->title). 'id = 3'). Zend Framework Database Step By Step Tutorial .?></h1> 002 require_once 'Zend/Db/Adapter/Pdo/Mysql. 'username' =>'root'. Click link del at one of rows.

$DB = new Zend_Db_Adapter_Pdo_Mysql ($params). 082 083 084 } $this->view->assign('data'. 072 073 074 075 076 077 078 $request = $this->getRequest(). $sql = "SELECT * FROM `user` WHERE id='". 079 080 081 $this->view->assign('title'. =>'zend' $request = $this->getRequest(). $this->view->assign('description'.'Last Name').'Member 054 Editing').'Edit'). $data). 'last_name' => $request>getParam('last_name'). 'dbname' ).'Registration succes').'First 055 Name'). $this->view->assign('action'. $this->view->assign('label_fname'. } 'username' =>'root'."/user/processedit"). 'password' => md5($request>getParam('password')) )."'". $DB = new Zend_Db_Adapter_Pdo_Mysql ($params). 'username' =>'root'.'Please update this form completely:'). $DB = new Zend_Db_Adapter_Pdo_Mysql ($params). $this->view->assign ('datas'. $this->view->assign ('description'. $result = $DB->fetchRow($sql). $this->view->assign('description'.$id. $data = array('first_name' => $request>getParam('first_name'). $this->view->assign ('label_pass'. 'username' =>'root'. 'dbname' ). our members:'). =>'zend' . $this->view->assign ('label_submit'. $result = $DB->fetchAssoc($sql). 'password' =>'admin'.037 038 039 040 041 042 043 044 045 046 047 048 049 050 051 052 053 public function editAction() { $params = array('host' =>'localhost'. 'password' =>'admin'. 056 057 058 059 060 061 062 063 064 065 066 067 068 069 070 071 $params = array('host' =>'localhost'. $id = $request->getParam("id"). $request>getBaseURL(). 'password' =>'admin'. public function listAction() { $params = array('host' =>'localhost'. public function processAction() { $this->view->assign('label_lname'. 085 086 087 088 089 090 091 092 093 094 095 096 097 098 $sql = "SELECT * FROM `user` ORDER BY user_name ASC".'Password').'Below.$result). =>'zend' 099 100 101 102 103 104 105 } $this->view->assign('title'. $DB->insert('user'.'Member List'). $DB->setFetchMode(Zend_Db::FETCH_OBJ).'Registration Process'). 'user_name' => $request>getParam('user_name'). $this->view->assign('label_uname'.'User Name'). 'dbname' ).$result). $this->view->assign('title'.

Ok.Part 1: What is registry? It is like global storage. public function delAction() { $params = array('host' =>'localhost'. 08 09 10 require_once 'Zend/Controller/Front. thus every time we need to display name of application.$request>getBaseURL(). We put a value for this parameter. we call registry. $DB->update('user'.php'. . we practice using registry. $this->view->assign('title'.'Editing succes'). =>'zend' } 146 } 147 ?> Zend Framework Registry Step By Step Tutorial . 04 date_default_timezone_set('Europe/London'). To use registry. 11 require_once 'Zend/Registry.php" within "web_root". } 07 set_include_path($rootDir . PATH_SEPARATOR . For example. =>'zend' We set a parameter named "title". 1 require_once 'Zend/Registry. 'username' =>'root'. 'user_name' => $request>getParam('user_name')."My First Application"). Zend_Registry::set('title'.106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 public function processeditAction() { $params = array('host' =>'localhost'.'id = '. 'last_name' => $request>getParam('last_name'). 05 06 $rootDir = dirname(dirname(__FILE__)). 12 13 14 15 16 17 18 ?> Zend_Controller_Front::run ('.$request>getParam('id')). true).13: 01 <?php 02 error_reporting(E_ALL|E_STRICT).php'. $request = $this->getRequest(). get_include_path()). 'password' =>'admin'. $request = $this->getRequest().'Deleting succes').'Editing 123 Process'). We just register values then we can use throughout application. 'dbname' ).'Delete $DB->delete('user'. $DB = new Zend_Db_Adapter_Pdo_Mysql ($params)."/user/list").php from Zend Framework: $data = array('first_name' => $request>getParam('first_name'). 142 143 144 145 141 Data'). we just call from registry. $DB = new Zend_Db_Adapter_Pdo_Mysql ($params). Then. $this->view->assign ('description'. 'password' =>'admin'. 'id = '. 03 ini_set('display_errors'.$request->getParam('id')). 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 $this->view->assign('title'.php'. '/library' . Open "index. We still use our previous practice./application/controllers'). 'password' => md5($request>getParam('password')) ). $this->view->assign('list'. $data. follow this steps: 1.. Enter line 11 . $this->view->assign 124 ('description'. 'dbname' ). Please read that tutorial before. 'username' =>'root'. we can store name of application.

12 } 01 <?php 02 error_reporting(E_ALL|E_STRICT). $this->view->assign('name'. 16 Zend_Registry::set('credits'.?> 11 </body> 12 </html> Test by point your browser to http://localhost/test/zend/helloworld/web_root/user/. 'Wiwit').dtd"> 02 <html xmlns="http://www.?>. charset=utf-8" /> This is another way to read registry.org/1999/xhtml"> 03 <head> 04 <meta http-equiv="Content-Type" content="text/html. = $registry['credits']. it is like register global parameter. Update indexAction like this: 1 public function indexAction() 2{ 3 4 5 6} $title = Zend_Registry::get('title'). ? ></title> 06 </head> 07 08 <body> 09 <h1><?=$this->escape($this->title). by pointing your browser to : http://localhost/test/zend/helloworld/web_root/user/.php within application/controllers.Part 2: In this part we talk how to register array value and how to read them? Every value that we store at registry can we read as array.$arrName). Open again your "index.php'. Next. 21 ?> We register a array parameter named "credits" at line 15-16. $this->view->assign('title'. Please update view at index. Update at indexAction become: 01 public function indexAction() 02 { 03 04 $registry = Zend_Registry::getInstance(). 03 ini_set('display_errors'. get_include_path()).'Aqila Farzana'.w3.2. You may remember a function at PHP that have function like this. It can we use to read several values from registry. <? =$this->escape($this->name). we try to call/read this registry.php"./application/controllers'). true). follow this steps to understand. $this->view->assign('name'. 08 09 10 require_once 'Zend/Controller/Front. $title).php'. $this->view->assign('credits'. Zend Framework Registry Step By Step Tutorial .$credits). Test. 12 13 14 15 $arrName = array('Ilmia Fatin'. Open your UserController.w3. Yup.?></h1> credits: <?=$this->escape($this->credits). How to read it? Ok. 'Wiwit'). $this->view->assign('title'. ". 05 06 $rootDir = dirname(dirname(__FILE__)). Ok. before test it. 11 require_once 'Zend/Registry.org/TR/xhtml1/DTD/xhtml1transitional. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1. 04 date_default_timezone_set('Europe/London').0 01 Transitional//EN" "http://www. 'Imanda Fahrizal')."My First Application"). $strCredit). $title). '/library' . PATH_SEPARATOR . Zend_Registry::set('title'. Update become like this: 05 06 07 08 09 10 11 $title $credits = $registry['title'].phtml within application/views/scripts/user. 10 17 18 19 20 Zend_Controller_Front::run ('.php" within web_root. 3. $strCredit = implode(". 07 set_include_path($rootDir . . define(). we test by update again "UserController. Ok.. Update with following code: 05 <title><? echo $this->escape($this->title).

open index. '/library' . read: 1 $registry = Zend_Registry::getInstance(). $this->view->assign('name'. 013 014 015 016 017 018 019 $strCredit = implode(". 01 <?php 02 error_reporting(E_ALL|E_STRICT). = $registry['credits'].php'. 25 26 $DB->setFetchMode(Zend_Db::FETCH_OBJ). $strCredit). How to apply it? What for? Remember.$DB). 'Imanda Fahrizal'). $this->view->assign('title'. write one and store to registry.'Aqila Farzana'..php within web_root. $title). 13 14 15 16 $arrName = array('Ilmia Fatin'. Then./application/controllers').php'.php'. We can save (energy).$credits). 'password' =>'admin'. Each time connect to database. 6 $DB = new Zend_Db_Adapter_Pdo_Mysql($params). 'dbname' ).Part 3: We can store object to Zend_Registry. we can use like this: 17 Zend_Registry::set('credits'. for practice. 2 $DB = $registry['DB']. public function indexAction() { $registry = Zend_Registry::getInstance().$arrName). 9 Zend_Registry::set('DB'. 'username' =>'root'. 7 8 $DB->setFetchMode(Zend_Db::FETCH_OBJ). 12 require_once 'Zend/Db/Adapter/Pdo/Mysql.php'. } . ". 'dbname' ). $this->view->assign('credits'. 'password' =>'admin'. true).Zend Framework Registry Step By Step Tutorial . 04 date_default_timezone_set('Europe/London')."My First Application"). 11 require_once 'Zend/Registry. PATH_SEPARATOR . 27 Zend_Registry::set('DB'. To register database connection.$DB). get_include_path()). 08 09 004 005 class UserController extends Zend_Controller_Action 006 { 007 008 009 010 011 012 $title $credits = $registry['title']. 05 06 $rootDir = dirname(dirname(__FILE__)). Next. =>'zend' 1 $params = array('host' 2 3 4 5 =>'localhost'. Zend_Registry::set('title'. update "UserController. =>'zend' 24 $DB = new Zend_Db_Adapter_Pdo_Mysql($params). 28 29 30 31 32 33 ?> Zend_Controller_Front::run ('. Update become like this: 001 <?php 002 003 require_once 'Zend/Controller/Action. 'Wiwit'). 03 ini_set('display_errors'. 23 'username' =>'root'. we write connection. our practice about Zend Framework database always write database config and connection several times.php" within application/controllers become like this: Ok. 10 require_once 'Zend/Controller/Front. 18 19 $params = array('host' 20 21 22 =>'localhost'. 07 set_include_path($rootDir .

'user_name' => $request>getParam('user_name'). $this->view->assign 040 ('label_submit'. $this->view->assign('name'. $request>getBaseURL().'First Name'). $this->view->assign('label_lname'. $this->view->assign ('label_submit'. 'password' => md5($request>getParam('password')) ). $DB->insert('user'. } 060 061 062 063 $request = $this->getRequest(). $this->view->assign('label_uname'. } $request = $this->getRequest(). 082 083 081 public function registerAction() { public function processAction() { $registry = Zend_Registry::getInstance ().'Last Name'). 078 079 080 $request = $this->getRequest(). } 084 085 public function listAction() ."'". 'last_name' => $request>getParam('last_name'). $this->view->assign ('description'. 041 042 043 044 045 046 047 048 049 050 051 052 053 $sql = "SELECT * FROM `user` WHERE id='"."/user/processedit").'Member Registration').'Password').'Password').'User Name'). $this->view->assign('title'. $result = $DB->fetchRow($sql)."process").$id.'Last Name'). $DB = $registry['DB']. $id = $request->getParam("id"). $data). 065 066 067 068 069 070 071 072 073 074 075 076 077 $registry = Zend_Registry::getInstance (). $data = array('first_name' => $request>getParam('first_name'). $request>getParam('gender')).'User Name'). $DB = $registry['DB']. $request>getParam('username')). } $this->view->assign('title'. $this->view->assign('label_lname'. 064 $this->view->assign('action'. $this->view->assign('description'. $this->view->assign('title'.'Register').'Registration Process'). public function editAction() { $this->view->assign('description'.'Please update this form completely:'). $this->view->assign ('label_pass'. 057 058 059 $this->view->assign('title'. $this->view->assign('label_fname'.'Member Editing').'First Name'). $this->view->assign('gender'. 'User Name').020 021 022 023 024 025 026 027 028 029 030 031 032 033 034 035 036 037 038 039 public function nameAction() { 054 055 056 $this->view->assign('data'.'Edit'). $this->view->assign('action'. $this->view->assign('label_fname'. $request = $this->getRequest(). $this->view->assign ('label_pass'.$result). $this->view->assign('label_uname'.'Registration succes').'Please enter this form completely:').

133 134 135 } } 136 ?> Can you see it? It is simple. $sql = "SELECT * FROM `user` ORDER BY 091 user_name ASC". $DB->delete('user'. (). public function delAction() { $registry = Zend_Registry::getInstance (). name of application. 'password' => md5($request>getParam('password')) ).'Editing succes'). $DB = $registry['DB']. database name. public function processeditAction() { } $this->view->assign('title'."/user/list"). $request = $this->getRequest(). In this post.'Delete Data').Part 1: There are important information where we build a database application such as host of database.'Deleting succes').086 087 088 089 090 { 119 120 $registry = Zend_Registry::getInstance 121 122 123 124 125 126 127 128 129 130 131 132 $this->view->assign('title'.'Editing Process'). $DB = $registry['DB']. our members:'). $this->view->assign('list'. .$request->getParam('id')). $request = $this->getRequest(). These informations are retrieved extensively. For case. 092 093 094 095 096 097 098 099 100 101 102 103 104 105 106 107 108 109 110 111 112 $data = array('first_name' => $request>getParam('first_name'). $this->view->assign ('description'.'id = 113 '. $this->view->assign ('description'. 05 06 $rootDir = dirname(dirname(__FILE__)). $this->view->assign ('datas'. 03 ini_set('display_errors'. $this->view->assign('description'. 01 <?php 02 error_reporting(E_ALL|E_STRICT). user. database. is designed to simplify access to and use of configuration data within applications. isn't it? Zend Framework Config Zend Framework Configuration Step By Step Tutorial . true). and so on.'Below. 'id = '.'Member List').$request>getBaseURL(). we see how to implement zend_config use array. $result = $DB->fetchAssoc($sql). It will concise if we put them on a configuration part. 'last_name' => $request>getParam('last_name'). Zend Framework have zend_config. $data. see our code from previous tutorial: $DB->update('user'. $registry = Zend_Registry::getInstance (). 04 date_default_timezone_set('Europe/London').$result). $DB = $registry['DB']. 114 115 116 117 118 } $this->view->assign('title'. 'user_name' => $request>getParam('user_name').$request>getParam('id')).

38 01 <?php 02 error_reporting(E_ALL|E_STRICT). 40 Zend_Registry::set('credits'.$arrName). get_include_path()).. true). 'password' =>$config>database->dbpass. 'password' =>'admin'. 30 $params = array('host' >dbhost.07 set_include_path($rootDir .php.$arrName). PATH_SEPARATOR . 'dbname' >dbname ). 'appName'=>'My First Zend'. 43 44 $DB->setFetchMode(Zend_Db::FETCH_OBJ). 41 42 $DB = new Zend_Db_Adapter_Pdo_Mysql($params).php'. 04 date_default_timezone_set('Europe/London'). =>'zend' 24 $DB = new Zend_Db_Adapter_Pdo_Mysql($params). '/library' . 31 =>$config->database). Line 14 and 19-23 often use along application. '/library' . 11 require_once 'Zend/Registry. 12 require_once 'Zend/Db/Adapter/Pdo/Mysql./application/controllers'). 17 Zend_Registry::set('credits'."My First Application").$DB).'Aqila Farzana'.php'. 07 set_include_path($rootDir . 25 26 $DB->setFetchMode(Zend_Db::FETCH_OBJ).php'. 'database'=>array( 'dbhost'=>'localhost'. 08 09 10 require_once 'Zend/Controller/Front.'Aqila Farzana'. PATH_SEPARATOR . Zend_Registry::set('title'. 'webhost'=>'localhost'. 05 06 $rootDir = dirname(dirname(__FILE__)). 32 33 ?> 'username' =>$config>database->dbuser. 13 require_once 'Zend/Config. 45 Zend_Registry::set('DB'. 'dbuser'=>'root'.$DB). 'Imanda Fahrizal'). We can put into an array configuration like this: 36 37 Zend_Registry::set('title'. 'dbname' ).php'. 28 29 30 Zend_Controller_Front::run 31 ('. . 27 Zend_Registry::set('DB'. get_include_path()).php'. 11 require_once 'Zend/Registry. 03 ini_set('display_errors'. 'Imanda Fahrizal'). =>$config->database- 32 33 34 35 That is index. 18 19 $params = array('host' 20 21 22 23 =>'localhost'. 08 09 10 require_once 'Zend/Controller/Front. Location code above at web_root. 'dbpass'=>'admin' ) 'username' =>'root'.$title). 'dbname'=>'zend'.php'. 28 29 $title = $config->appName. 39 $arrName = array('Ilmia Fatin'. 12 require_once 'Zend/Db/Adapter/Pdo/Mysql. 13 14 15 16 $arrName = array('Ilmia Fatin'. 14 15 16 $arrConfig = array( 17 18 19 20 21 22 23 24 25 26 27 $config = new Zend_Config($arrConfig).php'.

24 25 $DB = new Zend_Db_Adapter_Pdo_Mysql($params).password = admin .$arrName). 'username'=>'root'. 13 require_once 'Zend/Config.php" within application.username = root 7 database. 04 date_default_timezone_set('Europe/London')./application/config. Zend Framework Configuration Step By Step Tutorial .. For example. 07 set_include_path($rootDir . 26 27 $DB->setFetchMode(Zend_Db::FETCH_OBJ). 11 require_once 'Zend/Registry.php'). Let's do it.php'. config. To call this file.$DB). PATH_SEPARATOR . 1 . Create a file named "config.php'. ) 'webhost'=>'localhost'.host = www.'Aqila Farzana'. load Zend/Config. 03 ini_set('display_errors'. we put configuration data into separated file. 16 17 $title = $config->appName. 14 First. Then create array that contains configuration (see line 1625)../application/controllers').$title)./application/config. 05 06 $rootDir = dirname(dirname(__FILE__))..php').com = My Zend Framework = localhost 6 database.Part 3: We have learn about how to use zend_config at previous post. 12 require_once 'Zend/Db/Adapter/Pdo/Mysql. 'Imanda Fahrizal').php'. Enter following sample config: This is complete code: 01 <?php 02 error_reporting(E_ALL|E_STRICT).example. example access configuration data see line 29-34. 18 $params = $config->database->toArray().php. Now. we can use like this: 33 34 ?> 1 $config = new Zend_Config(require '. get_include_path()). 'password'=>'admin' 22 $arrName = array('Ilmia Fatin'. 50 51 ?> 08 09 10 require_once 'Zend/Controller/Front. Create a file named "config.php (see line 13). Enter following code: 15 $config = new Zend_Config(require '.ini" within application. 19 20 Zend_Registry::set('title'. Create the object-oriented wrapper upon the configuration data (see line 27). we try to implement using ini file. 21 01 <? 02 return array( 03 04 05 06 07 08 09 10 11 12 ?> ). 28 Zend_Registry::set('DB'./application/controllers'). 'appName'=>'My First Zend'.php'. Next.46 47 48 Zend_Controller_Front::run 49 ('.Part 2: For easy maintenance. 'database'=>array( 'host'=>'localhost'. true). Production site configuration data 2 [app] 3 webhost 4 title 5 database. Zend Framework Configuration Step By Step Tutorial .. 23 Zend_Registry::set('credits'. 29 30 31 32 Zend_Controller_Front::run ('. '/library' . Last. 'dbname'=>'zend'.

dbname = zend 26 $DB = new Zend_Db_Adapter_Pdo_Mysql($params).'app').xml'. 20 21 Zend_Registry::set('title'. we will talk how to create simple login system at Zend Framework. 2 3 $config = new Zend_Config_Ini ('..$DB). 08 09 10 require_once 'Zend/Controller/Front.ini'. 4 5 $title = $config->appName.Part 1: Login page is a important part for database application./application/config. 15 16 $config = new Zend_Config_Ini ('. we . Enter following code: 01 <?php 02 error_reporting(E_ALL|E_STRICT). 07 set_include_path($rootDir .'app'). To read it.php'.php'. First. 03 ini_set('display_errors'.. 2 3 $config = new Zend_Config_Xml ('. '/library' . PATH_SEPARATOR . 29 Zend_Registry::set('DB'.'Aqila Farzana'. true).xml" within application.$title). 'Imanda Fahrizal').. 22 23 $arrName = array('Ilmia Fatin'. 24 Zend_Registry::set('credits'. Zend Framework Login Zend Framework Login System Step by Step Tutorial . 17 18 $title = $config->appName. 13 14 require_once 'Zend/Config/Ini. we use like this: 1 require_once 'Zend/Config/Ini. 27 28 $DB->setFetchMode(Zend_Db::FETCH_OBJ).Part 4: This is another alternative to create a configuration file. Following complete code: Zend Framework Configuration Step By Step Tutorial ./application/config. 11 require_once 'Zend/Registry.$arrName). 05 06 $rootDir = dirname(dirname(__FILE__))./application/controllers'). In this tutorial series.php'. 19 $params = $config->database->toArray().php'.'app'). create a XML file named "config.php'./application/config. We can store configuration data in a simple XML format.php'. 12 require_once 'Zend/Db/Adapter/Pdo/Mysql. 30 31 32 33 34 35 ?> Zend_Controller_Front::run ('. we can use: 1 require_once 'Zend/Config/Xml. 6 $params = $config->database->toArray(). but I will choose login with database.0"?> 02 <configdata> 03 04 05 06 07 08 09 10 11 <app> <webhost>localhost</webhost> <database> <host>localhost</host> <username>root</username> <password>admin</password> <dbname>zend</dbname> </database> </app> 12 </configdata> To access it.8 database. There are several possible scenario. 25 01 <?xml version="1. we use Zend_Config_Xml().. To read that config. get_include_path()). It means.ini'. 04 date_default_timezone_set('Europe/London').

4 5 6 7 8 9} This form send two parameter: username and password. for security reason. `real_name` varchar(50) NOT NULL. `real_name`) 3 VALUES (1. `password` varchar(255) NOT NULL. Now. Enter following Now. we use MD5(). ?> Next. create a file named "loginform. They are sent to user/auth page. Above. In this post. `password`. $this->view->assign('action'. ?> 02 <h1><?=$this->escape($this->title). $this->view->assign('username'. But. $this->view->assign('title'. create loginformAction at controller. we insert a member with user name "admin". Now. password is stored at database is 21232f297a57a5a743894a0e4a801fc3. 2 VALUES (NULL .phtml".phtml". PRIMARY KEY (`id`). Open your UserController.php within application/controllers. his password is admin. We still use same database from previous practice. `username` varchar(50) NOT NULL. point your browser to http://localhost/test/zend/helloworld/web_root/user/loginform.need database for store member data. 'User Name').?>'> <table> <tr> <td><?=$this->escape($this->username). we need login form. 'admin'. 'Administrator').phtml" within application/views/scripts/user. MD5( 'admin' ) .`username` . $this->view->assign('password'. 16 <? include "footer. 4 '21232f297a57a5a743894a0e4a801fc3'. so that why. You can use this query: code: 01 <? include "header. The database name is "zend". 'admin'. 'Password'). The table name is "users". create table for store members information."/user/auth"). `username`.`password` . Actually. Add following loginFormAction() method: 1 INSERT INTO `zend`. This is screenshot if we use phpMyAdmin. UNIQUE KEY `username` (`username`) 03 04 05 06 07 08 09 8 ).?></h1> <form method='post' action='<?=$this->escape ($this->action). First.Part 2: To build login system. You may get like this: .? ></td> <td><input type='text' name='username'></td> </tr> <tr> <td><?=$this->escape($this->password). 'Login Form').? ></td> <td><input type='password' name='password'></td> </tr> </table> <input type='submit' name='login' value='Login'> </form> 1 CREATE TABLE `users` ( 2 3 4 5 6 7 `id` int(11) NOT NULL auto_increment.`users` (`id` . Zend Framework Login System Step by Step Tutorial . try to insert sample member data for example: 10 11 12 13 14 15 1 INSERT INTO `users` 2 (`id`. we will learn how to build simple login form. 'Administrator').`real_name`) 1 public function loginFormAction() 2{ 3 $request = $this->getRequest(). $request>getBaseURL().

The data that sent by login form will be checked at authAction().Part 3: After create form login. Open your "UserController. They are sent to user/auth page. So. Just remembering. Then. load Zend/Auth. define authentication adapter: 1 <php 2 require_once 'Zend/Auth. authAction() for data validation.php'. 4 $authAdapter->setCredential(md5($paswd)). Next. } Zend Framework Login System Step by Step Tutorial . saving the result $result = $auth->authenticate($authAdapter).php'. If you still don't understand about this code. we must build authAction() at UserController. }else{ $this->_redirect('/user/loginform'). Add authAction() like this: 1 $authAdapter->setTableName('users') 2 3 ->setIdentityColumn('username') ->setCredentialColumn('password'). we need authentication. login form send two parameter: username and password.php". $paswd = $request->getParam('password').. 3 require_once 'Zend/Auth/Adapter/DbTable.php. 1 $uname = $request->getParam('username'). 4 5 . // Perform the authentication query. $request = $this->getRequest(). load database adapter from registry (line 3. if($result->isValid()){ $data = $authAdapter->getResultRowObject (null. Please place at first line before any method(): 22 23 24 25 26 27 } First. 01 public function authAction(){ 02 03 04 05 2 $paswd = $request->getParam('password'). we catch HTTP parameter that contains username and password: $registry = Zend_Registry::getInstance().'password').14 15 16 17 18 19 20 21 $uname = $request->getParam('username'). 6).php and Zend/Auth/Adapter/DbTable. $authAdapter->setIdentity($uname). 6> 1 $authAdapter = new Zend_Auth_Adapter_DbTable ($DB). $authAdapter = new Zend_Auth_Adapter_DbTable ($DB). $auth->getStorage()->write($data). please read tutorial about Zend Framework Database and Zend Framework Registry. $authAdapter->setTableName('users') ->setIdentityColumn('username') ->setCredentialColumn 1 2 $result = $auth->authenticate($authAdapter).'password'). $this->_redirect('/user/userpage'). $auth = Zend_Auth::getInstance(). Data will be checked with existing data at database. Firs.. we define table name. 06 07 08 09 10 11 12 13 // Set the input credential values 3 ('password'). Next. Next. $authAdapter->setCredential(md5($paswd)). do authentication: . 3 $authAdapter->setIdentity($uname). $DB = $registry['DB']. if($result->isValid()){ $data = $authAdapter->getResultRowObject (null. column that contains username and password.

Next post. 'Password'). $authAdapter->setTableName('users') ->setIdentityColumn ('username') ->setCredentialColumn ('password').4 5 6 7 8 9} } $auth->getStorage()->write($data). $auth = Zend_Auth::getInstance(). $authAdapter->setIdentity($uname). $data = $authAdapter->getResultRowObject (null. we will talk about protected page. $auth->getStorage()->write($data). 003 require_once 'Zend/Controller/Action. 023 024 025 026 027 028 $registry = Zend_Registry::getInstance(). this is our complete UserController: 033 034 035 036 037 // Set the input credential values $uname = $request->getParam('username'). It is simple. Then. 001 <?php 002 038 039 // Perform the authentication query. saving the result $result = $auth->authenticate ($authAdapter). $this->view->assign('gender'. $DB = $registry['DB']. 040 041 042 043 044 045 046 047 048 049 050 051 052 053 054 055 056 if($result->isValid()){ //print_r($result). }else{ $this->_redirect('/user/loginform'). $paswd = $request->getParam('password').'password'). $this->view->assign('username'. $this->_redirect('/user').php'. $request>getParam('username')). isn't it? Ok. we will save user data at session using: 029 030 1 $data = $authAdapter->getResultRowObject (null. $this->view->assign('action'. }else{ $this->_redirect('/user/loginform'). 032 031 $authAdapter = new Zend_Auth_Adapter_DbTable($DB). 2 $auth->getStorage()->write($data). If valid user. 005 require_once 'Zend/Auth/Adapter/DbTable. $authAdapter->setCredential(md5($paswd)). $this->_redirect('/user/userpage').php'.php'. $this->view->assign('password'.'password'). 006 007 class UserController extends Zend_Controller_Action 008 { 009 010 011 012 013 014 015 016 017 018 019 020 021 022 public function authAction(){ $request = $this->getRequest(). $this->view->assign('title'. } } public function nameAction() { $request = $this->getRequest(). $request>getParam('gender')). page will be redirect to protected page (named userpage in this tutorial)."/user/auth"). $this->view->assign('name'. $request>getBaseURL(). 'Login Form'). 004 require_once 'Zend/Auth. . } 057 058 059 public function loginFormAction() { $request = $this->getRequest(). 'User Name').

'Please enter this form completely:').'Registration succes'). public function editAction() { $registry = Zend_Registry::getInstance ().'Password'). . $this->view->assign ('description'. $this->view->assign ('label_submit'. $this->view->assign('label_lname'.$result).060 $this->view->assign('title'."process").'First Name'). $data = array('first_name' => $request>getParam('first_name'). $this->view->assign('description'.'Last $sql = "SELECT * FROM `user` WHERE id='". $DB = $registry['DB']. 114 115 116 117 118 119 120 121 122 123 124 125 110 111 112 $this->view->assign('action'. 098 096 097 } 094 095 Name').'Edit'). 'last_name' => $request>getParam('last_name'). $this->view->assign('title'.'Member Editing').$id.'First Name'). $this->view->assign('label_uname'. $this->view->assign('title'."/user/processedit"). $DB->insert('user'. $id = $request->getParam("id"). $this->view->assign ('label_submit'.'User Name'). $this->view->assign('label_uname'. $this->view->assign ('label_pass'. } 100 101 102 103 104 105 106 107 108 109 public function registerAction() { $request = $this->getRequest(). } public function processAction() { $registry = Zend_Registry::getInstance (). $request = $this->getRequest().'Register'). $this->view->assign('description'. $this->view->assign('title'. 'password' => md5($request>getParam('password')) ). 113 $DB = $registry['DB']."'". 'User 061 Name'). $request = $this->getRequest(). } public function listAction() { $registry = Zend_Registry::getInstance ().'User Name'). $this->view->assign('label_fname'. $data). $this->view->assign('label_lname'. $this->view->assign('action'.'Registration Process'). $request>getBaseURL(). 062 063 064 065 066 067 099 068 069 070 071 072 073 074 075 076 077 078 079 080 081 082 083 084 085 086 087 088 089 090 091 092 093 $this->view->assign('data'. $this->view->assign('label_fname'. $result = $DB->fetchRow($sql). $DB = $registry['DB'].'Password'). 'user_name' => $request>getParam('user_name').'Member Registration'). $sql = "SELECT * FROM `user` ORDER BY user_name ASC".'Please update this form completely:'). $this->view->assign ('label_pass'.'Last Name').

'id = 147 '. $DB = $registry['DB']. Ok.'Editing succes'). let's fix this problem. $data.'Delete Data'). Sadly. public function delAction() { $registry = Zend_Registry::getInstance (). 166 167 168 169 } 170 ?> } Zend Framework Login System Step by Step Tutorial .php on line 340. 160 161 162 163 164 165 $this->view->assign('title'. $DB = $registry['DB']. 'password' => md5($request>getParam('password')) ).$request>getParam('id')). I decide to fix at core of Zend library. I get error message like this: Fatal error: Cannot use object of type stdClass as array in C:\AppServ5 \www\test\zend\helloworld\ library\Zend\Auth\Adapter\DbTable. Do you want to share idea? . $DB->delete('user'. I still didn't have much time to know about this error when I was writing this post. $this->view->assign ('description'. 'last_name' => $request>getParam('last_name'). public function processeditAction() { } $this->view->assign ('datas'. Become: 1 if ($resultIdentity>zend_auth_credential_match != '1') { Then."/user/list"). Update line 340: Before: 1 if ($resultIdentity ['zend_auth_credential_match'] != '1') { $DB->update('user'.php within library\Zend\Auth\Adapter\.'Member 128 List'). If you are same like me. 'user_name' => $request>getParam('user_name').Part 4: We run previous post. I will tell you latter if I find fixing this error more better than this post.'Below. update line 346: 1 unset($resultIdentity ['zend_auth_credential_match']). $this->view->assign('title'. 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 $data = array('first_name' => $request>getParam('first_name'). $request = $this->getRequest(). Become: 1 unset($resultIdentity>zend_auth_credential_match). } $this->view->assign('title'. $registry = Zend_Registry::getInstance ().'Deleting succes').126 127 $result = $DB->fetchAssoc($sql). open DbTable. So.'Editing Process'). 129 our members:'). Skip this post if you don't fine any error at previous post. I will update this post if there is more better than this way. $this->view->assign ('description'.$request>getBaseURL(). $this->view->assign('list'. 148 149 150 151 152 153 154 155 156 157 158 159 $request = $this->getRequest().$result).$request->getParam('id')). $this->view->assign('description'. 'id = '.

we try to protect a page.php" within application/controllers.Part 6: At protected page (in previous post).$logoutUrl).6 we needed to protect page from unlogin users.php'. Then. This post talk about logout page. Zend Framework Login System Step by Step Tutorial . Update indexAction(): 01 public function userPageAction(){ 02 03 04 05 06 07 08 09 10 11 12 13 14 $this->view->assign('username'. ?> Then. Add following method: We just use clearIdentity(). we had created a link to logout. 005 require_once 'Zend/Auth/Adapter/DbTable. $real_name).?></h1> 3 <a href='<?=$this->escape($this->urllogout). below complete code for controller for this session: Line 2 . we create a method for controller named logoutAction().Step by Step Tutorial . <?=$this->escape($this->username). 16 } Ok.phtml". You can point your browser to http://localhost/test/zend/helloworld/web_root/user/loginform.php" within application/controllers.phtml". $auth = Zend_Auth::getInstance(). Open "UserController. Add following method: 1 public function logoutAction() 2{ 3 4 5 6} $auth = Zend_Auth::getInstance(). $this->view->assign 15 ('urllogout'. Create a file named "logout. When visitors access this page. 004 require_once 'Zend/Auth. We will modify front page (user/index). if(!$auth->hasIdentity()){ $this->_redirect('/user/loginform'). First. Open "UserController. Thus. }else{ $this->_redirect('/user/userpage'). Try to login. they will be redirected to form login. Leave blank (because we don't want to show anything). ?> 2 <h1>Hello.Part 5: We have created authentication at previous post.php".phtml" within application/views/scripts/user. 006 Zend Framework Login System Step by Step Tutorial . $request = $this->getRequest().?>'>Logout</a> 4 <? include "footer. when not member try to enter the page. create view for logout. 001 <?php 002 003 require_once 'Zend/Controller/Action. Now.'/user/logout'.phtml" within application/views/scripts/user.php'. $user = $auth->getIdentity().Part 7: This is last post from this tutorial series. First.php'. Then redirect page to another page (in this sample to user). 1 <? include "header. 01 public function indexAction() 02 { 03 04 05 06 07 08 09 10 } } $request = $this->getRequest(). 007 class UserController extends Zend_Controller_Action 008 { 009 010 011 public function indexAction() { $request = $this->getRequest(). $logoutUrl = $request->getBaseURL (). Create a file named "userpage. $auth = Zend_Auth::getInstance(). . $real_name = $user->real_name. $auth->clearIdentity(). create view. Open "UserController. we add method at controller. $this->_redirect('/user'). they will be redirected to login form. Then login. $username = $user->username. } if(!$auth->hasIdentity()){ $this->_redirect('/user/loginform').

$data = $authAdapter->getResultRowObject (null. saving the result $result = $auth->authenticate ($authAdapter). }else{ $this->_redirect('/user/userpage').'/user/logout'. if($result->isValid()){ //print_r($result). 'Password'). } public function logoutAction() { $auth = Zend_Auth::getInstance(). 035 $real_name). 065 066 067 068 069 070 071 072 073 074 075 076 077 078 079 080 081 082 083 084 } $real_name = $user->real_name. $paswd = $request->getParam('password'). 'Login Form').$logoutUrl). $authAdapter->setTableName('users') ->setIdentityColumn ('username') ->setCredentialColumn ('password')."/user/auth"). $this->view->assign 036 ('urllogout'. } 060 061 062 063 // Set the input credential values $uname = $request->getParam('username'). 'User Name'). $request>getBaseURL(). $this->_redirect('/user'). $authAdapter->setIdentity($uname). $this->view->assign('title'. }else{ $this->_redirect('/user/loginform'). 059 if(!$auth->hasIdentity()){ $this->_redirect('/user/loginform'). $this->view->assign('username'. if(!$auth->hasIdentity()){ $this->_redirect('/user/loginform'). if(!$auth->hasIdentity()){ $this->_redirect('/user/loginform'). } $request = $this->getRequest(). $logoutUrl = $request->getBaseURL (). $this->view->assign('action'. 037 038 039 040 041 042 043 044 045 046 047 048 049 public function authAction(){ } 085 086 087 public function loginFormAction() { $request = $this->getRequest(). $username = $user->username.012 013 014 015 016 017 018 019 020 021 } $auth = Zend_Auth::getInstance(). $auth = Zend_Auth::getInstance(). 028 064 029 030 031 032 033 034 $this->view->assign('real_name'.'password'). $authAdapter->setCredential(md5($paswd)). $user = $auth->getIdentity(). } $registry = Zend_Registry::getInstance(). $auth->getStorage()->write($data). $authAdapter = new Zend_Auth_Adapter_DbTable($DB). . // Perform the authentication query. $this->view->assign('password'. public function userPageAction(){ 057 058 022 023 024 025 026 027 $auth = Zend_Auth::getInstance(). 050 051 052 053 054 055 056 $request = $this->getRequest(). $DB = $registry['DB'].

'Member Editing'). 148 149 150 if(!$auth->hasIdentity()){ $this->_redirect('/user/loginform'). $this->_redirect('/user'). } .088 089 090 091 092 093 094 095 096 097 098 099 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 } } 126 Name')."/user/processedit"). $result = $DB->fetchRow($sql). $this->view->assign('label_lname'. $DB = $registry['DB'].'Please update this form completely:'). $this->view->assign('label_uname'. $this->view->assign('data'. $this->view->assign('description'.'Member Registration').$id."process").'Register'). } 135 136 137 138 139 $request = $this->getRequest().'First Name').'Please enter this form completely:'). 154 155 $this->view->assign('action'. } 152 153 $request = $this->getRequest(). $id = $request->getParam("id"). $this->view->assign ('label_submit'.'Edit'). 140 141 142 143 144 $this->view->assign('title'. $this->view->assign ('label_submit'. $this->view->assign('label_uname'.'User 156 157 158 159 151 public function editAction() { $auth = Zend_Auth::getInstance(). $this->view->assign('label_fname'. } 145 146 147 public function registerAction() { $auth = Zend_Auth::getInstance().'User Name'). 133 134 if(!$auth->hasIdentity()){ $this->_redirect('/user/loginform'). $request>getParam('gender')).'Password'). 127 $this->view->assign ('label_pass'. } $auth->clearIdentity(). $request>getBaseURL(). $this->view->assign('description'. $this->view->assign ('label_pass'. $request = $this->getRequest(). $sql = "SELECT * FROM `user` WHERE id='".'Last Name'). if(!$auth->hasIdentity()){ $this->_redirect('/user/loginform').'Last Name').$result). $this->view->assign('action'.'Password'). $this->view->assign('title'. 128 129 public function nameAction() { 130 131 132 $auth = Zend_Auth::getInstance(). $this->view->assign('name'.'First Name'). $request>getParam('username')). } $registry = Zend_Registry::getInstance (). $this->view->assign('gender'. $this->view->assign('label_fname'. 'User Name')."'". $this->view->assign('title'. $this->view->assign('label_lname'.

227 228 195 $registry = Zend_Registry::getInstance (). $DB = $registry['DB'].160 161 162 163 164 165 166 167 168 169 170 171 172 173 $request = $this->getRequest(). $sql = "SELECT * FROM `user` ORDER BY user_name ASC". } $this->view->assign('title'. $data = array('first_name' => $request174 >getParam('first_name'). 'user_name' => $request>getParam('user_name'). .$request->getParam('id')). $request = $this->getRequest(). $data). 'password' => md5($request>getParam('password')) ). } if(!$auth->hasIdentity()){ $this->_redirect('/user/loginform').'Registration succes').'id = 226 '. $DB->insert('user'. public function listAction() { $auth = Zend_Auth::getInstance(). 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 } if(!$auth->hasIdentity()){ $this->_redirect('/user/loginform'). $this->view->assign('title'.'Registration Process'). $registry = Zend_Registry::getInstance ().'Editing Process'). public function processAction() { $auth = Zend_Auth::getInstance(). $this->view->assign ('description'. 'last_name' => $request>getParam('last_name'). public function processeditAction() { $auth = Zend_Auth::getInstance(). 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 } $DB = $registry['DB']. 'password' => md5($request>getParam('password')) ). $DB = $registry['DB']. $registry = Zend_Registry::getInstance (). if(!$auth->hasIdentity()){ $this->_redirect('/user/loginform'). $result = $DB->fetchAssoc($sql). 229 $this->view->assign('title'.'Below. } 214 215 216 217 218 219 220 221 222 223 224 225 $data = array('first_name' => $request>getParam('first_name'). 'last_name' => $request>getParam('last_name'). $this->view->assign ('datas'. $DB->update('user'. $data. 'user_name' => $request>getParam('user_name'). $this->view->assign('description'. $this->view->assign ('description'.$result). our members:').'Editing succes').'Member List').

If you are not familiar with namespace.$request>getParam('id')). we will learn to implement namespace into session at Zend Framework. $this->view->assign('list'. Below. we know $_SESSION. Edit loginFormAction() method become like this: 01 public function loginFormAction() 02 { 03 $request = $this->getRequest(). Zend Framework Session Step By Step Tutorial . web browser). Support exists for both anonymous and authenticated (e. And a important class that we must know is Zend_Session_Namespace. . the authentication component of the Zend Framework. Zend_Session_Namespace provides a standardized. maintains the association between a client and session state data. whether conveyed by cookie or within URLs.'Delete Data'). namespace is like name of groups of associative arrays.php. It is simple. For practice. as well as providing an API for Zend_Session_Namespace to persist session namespaces.. Thus. Still confuse? Don't worry. At Zend Framework. usually. When we build complex application. Simply. $request = $this->getRequest(). and user agent client (e. Zend_Session manage server-side data stored in $_SESSION. 1 require_once 'Zend/Session/Namespace. Since Zend_Session uses the normal PHP ext/session functions internally. open your UserController."/user/list"). persistent state data. object-oriented interface for working with namespaces persisted inside PHP's standard session mechanism. is quotes from zend framework manual: To do this. keyed by strings. we can use Zend_Session. one to one connection between server-site. $DB = $registry['DB']. $this->view->assign ('description'. we involved session. $DB->delete('user'.php within application/controllers. all the familiar configuration options and settings apply (see http://www. uses Zend_Session_Namespace to store some information associated with authenticated users.g.net/session). Include Zend/Session/Namespace.php at first line after delimeter.php'.g. } Zend_Session_Namespace instances are accessor objects for namespaced slices of $_SESSION. don't worry. we use our previous practice. In PHP. $registry = Zend_Registry::getInstance (). next post we will try to practice about session at Zend Framework. 'id = '.Part 1: Session is a familiar word in PHP programming. keyed by strings (similar to normal PHP arrays). Ok. The Zend_Session component wraps the existing PHP ext/session with an administration and management interface. with such bonuses as the convenience of an object-oriented interface and default behavior that provides both best practices and smooth integration with the Zend Framework. It represent a logical. See this picture: Zend Framework Session Zend Framework Session Step By Step Tutorial . Zend_Auth. } if(!$auth->hasIdentity()){ $this->_redirect('/user/loginform'). public function delAction() { $auth = Zend_Auth::getInstance(). Let's do it. we want to count number of a particular page request form a visitor.$request>getBaseURL(). We will begin with Zend_Session_namespace.'Deleting succes'). "login") session namespaces.230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 } 256 ?> } $this->view->assign('title'. a standard PHP session identifier. Session namespaces provide access to session data using classic namespaces implemented logically as named groups of associative arrays.Part 2: In this post.

You may get screen like picture above. $this->view->assign('password'. <?=$this->escape($this->real_name). etc. ?> .?></h1> You have entered this page: <?=$this->escape 03 ($this->request). modify loginform. we named "HelloWorld".phtml: 1 <? include "header. <a href='<?=$this->escape($this->urllogout). $this->view->assign('username'. $this->view->assign('username'. Next. try again for userPageAction() at controller. ?> 2 3 4 <h1>Hello. Ok. } if(!isset($ns->yourLoginRequest)){ $ns->yourLoginRequest = 1. 'User Name'). It is like $_SESSION['HelloWorld']. 04 <form method='post' action='<?=$this->escape ($this->action).? ></h1> You have entered this page: <?=$this->escape ($this->request). 'Login Form'). $this->view->assign('password'.phtml". Update become like this: 01 public function loginFormAction() 02 { 03 04 05 06 07 08 09 10 11 12 13 14 $this->view->assign('request'.phtml". at line 7.phtml". $this->view->assign('action'. }else{ $ns->yourLoginRequest++.04 05 $ns = new Zend_Session_Namespace ('HelloWorld'). Update become like this: 01 <? include "header. 15 16 $this->view->assign('title'. } if(!isset($ns->yourLoginRequest)){ $ns->yourLoginRequest = 1. 17 'Password'). update its view at application/views/scripts/user/userpage.? ></td> <td><input type='password' name='password'></td> </tr> 15 16 17 18 19 } Then. }else{ $ns->yourLoginRequest++. 06 07 08 09 10 11 12 13 $this->view->assign('request'.? ></td> <td><input type='text' name='username'></td> </tr> <tr> <td><?=$this->escape($this->password). myapplication.?>'> 05 06 07 08 09 10 11 12 13 <table> <tr> <td><?=$this->escape($this->username). 'User Name'). $request = $this->getRequest().phtml".? >'>Logout</a> 5 <? include "footer. $this->view->assign('action'. $this->view->assign('title'."/user/auth").?> time(s). The namespace. You can put name such as mynamespace. we check value of $ns->yourLoginRequest. we define object of namespace.?> time(s). $request>getBaseURL(). ?> 02 <h1><?=$this->escape($this->title). $ns>yourLoginRequest). It is like $_SESSION['HelloWorld'] ['yourLoginRequest']. point your browser to http://localhost/test/zend/helloworld/web_root/user/loginform. $request14 >getBaseURL()."/user/auth"). 14 15 16 </table> <input type='submit' name='login' value='Login'> </form> 17 <? include "footer. $ns = new Zend_Session_Namespace ('HelloWorld'). 'Login Form'). $ns>yourLoginRequest). 18 19 } At line 5. Next. 'Password').phtml within application/views/scripts/user. ?> Now.

2 echo $ns->thisIsSession. we can not update or delete value of session. $ns->yourLoginRequest++. 5 }else{ 7 8} Then. use isLocked(). 3 echo $ns->nameOfSession. Then. how to access the values? Just simple: Zend Framework Session Step By Step Tutorial . As we know. Look this sample: 1 echo $ns->yourLoginRequest. }else{ $ns->yourLoginRequest++. Now. 2 3 if(!isset($ns->yourLoginRequest)){ 4 6 7} $ns->yourLoginRequest = 1. } } Zend Framework Session Step By Step Tutorial . Just give properties such as: 1 ns->yourLoginRequest = '2'. 2 foreach ($ns as $index => $value) { 3 4} echo "ns->$index = '$value'. 2 $ns->thisIsSession = "Oke". we talk about how to access the session. try to point your browser to http://localhost/test/zend/helloworld/web_root/user/stats. 01 public function loginFormAction() 02 { 03 04 05 $ns = new Zend_Session_Namespace ('HelloWorld'). So. Create a file named "stats. Now. You may get such as: From above. What's that mean? When a namespace session is locked. we know how to set session at Zend framework. Please. 2 ns->yourUserPageRequest = '7'.phtml" within application/views/scripts/user. $request- 1 public function statsAction() 2{ 15 16 .php" within application/controllers. We have put session at loginform and userpage page. we learn how to access session data. 3 $ns->nameOfSession = "MySession". 4 echo $ns->foo.Part 5: Session namespaces can be locked. 06 07 08 09 10 11 12 13 14 Ok. $this->view->assign('request'. foreach ($ns as $index => $value) { echo "ns->$index = '$value'. Now. $ns->lock(). echo "<br />".Zend Framework Session Step By Step Tutorial . Ok. Open your "UserController. if want to check whether locked or not. it will be read-only. try to test it. we want to show request statistic for that page. Leave blank. $this->view->assign('action'. Values depend on number of visiting. Add following method: $request = $this->getRequest().Part 3: In this post.". session can have behavior like array. 4 $ns->foo = 10. we can show values like array. } 1 $ns = new Zend_Session_Namespace('HelloWorld'). We can use lock() to lock namespace.Part 4: This is a little tips to show all value at a namespace. $ns>yourLoginRequest). 1 $ns->yourLoginRequest = 1. if (!$ns->isLocked()) { if(!isset($ns->yourLoginRequest)){ $ns->yourLoginRequest = 1.". create a view for this action controller. give attention a code at previous post: 3 4 5 6 $ns = new Zend_Session_Namespace ('HelloWorld'). Look this sample: 1 $ns = new Zend_Session_Namespace('HelloWorld').

Zend Framework Session Step By Step Tutorial . 1 $ns->setExpirationSeconds (60. 'User Name').g. you can use unLock() method. $this->view->assign('password'."/user/auth"). we want to count number of page request by a person in one minute. 19 'Password'). yourLoginRequest): 1 $ns= new Zend_Session_Namespace('HelloWorld'). You can set only at a particular key (e. . $this->view->assign('request'. $this->view->assign('title'. }else{ $ns->yourLoginRequest++. 15 16 17 18 19 $this->view->assign('action'. 20 21 } 20 } To unlock. After 1 minute.>getBaseURL(). So. 'Login Form'). we called namespace expiration. $this->view->assign('password'."/user/auth"). Update loginFormAction(): 01 public function loginFormAction() 02 { 03 04 05 06 07 08 09 10 11 12 13 14 $request = $this->getRequest(). the code like this: We try at loginform.'yourLoginRequest'). $ns->setExpirationSeconds(60). Open UserController. $request>getBaseURL().Part 6: We can set limited time for a namespace. $ns>yourLoginRequest). 17 18 $this->view->assign('title'. $this->view->assign('username'. 'Login Form'). he will be counted as new person. 2 3 // marking session as read only locked 4 $ns->lock().php within application/controllers. 'Password'). Example. For this feature. 5 6 // unlocking read-only lock 7 if ($ns->isLocked()) { 8 9} $ns->unLock(). if(!isset($ns->yourLoginRequest)){ $ns->yourLoginRequest = 1. } $ns = new Zend_Session_Namespace ('HelloWorld'). $this->view->assign('username'. 'User Name').

Sign up to vote on this title
UsefulNot useful