Zend framework tutorial

Description

Zend framework tutorial

Reviews
Shared by: Harsh Gupta
Categories
Tags
Stats
views:
7478
rating:
10(1)
reviews:
0
posted:
6/25/2008
language:
pages:
0
Zend Framework Intro: Folder Structure Tag: framework, zend, zend framework, folder, structure, MVC Category: PHP Framework post: 08 Apr 2008 read: 2,211 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: 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). Then, download zend framework from here. Extract compressed file. You may get structure like this: Copy zend library (folder named "zend" under library) to helloworld/library. Thus, your application become like this: Next post, I will explain meaning of the folders. Zend Framework Intro: Explaining Anatomy of Zend Framework Application Tag: framework, zend, zend framework, anatomy application, MVC   Category: PHP Framework  post: 08 Apr 2008 read: 1,960      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.   There are 4 top level directories within application's folder: 1. Application 2. library 3. test 4. web_root 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  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 Intro: Creating Index.php as Single Access File Tag: framework, zend, zend framework, access file, controller, MVC Category: PHP Framework post: 08 Apr 2008 read: 1,845 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: 1 view plain | print 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. 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 Intro: Creating Apache .htaccess Tag: framework, zend, zend framework, htaccess, mod_rewrite, apache Category: PHP Framework post: 08 Apr 2008 read: 1,778 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: 1RewriteEngine On 2RewriteRule .* index.php view plain | print 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 Intro: Creating Controller Tag: framework, zend, zend framework, controller, MVC Category: PHP Framework post: 09 Apr 2008 read: 1,976 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: 1 view->assign('title', 'Hello, World!'); 9 } 10 11} 12?> view plain | print 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 Intro: Creating View Tag: framework, zend, zend framework, access file, view, MVC Category: PHP Framework post: 09 Apr 2008 read: 1,787 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: 1 2 3 4 5 <? echo $this->escape($this->title); ?> 6 7 8 9

escape($this->title); ?>

10 11 view plain | print 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. ====================================================================================    ====================================================================================    Zend Framework Action: Dynamic Content Tag: framework, zend, zend framework, actions, dynamic content Category: PHP Framework post: 11 Apr 2008 read: 1,587 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: 1 view->assign('title', 'Hello, World!'); 9 } 10 11} 12?> view plain | print View will catch with like this: 1escape($this->title); ?> view plain | print 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: 1 2 3 4 5 6 7 8 view->assign('title', 'Hello, World!'); $this->view9 >assign('wellcome','Wellcome to my site. This site is built using Zend Framework. Enjoy it!'); 10 $this->view->assign('webmaster','Wiwit'); 11 } 12 13} 14?> view plain | print Then, change you "index.phtml" within application/views/scripts/index. 2 3 4 5 <? echo $this->escape($this->title); ?> 6 7 8 9

escape($this->title); ?>

10 escape($this->wellcome);?> 11

 

12 escape($this->webmaster);?> 13 14 view plain | print Point your browser to http://localhost/test/zend/helloworld/web_root/. May you get like this: Zend Framework Action: URL Structure and Controller Tag: framework, zend, zend framework, actions, url structure, controller Category: PHP Framework post: 11 Apr 2008 read: 1,215 Zend Framework Action Step By Step Tutorial - Part 2: URL is important part when we develop web application. We use them for jump between pages. As we know, every framework have rule about URL. It is a key to understanding how the framework works. Now, we will talk rule at Zend Framework. Zend Framework breaks URL into pieces. Those pieces are laid out as follows: http://hostname/controller/action/parametes. Look at our url that we used to access hello page: http://localhost/test/zend/helloworld/web_root/. Assume http://hostname is same with http://localhost/test/zend/helloworld/web_root/. Next path is controller. Try to point your browser to http://localhost/test/zend/helloworld/web_root/index. We get same with http://localhost/test/zend/helloworld/web_root/. Why? as default, http://localhost/test/zend/helloworld/web_root/ will access index controller. Can you catch idea? I know, you don't patient to test with other controller. How about we use other controller named "user"? Never mind. From theory above, we will access with http://localhost/test/zend/helloworld/web_root/user. Let's do it. First, create controller name "UserController". Create a file named "UserController" within application/controllers. Enter following code: 1 view->assign('name', 'Wiwit'); 9 $this->view->assign('title', 'Hello'); 10 } 11 12} 13?> view plain | print Next, create a folder named "user" within helloworld\application\views\scripts. Create a file named "index.phtml" within user. Enter following code: 2 3 4 5 <? echo $this->escape($this->title); ?> 6 7 8 9

escape($this->title);?>, escape($this->name);?>

10 11 view plain | print Now, point your browser to http://localhost/test/zend/helloworld/web_root/user. Zend Framework Action: URL Structure and Action Tag: framework, zend, zend framework, actions, url structure, controller Category: PHP Framework post: 11 Apr 2008 read: 1,186 Zend Framework Action Step By Step Tutorial - Part 3: Just remember, we have rule like this: http://hostname/controller/action/parametes. We have talked about controller. Now, we discuss next part, action. For simply, action is method in our class at controller. Look at our code: 1 view->assign('name', 'Wiwit'); 9 $this->view->assign('title', 'Hello'); 10 } 11 12} 13?> view plain | print Above, we have action name "index". Naming in Zend Framework, add with Action. Thus, we have action "indexAction". When we call this controller (such as user), but we don't specify action, it will call indexAction as default. Now, add a method in there, example "nameAction" such as: 1public function nameAction() 2{ 3 $this->view->assign('name', 'Wiwit'); 4 $this->view->assign('title', 'User Name'); 5} view plain | print Below, complete our userController: 1 view->assign('name', 'Wiwit'); 9 $this->view->assign('title', 'Hello'); 10 } 11 12 public function nameAction() 13 { 14 $this->view->assign('name', 'Wiwit'); 15 $this->view->assign('title', 'User Name'); 16 } 17 18} 19?> view plain | print Next, create a file named "name.phtml" within views/scrips/user. Enter following code: <? echo $this->escape($this->title); ?> 1 2 3 4 5 6 7 8 9

escape($this->title);?>, escape($this->name);?>

10 11 view plain | print Now, point your browser to http://localhost/test/zend/helloworld/web_root/user/name Zend Framework Action: GET Parameters Tag: framework, zend, zend framework, controller, MVC, actions, parameter, GET Category: PHP Framework post: 11 Apr 2008 read: 1,070 Zend Framework Action Step By Step Tutorial - Part 4: Now, we talk last part or URL in Zend Framework, parameters. Ok, what the rule in Zend Framework? Look this URL sample: 1http://hostname/user/name/username/wiwit/gender/man view plain | print We can interpret like this: 1. 2. 3. 4. Controller = user Action = name username = wiwit gender = man What is your conclusion? Yeah, we have general formula like this: 1http://hostname/controller/action/var1/value1/var2/value2/... view plain | print How to catch the parameters? Ok, we can learn from sample. Open UserController.php within application/controller. Update became like this: 1 view->assign('name', 'Wiwit'); 9 $this->view->assign('title', 'Hello'); 10 } 11 12 public function nameAction() 13 { 14 15 $request = $this->getRequest(); 16 $this->view->assign('name', $request->getParam('username')); 17 $this->view->assign('gender', $request->getParam('gender')); 18 19 $this->view->assign('title', 'User Name'); 20 } 21} 22?> view plain | print Give attention at line 15-17. It is clear, isn't it? Last, update your view: "name.phtml": <? echo $this->escape($this->title); ?> 1 2 3 4 5 6 7 8 9

escape($this->title);?>, escape($this->name);?>

10

Gender: escape($this->gender);?>

11 12 view plain | print Zend Framework Action: Including Header and Footer Tag: framework, zend, zend framework, view, MVC, action, header, footer Category: PHP Framework post: 11 Apr 2008 read: 1,194 Zend Framework Action Step By Step Tutorial - Part 5: Last posting from this series, we talk about how to include header and footer for template. I know, may be this is not any relation with Zend Framework Action, but, may be this knowledge become useful for you to build web site now. Create a file named "header.phtml" within views/scripts/user. Enter following code: 2 3 4 5 <? echo $this->escape($this->title); ?> 6 7 8 view plain | print Now, create a file named "footer.phtml" within views/scripts/user. Enter following code: 1 4 5 view plain | print Last, update "name.phtml" within views/scripts/user with this: 1 2

escape($this->title);?>, escape($this->name);?>

3

Gender: escape($this->gender);?>

4 view plain | print Now, try to point your browser to http://localhost/test/zend/helloworld/web_root/user/ name/username/wiwit/gender/man =======================================================================================    ==================================================================================    Zend Framework Database: Intro Tag: framework, zend, zend framework, database Category: PHP Framework post: 12 Apr 2008 read: 2,563 Zend Framework Database Step By Step Tutorial - Part 1: Zend Framework provides classes for supporting database. Name of that class is Zend_Db. More benefit using this class and its related classes is features that offer simple SQL database interface. If we talk about database in Zend Framework, we may talk what is Zend_Db_Adapter. This is basic class you use to connect your PHP application to and RDBMS. As we know, there is a different adapter class for each brand of RDBMS. The Zend_Db_Adapters is a bridge from the vendor-specific PHP extensions to a common interface. With this class, you can write PHP application for multi brands of RDBMS (with little effort). May be you remember there is extension at PHP that have function like Zend_Db_Adapter, PHP Data Object (PDO). PDO provide interface for multi database at PHP 5. At PHP 4, we know a library, ADOdb. About PHP ADOdb, you can read this post. in this practice, we will use PDO for mysql (pdo_mysql). First try to check availabelity pdo_mysql in your system. Check in your php.ini. uncomment extension=php_pdo.dll,extension=php_pdo_mysql.dll, then restart your apache. Ok, just remmembering, this practise base on our previous practice at here and here. So, make sure that you have followed that practice. For this practice, create a database named "zend". Then create a table named "user". You can use this query: 1CREATE TABLE `user` ( 2 `id` int(11) NOT NULL auto_increment, 3 `first_name` varchar(50) NOT NULL, 4 `last_name` varchar(50) NOT NULL, 5 `user_name` varchar(50) NOT NULL, 6 `password` varchar(255) NOT NULL, 7 PRIMARY KEY (`id`), 8 UNIQUE KEY `user_name` (`user_name`) 9); view plain | print   Zend Framework Database: Creating Input Form Tag: framework, zend, zend framework, database, form Category: PHP Framework post: 12 Apr 2008 read: 2,432 Zend Framework Database Step By Step Tutorial - Part 2: First step, we will create a form for inputing data. We create this form at User Controller. We just add method named "registerAction" as action for this controller. Open "UserController.php" within application/controllers. Add following method: 1 public function registerAction() 2 { 3 $request = $this->getRequest(); 4 5 $this->view->assign('action',"process"); 6 $this->view->assign('title','Member Registration'); 7 $this->view->assign('label_fname','First Name'); 8 $this->view->assign('label_lname','Last Name'); 9 $this->view->assign('label_uname','User Name'); 10$this->view->assign('label_pass','Password'); 11$this->view->assign('label_submit','Register'); 12$this->view->assign('description','Please enter this form completely:'); 13 } view plain | print As we know, if we create action name "registerAction", we must create a view named "register.phtml". Ok, create a file named "register.phtml" within application/views/scripts/user. Enter following code: 1 2

escape($this->title);?>

3
4 escape($this->description);?> 5
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
escape($this->label_fname)?>
escape($this->label_lname)?>
escape($this->label_uname)?>
escape($this->label_pass)?>
25 26 27 view plain | print Point your browser to http://localhost/test/zend/helloworld/web_root/user/register   Zend Framework Database: Inputing Data to Database Tag: framework, zend, zend framework, database, input Category: PHP Framework post: 12 Apr 2008 read: 1,934 Zend Framework Database Step By Step Tutorial - Part 3: After create form, now, we create action to input data to database. For this job, open "UserController.php" within application/controllers. Add a method named "processAction", with following code: 1 public function processAction() 2 { 3 4 $params = array('host' =>'localhost', 5 'username' =>'root', 6 'password' =>'admin', 7 'dbname' =>'zend' 8 ); 9 DB = new Zend_Db_Adapter_Pdo_Mysql($params); 10 11 $request = $this->getRequest(); 12 13sql = "INSERT INTO `user` 14 (`first_name` , `last_name` ,`user_name` ,`password`) 15 VALUES ('".$request->getParam('first_name')."', '".$request->getParam('last_name')."', '".$request16 >getParam('user_name')."', MD5('".$request->getParam('password')."'))"; 17DB->query($sql); 18 19 $this->view->assign('title','Registration Process'); 20this->view->assign('description','Registration succes'); 21 22} view plain | print It is simple to understand it, isn't it? Next, create view for this processAction. create a file named "process.phtml" within application/views/scripts/user . Enter following code: 1 2

escape($this->title);?>

3

escape($this->description);?>

4 Member List 5 view plain | print Now, point your browser to http://localhost:8050/test/zend/helloworld/web_root/user/register. Then, enter a user information such as: Click registration button. You will get like this:   Zend Framework Database: Inserting Expressions to a table Tag: framework, zend, zend framework, database, input, insert, expression, table Category: PHP Framework post: 12 Apr 2008 read: 1,286 Zend Framework Database Step By Step Tutorial - Part 4: At previous post, we use ordinary query for input data. Zend framework have simple way for insert query. It is good alternative because simple and more neat. You can use like this: 1$data = array('first_name' => $request->getParam('first_name'), 2 'last_name' => $request->getParam('last_name'), 3 'user_name' => $request->getParam('user_name'), 4 'password' => md5($request->getParam('password')) 5 ); 6 $DB->insert('user', $data); view plain | print old query like this: 1$sql = "INSERT INTO `user` 2 (`first_name` , `last_name` ,`user_name` ,`password`) 3 VALUES ('".$request->getParam('first_name')."', '".$request->getParam('last_name')."', '".$request4 >getParam('user_name')."', MD5('".$request->getParam('password')."'))"; 5$DB->query($sql); view plain | print Ok, replace our processAction become like this: 1 public function processAction() 2 { 3 4 $params = array('host' =>'localhost', 5 'username' =>'root', 6 'password' =>'admin', 7 'dbname' =>'zend' 8 ); 9 $DB = new Zend_Db_Adapter_Pdo_Mysql($params); 10 11 $request = $this->getRequest(); 12$data = array('first_name' => $request->getParam('first_name'), 13 'last_name' => $request->getParam('last_name'), 14 'user_name' => $request->getParam('user_name'), 15 'password' => md5($request->getParam('password')) 16 ); 17 $DB->insert('user', $data); 18 19 $this->view->assign('title','Registration Process'); 20$this->view->assign('description','Registration succes'); 21 22 } view plain | print   Zend Framework Database: Creating List of Data Tag: framework, zend, zend framework, database, list data, grid data Category: PHP Framework post: 12 Apr 2008 read: 1,404 Zend Framework Database Step By Step Tutorial - Part 5: Now, we create table of data that we have inputed. For this job, we create a methode named "listAction" within controller. Then, create view for that list. Open "UserController.php" within application/controllers. Add following method: 1 public function listAction() 2 { 3 $params = array('host' =>'localhost', 4 'username' =>'root', 5 'password' =>'admin', 6 'dbname' =>'zend' 7 ); 8 $DB = new Zend_Db_Adapter_Pdo_Mysql($params); 9 10$DB->setFetchMode(Zend_Db::FETCH_OBJ); 11 12$sql = "SELECT * FROM `user` ORDER BY user_name ASC"; 13$result = $DB->fetchAssoc($sql); 14 15 $this->view->assign('title','Member List'); 16$this->view->assign('description','Below, our members:'); 17$this->view->assign('datas',$result); 18 19 } view plain | print Then, create named "list.phtml" within application/views/scripts/user. Enter following code: 1 2

escape($this->title);?>

3

escape($this->description);?>

4 Register 5 6 7 8 9 10 11 12 13 datas; 14 for($i = 1; $i<= count($datas);$i++){ ?> 15 16 17 18 19 20 25 26 27
IDUser NameFirst NameLast NameAction
21 Edit 22 | 23 Delete 24
28 29 view plain | print Point your browser to http://localhost/test/zend/helloworld/ web_root/user/list   Zend Framework Database: Creating Editing Form Tag: framework, zend, zend framework, database, form, edit Category: PHP Framework post: 12 Apr 2008 read: 1,208 Zend Framework Database Step By Step Tutorial - Part 6: To create editing form, add a method in controller. In this practice, we add a action named "editAction". This action will show individual data that filtered by GET http parameter. Open "UserController.php" within application/controllers. Add following method: 1 public function editAction() 2 { 3 $params = array('host' =>'localhost', 4 'username' =>'root', 5 'password' =>'admin', 6 'dbname' =>'zend' 7 ); 8 $DB = new Zend_Db_Adapter_Pdo_Mysql($params); 9 10 $request = $this->getRequest(); 11$id = $request->getParam("id"); 12 13$sql = "SELECT * FROM `user` WHERE id='".$id."'"; 14$result = $DB->fetchRow($sql); 15 16$this->view->assign('data',$result); 17$this->view->assign('action', $request->getBaseURL()."/user/processedit"); 18 $this->view->assign('title','Member Editing'); 19$this->view->assign('label_fname','First Name'); 20$this->view->assign('label_lname','Last Name'); 21$this->view->assign('label_uname','User Name'); 22$this->view->assign('label_pass','Password'); 23$this->view->assign('label_submit','Edit'); 24$this->view->assign('description','Please update this form completely:'); 25 } view plain | print Then, create a file named "edit.phtml" within application/views/scripts/user. Enter following code: 1 2

escape($this->title);?>

3
4 escape($this->description);?> 5
6
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
escape($this->label_fname)?>
escape($this->label_lname)?>
escape($this->label_uname)?>
22 23
24 view plain | print Then open your table data again at http://localhost/test/zend/helloworld/web_root/user/list. Klik a edit link at one of rows. The page will jump to edit form such as:   Zend Framework Database: Updating Data Tag: framework, zend, zend framework, database, update, data Category: PHP Framework post: 12 Apr 2008 read: 1,001 Zend Framework Database Step By Step Tutorial - Part 7: In this post, we will see action for updating data. Open your "UserController.php" within application/controller. Enter following new action: 1 public function processeditAction() 2 { 3 $params = array('host' =>'localhost', 4 'username' =>'root', 5 'password' =>'admin', 6 'dbname' =>'zend' 7 ); 8 $DB = new Zend_Db_Adapter_Pdo_Mysql($params); 9 $request = $this->getRequest(); 10 11$sql = "UPDATE `user` SET `first_name` = '".$request->getParam('first_name')."', 12 `last_name` = '".$request->getParam('last_name')."', 13 `user_name` = '".$request->getParam('user_name')."' 14 WHERE id = '".$request->getParam('id')."'"; 15$DB->query($sql); 16 17 $this->view->assign('title','Editing Process'); 18$this->view->assign('description','Editing succes'); 19 20 } view plain | print Then, create a file named "processedit.phtml" within views/scripts/user. Enter following code: 1 2

escape($this->title);?>

3

escape($this->description);?>

4 Member List 5 view plain | print Ok, try to update data from edit form.   Zend Framework Database: Updating Data Use Update Query From Zend Framework Tag: framework, zend, zend framework, database, update, data, query Category: PHP Framework post: 12 Apr 2008 read: 1,245 Zend Framework Database Step By Step Tutorial - Part 8: Like insert query, Zend Framework have another option to create update query. It is more simple and neat. You can use like this: 1$data = array('first_name' => $request->getParam('first_name'), 2 'last_name' => $request->getParam('last_name'), 3 'user_name' => $request->getParam('user_name'), 4 'password' => md5($request->getParam('password')) 5 ); 6$DB->update('user', $data,'id = 1'); view plain | print Following complete code for processeditAction method: 1 public function processeditAction() 2 { 3 $params = array('host' =>'localhost', 4 'username' =>'root', 5 'password' =>'admin', 6 'dbname' =>'zend' 7 ); 8 $DB = new Zend_Db_Adapter_Pdo_Mysql($params); 9 $request = $this->getRequest(); 10 11$data = array('first_name' => $request->getParam('first_name'), 12 'last_name' => $request->getParam('last_name'), 13 'user_name' => $request->getParam('user_name'), 14 'password' => md5($request->getParam('password')) 15 ); 16 $DB->update('user', $data,'id = '.$request->getParam('id')); 17 18 $this->view->assign('title','Editing Process'); 19$this->view->assign('description','Editing succes'); 20 21 } view plain | print Zend Framework Database: Deleting Data Tag: framework, zend, zend framework, database, delete, data Category: PHP Framework post: 12 Apr 2008 read: 853 Zend Framework Database Step By Step Tutorial - Part 9: Last action from this series is delete data. For this job, create new method in controller. Open your "UserController.php" within application/controllers. Add new method for delete action (i give name "delAction"): 1 public function delAction() 2 { 3 $params = array('host' =>'localhost', 4 'username' =>'root', 5 'password' =>'admin', 6 'dbname' =>'zend' 7 ); 8 $DB = new Zend_Db_Adapter_Pdo_Mysql($params); 9 $request = $this->getRequest(); 10 11$sql = "DELETE FROM `user` WHERE id='".$request->getParam('id')."'"; 12$DB->query($sql); 13 14 $this->view->assign('title','Delete Data'); 15$this->view->assign('description','Deleting succes'); 16 $this->view->assign('list',$request->getBaseURL()."/user/list"); view plain | print Next, create a view for this action. Create a file named "del.phtml" within application/views/scripts/user. Enter following code: 1 2

escape($this->title);?>

3

escape($this->description);?>

4 Member List 5 view plain | print Now, try to test from table data. Click link del at one of rows.   Zend Framework Database: Delete Query Style Tag: framework, zend, zend framework, database, delete, data, query Category: PHP Framework post: 12 Apr 2008 read: 1,112 Zend Framework Database Step By Step Tutorial - Part 10: Like insert and update, Zend framework have other option for delete query. You can use this style, and i am sure you will be like. 1$DB->delete('user', 'id = 3'); view plain | print Ok, try to update your delAction become like this: 1 public function delAction() 2 { 3 $params = array('host' =>'localhost', 4 'username' =>'root', 5 'password' =>'admin', 6 'dbname' =>'zend' 7 ); 8 $DB = new Zend_Db_Adapter_Pdo_Mysql($params); 9 $request = $this->getRequest(); 10 11 $DB->delete('user', 'id = '.$request->getParam('id')); 12 13 $this->view->assign('title','Delete Data'); 14$this->view->assign('description','Deleting succes'); 15 $this->view->assign('list',$request->getBaseURL()."/user/list"); 16 17 } view plain | print Oke, now, you are ready to read more about Zend Framework query style at Zend Framework manual guide.   Zend Framework Database: Summarizing Action Controller Tag: framework, zend, zend framework, controller, MVC, action Category: PHP Framework post: 12 Apr 2008 read: 1,434 Zend Framework Database Step By Step Tutorial - Part 11: This is last post from Zend Framework Database Tutorial series. In this post, you can look complete action that we had talked. 1 view->assign('name', 'Wiwit'); 10 $this->view->assign('title', 'Hello'); 11 } 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 public function nameAction() { $request = $this->getRequest(); $this->view->assign('name', $request->getParam('username')); $this->view->assign('gender', $request->getParam('gender')); $this->view->assign('title', 'User Name'); } public function registerAction() { $request = $this->getRequest(); $this->view->assign('action',"process"); $this->view->assign('title','Member Registration'); $this->view->assign('label_fname','First Name'); $this->view->assign('label_lname','Last Name'); $this->view->assign('label_uname','User Name'); $this->view->assign('label_pass','Password'); $this->view->assign('label_submit','Register'); $this->view->assign('description','Please enter this form completely:'); } public function editAction() { $params = array('host' =>'localhost', 'username' =>'root', 'password' =>'admin', 'dbname' =>'zend' ); $DB = new Zend_Db_Adapter_Pdo_Mysql($params); $request = $this->getRequest(); $id = $request->getParam("id"); $sql = "SELECT * FROM `user` WHERE id='".$id."'"; $result = $DB->fetchRow($sql); $this->view->assign('data',$result); $this->view->assign('action', $request->getBaseURL()."/user/processedit"); $this->view->assign('title','Member Editing'); $this->view->assign('label_fname','First Name'); $this->view->assign('label_lname','Last Name'); $this->view->assign('label_uname','User Name'); $this->view->assign('label_pass','Password'); $this->view->assign('label_submit','Edit'); $this->view->assign('description','Please update this form completely:'); } 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 public function processAction() { $params = array('host' =>'localhost', 'username' =>'root', 'password' =>'admin', 'dbname' =>'zend' ); $DB = new Zend_Db_Adapter_Pdo_Mysql($params); $request = $this->getRequest(); $data = array('first_name' => $request->getParam('first_name'), 'last_name' => $request->getParam('last_name'), 'user_name' => $request->getParam('user_name'), 'password' => md5($request->getParam('password')) ); $DB->insert('user', $data); $this->view->assign('title','Registration Process'); $this->view->assign('description','Registration succes'); } public function listAction() { $params = array('host' =>'localhost', 'username' =>'root', 'password' =>'admin', 'dbname' =>'zend' ); $DB = new Zend_Db_Adapter_Pdo_Mysql($params); $DB->setFetchMode(Zend_Db::FETCH_OBJ); $sql = "SELECT * FROM `user` ORDER BY user_name ASC"; $result = $DB->fetchAssoc($sql); $this->view->assign('title','Member List'); $this->view->assign('description','Below, our members:'); $this->view->assign('datas',$result); } public function processeditAction() { $params = array('host' =>'localhost', 'username' =>'root', 'password' =>'admin', 'dbname' =>'zend' 112 ); 113 $DB = new Zend_Db_Adapter_Pdo_Mysql($params); 114 $request = $this->getRequest(); 115 116 $data = array('first_name' => $request->getParam('first_name'), 117 'last_name' => $request->getParam('last_name'), 118 'user_name' => $request->getParam('user_name'), 119 'password' => md5($request->getParam('password')) 120 ); 121 $DB->update('user', $data,'id = '.$request->getParam('id')); 122 123 $this->view->assign('title','Editing Process'); 124 $this->view->assign('description','Editing succes'); 125 126 } 127 128 public function delAction() 129 { 130 $params = array('host' =>'localhost', 131 'username' =>'root', 132 'password' =>'admin', 133 'dbname' =>'zend' 134 ); 135 $DB = new Zend_Db_Adapter_Pdo_Mysql($params); 136 $request = $this->getRequest(); 137 138 $DB->delete('user', 'id = '.$request->getParam('id')); 139 140 $this->view->assign('title','Delete Data'); 141 $this->view->assign('description','Deleting succes'); 142 $this->view->assign('list',$request->getBaseURL()."/user/list"); 143 144 } 145 146} 147?> view plain | print   ==============================================================================================    ==============================================================================================  Zend Framework Registry: Setting and Reading Values Tag: framework, zend, zend framework, registry, zend_registry Category: PHP Framework post: 16 Apr 2008 read: 1,041 Zend Framework Registry Step By Step Tutorial - Part 1: What is registry? It is like global storage. We just register values then we can use throughout application. For example, we can store name of application, thus every time we need to display name of application, we just call from registry. To use registry, we call registry.php from Zend Framework: 1require_once 'Zend/Registry.php'; view plain | print Ok, we practice using registry. We still use our previous practice. Please read that tutorial before. Then, follow this steps: 1. Open "index.php" within "web_root". Enter line 11 - 13: 1 view plain | print 21. We set a parameter named "title". We put a value for this parameter. 22. Next, we try to call/read this registry. Open your UserController.php within application/controllers. 23. Update indexAction like this: 1public function indexAction() 2{ 3 $title = Zend_Registry::get('title'); 4 $this->view->assign('name', 'Wiwit'); 5 $this->view->assign('title', $title); 6} view plain | print Test, by pointing your browser to : http://localhost/test/zend/helloworld/web_root/user/. You may remember a function at PHP that have function like this, define(). Yup, it is like register global parameter.   Zend Framework Registry: Storing Array Values Tag: framework, zend, zend framework, registry, zend_registry, array Category: PHP Framework post: 16 Apr 2008 read: 722 Zend Framework Registry Step By Step Tutorial - 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. Ok, follow this steps to understand. Open again your "index.php" within web_root. Update become like this: 1 view plain | print We register a array parameter named "credits" at line 15-16. How to read it? Ok, we test by update again "UserController.php". Update at indexAction become: 1 2 3 4 5 6 public function indexAction() { $registry = Zend_Registry::getInstance(); $title = $registry['title']; $credits = $registry['credits']; 7 $strCredit = implode(", ",$credits); 8 9 $this->view->assign('name', 'Wiwit'); 10 $this->view->assign('title', $title); 11 $this->view->assign('credits', $strCredit); 12 } view plain | print This is another way to read registry. It can we use to read several values from registry. Ok, before test it. Please update view at index.phtml within application/views/scripts/user. Update with following code: 2 3 4 5 <? echo $this->escape($this->title); ?> 6 7 8 9

escape($this->title);?>, escape($this->name);?>

10 credits: escape($this->credits);?> 11 12 view plain | print Test by point your browser to http://localhost/test/zend/helloworld/web_root/user/.   Zend Framework Registry: Working with Objects Tag: framework, zend, zend framework, registry, zend_registry, objects Category: PHP Framework post: 16 Apr 2008 read: 913 Zend Framework Registry Step By Step Tutorial - Part 3: We can store object to Zend_Registry. How to apply it? What for? Remember, our practice about Zend Framework database always write database config and connection several times. Each time connect to database, we write connection. We can save (energy), write one and store to registry. To register database connection, we can use like this: 1$params = array('host' =>'localhost', 2 'username' =>'root', 3 'password' =>'admin', 4 'dbname' =>'zend' 5 ); 6$DB = new Zend_Db_Adapter_Pdo_Mysql($params); 7 8$DB->setFetchMode(Zend_Db::FETCH_OBJ); 9Zend_Registry::set('DB',$DB); view plain | print Then, read: 1$registry = Zend_Registry::getInstance(); 2$DB = $registry['DB']; view plain | print Ok, for practice, open index.php within web_root. Update become like this: 1 'localhost', 20 'username' =>'root', 21 'password' =>'admin', 22 'dbname' =>'zend' 23 ); 24$DB = new Zend_Db_Adapter_Pdo_Mysql($params); 25 26$DB->setFetchMode(Zend_Db::FETCH_OBJ); 27Zend_Registry::set('DB',$DB); 28 29 30 31Zend_Controller_Front::run('../application/controllers'); 32 33?> view plain | print Next, update "UserController.php" within application/controllers become like this: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 view->assign('name', 'Wiwit'); $this->view->assign('title', $title); $this->view->assign('credits', $strCredit); } public function nameAction() { $request = $this->getRequest(); $this->view->assign('name', $request->getParam('username')); $this->view->assign('gender', $request->getParam('gender')); $this->view->assign('title', 'User Name'); } public function registerAction() { $request = $this->getRequest(); $this->view->assign('action',"process"); $this->view->assign('title','Member Registration'); $this->view->assign('label_fname','First Name'); $this->view->assign('label_lname','Last Name'); $this->view->assign('label_uname','User Name'); $this->view->assign('label_pass','Password'); $this->view->assign('label_submit','Register'); $this->view->assign('description','Please enter this form completely:'); } public function editAction() { $registry = Zend_Registry::getInstance(); $DB = $registry['DB']; 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 $request = $this->getRequest(); $id = $request->getParam("id"); $sql = "SELECT * FROM `user` WHERE id='".$id."'"; $result = $DB->fetchRow($sql); $this->view->assign('data',$result); $this->view->assign('action', $request->getBaseURL()."/user/processedit"); $this->view->assign('title','Member Editing'); $this->view->assign('label_fname','First Name'); $this->view->assign('label_lname','Last Name'); $this->view->assign('label_uname','User Name'); $this->view->assign('label_pass','Password'); $this->view->assign('label_submit','Edit'); $this->view->assign('description','Please update this form completely:'); } public function processAction() { $registry = Zend_Registry::getInstance(); $DB = $registry['DB']; $request = $this->getRequest(); $data = array('first_name' => $request->getParam('first_name'), 'last_name' => $request->getParam('last_name'), 'user_name' => $request->getParam('user_name'), 'password' => md5($request->getParam('password')) ); $DB->insert('user', $data); $this->view->assign('title','Registration Process'); $this->view->assign('description','Registration succes'); } public function listAction() { $registry = Zend_Registry::getInstance(); $DB = $registry['DB']; $sql = "SELECT * FROM `user` ORDER BY user_name ASC"; $result = $DB->fetchAssoc($sql); $this->view->assign('title','Member List'); $this->view->assign('description','Below, our members:'); $this->view->assign('datas',$result); } 99 100 public function processeditAction() 101 { 102 103 $registry = Zend_Registry::getInstance(); 104 $DB = $registry['DB']; 105 106 $request = $this->getRequest(); 107 108 $data = array('first_name' => $request->getParam('first_name'), 109 'last_name' => $request->getParam('last_name'), 110 'user_name' => $request->getParam('user_name'), 111 'password' => md5($request->getParam('password')) 112 ); 113 $DB->update('user', $data,'id = '.$request->getParam('id')); 114 115 $this->view->assign('title','Editing Process'); 116 $this->view->assign('description','Editing succes'); 117 118 } 119 120 public function delAction() 121 { 122 $registry = Zend_Registry::getInstance(); 123 $DB = $registry['DB']; 124 125 $request = $this->getRequest(); 126 127 $DB->delete('user', 'id = '.$request->getParam('id')); 128 129 $this->view->assign('title','Delete Data'); 130 $this->view->assign('description','Deleting succes'); 131 $this->view->assign('list',$request->getBaseURL()."/user/list"); 132 133 } 134 135} 136?> view plain | print Can you see it? It is simple, isn't it?   ==========================================================================================    =========================================================================================    Zend Framework Config: Using Array Configuration Tag: framework, zend, zend framework, configuration Category: PHP Framework post: 21 Apr 2008 read: 773 Zend Framework Configuration Step By Step Tutorial - Part 1: There are important information where we build a database application such as host of database, database name, user, database, name of application, and so on. These informations are retrieved extensively. It will concise if we put them on a configuration part. Zend Framework have zend_config, is designed to simplify access to and use of configuration data within applications. In this post, we see how to implement zend_config use array. For case, see our code from previous tutorial: 1 'localhost', 20 'username' =>'root', 21 'password' =>'admin', 22 'dbname' =>'zend' 23 ); 24$DB = new Zend_Db_Adapter_Pdo_Mysql($params); 25 26$DB->setFetchMode(Zend_Db::FETCH_OBJ); 27Zend_Registry::set('DB',$DB); 28 29 30 31Zend_Controller_Front::run('../application/controllers'); 32 33?> view plain | print That is index.php. Location code above at web_root. Line 14 and 19-23 often use along application. We can put into an array configuration like this: 1 'localhost', 18 'appName'=>'My First Zend', 19 'database'=>array( 20 'dbhost'=>'localhost', 21 'dbname'=>'zend', 22 'dbuser'=>'root', 23 'dbpass'=>'admin' 24 ) 25 ); 26 27$config = new Zend_Config($arrConfig); 28 29$title = $config->appName; 30$params = array('host' =>$config->database->dbhost, 31 'username' =>$config->database->dbuser, 32 'password' =>$config->database->dbpass, 33 'dbname' =>$config->database->dbname 34 ); 35 36 37Zend_Registry::set('title',$title); 38 39$arrName = array('Ilmia Fatin','Aqila Farzana', 'Imanda Fahrizal'); 40Zend_Registry::set('credits',$arrName); 41 42$DB = new Zend_Db_Adapter_Pdo_Mysql($params); 43 44$DB->setFetchMode(Zend_Db::FETCH_OBJ); 45Zend_Registry::set('DB',$DB); 46 47 48 49Zend_Controller_Front::run('../application/controllers'); 50 51?> view plain | print First, load Zend/Config.php (see line 13). Then create array that contains configuration (see line 16-25). Next, Create the object-oriented wrapper upon the configuration data (see line 27). Last, example access configuration data see line 29-34.   Zend Framework Config: Creating File Configuration Tag: framework, zend, zend framework, configuration, file Category: PHP Database post: 21 Apr 2008 read: 636 Zend Framework Configuration Step By Step Tutorial - Part 2: For easy maintenance, we put configuration data into separated file. For example, config.php. Let's do it. Create a file named "config.php" within application. Enter following code: 1 'localhost', 4 'appName'=>'My First Zend', 5 'database'=>array( 6 'host'=>'localhost', 7 'dbname'=>'zend', 8 'username'=>'root', 9 'password'=>'admin' 10 ) 11 ); 12?> view plain | print To call this file, we can use like this: 1$config = new Zend_Config(require '../application/config.php'); view plain | print This is complete code: 1 2 3 4 5 6 appName; 18$params = $config->database->toArray(); 19 20Zend_Registry::set('title',$title); 21 22$arrName = array('Ilmia Fatin','Aqila Farzana', 'Imanda Fahrizal'); 23Zend_Registry::set('credits',$arrName); 24 25$DB = new Zend_Db_Adapter_Pdo_Mysql($params); 26 27$DB->setFetchMode(Zend_Db::FETCH_OBJ); 28Zend_Registry::set('DB',$DB); 29 30 31 32Zend_Controller_Front::run('../application/controllers'); 33 34?> view plain | print     Zend Framework Config: Using INI File Configuration Tag: framework, zend, zend framework, configuration, file Category: PHP Framework post: 21 Apr 2008 read: 955 Zend Framework Configuration Step By Step Tutorial - Part 3: We have learn about how to use zend_config at previous post. Now, we try to implement using ini file. Create a file named "config.ini" within application. Enter following sample config: 1; Production site configuration data 2[app] 3webhost = www.example.com 4title = My Zend Framework 5database.host = localhost 6database.username = root 7database.password = admin 8database.dbname = zend view plain | print To read that config, we use like this: 1require_once 'Zend/Config/Ini.php'; 2 3$config = new Zend_Config_Ini('../application/config.ini','app'); 4 5$title = $config->appName; 6$params = $config->database->toArray(); view plain | print Following complete code: 1 appName; 19$params = $config->database->toArray(); 20 21Zend_Registry::set('title',$title); 22 23$arrName = array('Ilmia Fatin','Aqila Farzana', 'Imanda Fahrizal'); 24Zend_Registry::set('credits',$arrName); 25 26$DB = new Zend_Db_Adapter_Pdo_Mysql($params); 27 28$DB->setFetchMode(Zend_Db::FETCH_OBJ); 29Zend_Registry::set('DB',$DB); 30 31 32 33Zend_Controller_Front::run('../application/controllers'); 34 35?> view plain | print     Zend Framework Config: Using XML File Configuration Tag: framework, zend, zend framework, configuration, file, xml Category: PHP Framework post: 21 Apr 2008 read: 743 Zend Framework Configuration Step By Step Tutorial - Part 4: This is another alternative to create a configuration file. We can store configuration data in a simple XML format. To read it, we use Zend_Config_Xml(). First, create a XML file named "config.xml" within application. Enter following code: 1 2 3 4 localhost 5 6 localhost 7 root 8 admin 9 zend 10 11 12 view plain | print To access it, we can use: 1require_once 'Zend/Config/Xml.php'; 2 3$config = new Zend_Config_Xml('../application/config.xml','app'); view plain | print     ============================================================================================    ============================================================================================    Zend Framework Login: Preparing Database Tag: framework, zend, zend framework, authentication, Zend_Auth Category: PHP Framework post: 22 Apr 2008 read: 2,788 Zend Framework Login System Step by Step Tutorial - Part 1: Login page is a important part for database application. In this tutorial series, we will talk how to create simple login system at Zend Framework. There are several possible scenario, but I will choose login with database. It means, we need database for store member data. We still use same database from previous practice. The database name is "zend". Now, create table for store members information. The table name is "users". You can use this query: 1CREATE TABLE `users` ( 2 `id` int(11) NOT NULL auto_increment, 3 `username` varchar(50) NOT NULL, 4 `password` varchar(255) NOT NULL, 5 `real_name` varchar(50) NOT NULL, 6 PRIMARY KEY (`id`), 7 UNIQUE KEY `username` (`username`) 8); view plain | print Now, try to insert sample member data for example: 1INSERT INTO `users` 2(`id`, `username`, `password`, `real_name`) 3VALUES 4(1, 'admin', '21232f297a57a5a743894a0e4a801fc3', 'Administrator'); view plain | print Above, we insert a member with user name "admin". Actually, his password is admin. But, for security reason, we use MD5(), so that why, password is stored at database is 21232f297a57a5a743894a0e4a801fc3. 1INSERT INTO `zend`.`users` (`id` ,`username` ,`password` ,`real_name`) 2VALUES (NULL , 'admin', MD5( 'admin' ) , 'Administrator'); view plain | print This is screenshot if we use phpMyAdmin. Zend Framework Login: Creating Form Login Tag: framework, zend, zend framework, authentication, Zend_Auth, Form, Login Category: PHP Framework post: 22 Apr 2008 read: 2,982 Zend Framework Login System Step by Step Tutorial - Part 2: To build login system, we need login form. In this post, we will learn how to build simple login form. First, create a file named "loginform.phtml" within application/views/scripts/user. Enter following code: 1 2

escape($this->title);?>

3
4 5 6 7 8 9 10 11 12 13
escape($this->username);?>
escape($this->password);?>
14 15
16 view plain | print Next, create loginformAction at controller. Open your UserController.php within application/controllers. Add following loginFormAction() method: 1public function loginFormAction() 2{ 3 $request = $this->getRequest(); 4this->view->assign('action', $request->getBaseURL()."/user/auth"); 5 $this->view->assign('title', 'Login Form'); 6 $this->view->assign('username', 'User Name'); 7 $this->view->assign('password', 'Password'); 8 9} view plain | print This form send two parameter: username and password. They are sent to user/auth page. Now, point your browser to http://localhost/test/zend/helloworld/web_root/user/loginform. You may get like this:   Zend Framework Login: Creating Authentication Tag: framework, zend, zend framework, authentication, Zend_Auth Category: PHP Framework post: 22 Apr 2008 read: 4,369 Zend Framework Login System Step by Step Tutorial - Part 3: After create form login, we need authentication. Data will be checked with existing data at database. Just remembering, login form send two parameter: username and password. They are sent to user/auth page. So, we must build authAction() at UserController. authAction() for data validation. The data that sent by login form will be checked at authAction(). Open your "UserController.php". Firs, load Zend/Auth.php and Zend/Auth/Adapter/DbTable.php. Please place at first line before any method(): 1 view plain | print Add authAction() like this: 1 public function authAction(){ 2 $request = $this->getRequest(); 3 $registry = Zend_Registry::getInstance(); 4 $auth = Zend_Auth::getInstance(); 5 6 $DB = $registry['DB']; 7 8 $authAdapter = new Zend_Auth_Adapter_DbTable($DB); 9 $authAdapter->setTableName('users') 10 ->setIdentityColumn('username') 11 ->setCredentialColumn('password'); 12 13// Set the input credential values 14$uname = $request->getParam('username'); 15$paswd = $request->getParam('password'); 16 $authAdapter->setIdentity($uname); 17 $authAdapter->setCredential(md5($paswd)); 18 19 // Perform the authentication query, saving the result 20 $result = $auth->authenticate($authAdapter); 21 22 if($result->isValid()){ 23 $data = $authAdapter->getResultRowObject(null,'password'); 24 $auth->getStorage()->write($data); 25 $this->_redirect('/user/userpage'); 26}else{ 27 $this->_redirect('/user/loginform'); 28} 29} view plain | print First, load database adapter from registry (line 3, 6). If you still don't understand about this code, please read tutorial about Zend Framework Database and Zend Framework Registry. Then, define authentication adapter: 1$authAdapter = new Zend_Auth_Adapter_DbTable($DB); view plain | print Next, we define table name, column that contains username and password. 1$authAdapter->setTableName('users') 2 ->setIdentityColumn('username') 3 ->setCredentialColumn('password'); view plain | print Next, we catch HTTP parameter that contains username and password: 1$uname = $request->getParam('username'); 2$paswd = $request->getParam('password'); 3 $authAdapter->setIdentity($uname); 4 $authAdapter->setCredential(md5($paswd)); view plain | print Next, do authentication: 1 $result = $auth->authenticate($authAdapter); 2 3 if($result->isValid()){ 4 $data = $authAdapter->getResultRowObject(null,'password'); 5 $auth->getStorage()->write($data); 6 $this->_redirect('/user/userpage'); 7 }else{ 8 $this->_redirect('/user/loginform'); 9 } 10} view plain | print If valid user, we will save user data at session using: 1$data = $authAdapter->getResultRowObject(null,'password'); 2$auth->getStorage()->write($data); view plain | print Then, page will be redirect to protected page (named userpage in this tutorial). Next post, we will talk about protected page. It is simple, isn't it? Ok, this is our complete UserController: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 getRequest(); $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'); } public function authAction(){ $request = $this->getRequest(); $registry = Zend_Registry::getInstance(); $auth = Zend_Auth::getInstance(); $DB = $registry['DB']; $authAdapter = new Zend_Auth_Adapter_DbTable($DB); $authAdapter->setTableName('users') ->setIdentityColumn('username') ->setCredentialColumn('password'); 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 // Set the input credential values $uname = $request->getParam('username'); $paswd = $request->getParam('password'); $authAdapter->setIdentity($uname); $authAdapter->setCredential(md5($paswd)); // Perform the authentication query, saving the result $result = $auth->authenticate($authAdapter); if($result->isValid()){ //print_r($result); $data = $authAdapter->getResultRowObject(null,'password'); $auth->getStorage()->write($data); $this->_redirect('/user'); }else{ $this->_redirect('/user/loginform'); } } public function nameAction() { $request = $this->getRequest(); $this->view->assign('name', $request->getParam('username')); $this->view->assign('gender', $request->getParam('gender')); $this->view->assign('title', 'User Name'); } public function registerAction() { $request = $this->getRequest(); $this->view->assign('action',"process"); $this->view->assign('title','Member Registration'); $this->view->assign('label_fname','First Name'); $this->view->assign('label_lname','Last Name'); $this->view->assign('label_uname','User Name'); $this->view->assign('label_pass','Password'); $this->view->assign('label_submit','Register'); $this->view->assign('description','Please enter this form completely:'); } public function editAction() { $registry = Zend_Registry::getInstance(); $DB = $registry['DB']; 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 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 127 128 129 130 131 $request = $this->getRequest(); $id = $request->getParam("id"); $sql = "SELECT * FROM `user` WHERE id='".$id."'"; $result = $DB->fetchRow($sql); $this->view->assign('data',$result); $this->view->assign('action', $request->getBaseURL()."/user/processedit"); $this->view->assign('title','Member Editing'); $this->view->assign('label_fname','First Name'); $this->view->assign('label_lname','Last Name'); $this->view->assign('label_uname','User Name'); $this->view->assign('label_pass','Password'); $this->view->assign('label_submit','Edit'); $this->view->assign('description','Please update this form completely:'); } public function processAction() { $registry = Zend_Registry::getInstance(); $DB = $registry['DB']; $request = $this->getRequest(); $data = array('first_name' => $request->getParam('first_name'), 'last_name' => $request->getParam('last_name'), 'user_name' => $request->getParam('user_name'), 'password' => md5($request->getParam('password')) ); $DB->insert('user', $data); $this->view->assign('title','Registration Process'); $this->view->assign('description','Registration succes'); } public function listAction() { $registry = Zend_Registry::getInstance(); $DB = $registry['DB']; $sql = "SELECT * FROM `user` ORDER BY user_name ASC"; $result = $DB->fetchAssoc($sql); $this->view->assign('title','Member List'); $this->view->assign('description','Below, our members:'); $this->view->assign('datas',$result); 132 } 133 134 public function processeditAction() 135 { 136 137 $registry = Zend_Registry::getInstance(); 138 $DB = $registry['DB']; 139 140 $request = $this->getRequest(); 141 142 $data = array('first_name' => $request->getParam('first_name'), 143 'last_name' => $request->getParam('last_name'), 144 'user_name' => $request->getParam('user_name'), 145 'password' => md5($request->getParam('password')) 146 ); 147 $DB->update('user', $data,'id = '.$request->getParam('id')); 148 149 $this->view->assign('title','Editing Process'); 150 $this->view->assign('description','Editing succes'); 151 152 } 153 154 public function delAction() 155 { 156 $registry = Zend_Registry::getInstance(); 157 $DB = $registry['DB']; 158 159 $request = $this->getRequest(); 160 161 $DB->delete('user', 'id = '.$request->getParam('id')); 162 163 $this->view->assign('title','Delete Data'); 164 $this->view->assign('description','Deleting succes'); 165 $this->view->assign('list',$request->getBaseURL()."/user/list"); 166 167 } 168 169} 170?>   Zend Framework Login: Fatal error Cannot use object of type stdClass as array Tag: framework, zend, zend framework, authentication, Zend_Auth, error, stdClass, array Category: PHP Framework post: 22 Apr 2008 read: 1,620 Zend Framework Login System Step by Step Tutorial - Part 4: We run previous post, 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.php on line 340. If you are same like me, let's fix this problem. Skip this post if you don't fine any error at previous post. Sadly, I still didn't have much time to know about this error when I was writing this post. So, I decide to fix at core of Zend library. I will tell you latter if I find fixing this error more better than this post. Ok, open DbTable.php within library\Zend\Auth\Adapter\. Update line 340: Before: 1if ($resultIdentity['zend_auth_credential_match'] != '1') { view plain | print Become: 1if ($resultIdentity->zend_auth_credential_match != '1') { view plain | print Then, update line 346: 1unset($resultIdentity['zend_auth_credential_match']); view plain | print Become: 1unset($resultIdentity->zend_auth_credential_match); view plain | print I will update this post if there is more better than this way. Do you want to share idea? Zend Framework Login: Protected Page Tag: framework, zend, zend framework, authentication, Zend_Auth Category: PHP Framework post: 22 Apr 2008 read: 1,680 Zend Framework Login System Step by Step Tutorial - Part 5: We have created authentication at previous post. Now, we try to protect a page. Thus, when not member try to enter the page, they will be redirected to login form. First, create view. Create a file named "userpage.phtml" within application/views/scripts/user. Enter following code: 1 2

Hello, escape($this->username);?>

3Logout 4 view plain | print Then, we add method at controller. Open "UserController.php". Add following method: 1 public function userPageAction(){ 2 $auth = Zend_Auth::getInstance(); 3 4 if(!$auth->hasIdentity()){ 5 $this->_redirect('/user/loginform'); 6 } 7 8 $request = $this->getRequest(); 9 $user = $auth->getIdentity(); 10 $real_name = $user->real_name; 11 $username = $user->username; 12 $logoutUrl = $request->getBaseURL().'/user/logout'; 13 14 $this->view->assign('username', $real_name); 15 $this->view->assign('urllogout',$logoutUrl); 16} view plain | print Line 2 - 6 we needed to protect page from unlogin users. Try to login. You can point your browser to http://localhost/test/zend/helloworld/web_root/user/loginform. Then login. Zend Framework Login: Creating Logout Tag: framework, zend, zend framework, authentication, Zend_Auth, logout Category: PHP Framework post: 22 Apr 2008 read: 1,718 Zend Framework Login System Step by Step Tutorial - Part 6: At protected page (in previous post), we had created a link to logout. This post talk about logout page. First, create view for logout. Create a file named "logout.phtml" within application/views/scripts/user. Leave blank (because we don't want to show anything). Then, we create a method for controller named logoutAction(). Open "UserController.php" within application/controllers. Add following method: 1public function logoutAction() 2{ 3 $auth = Zend_Auth::getInstance(); 4$auth->clearIdentity(); 5$this->_redirect('/user'); 6} view plain | print We just use clearIdentity(). Then redirect page to another page (in this sample to user). Zend Framework Login: Creating Switching for Front Page Tag: framework, zend, zend framework, authentication, Zend_Auth Category: PHP Framework post: 22 Apr 2008 read: 2,425 Zend Framework Login System Step by Step Tutorial - Part 7: This is last post from this tutorial series. We will modify front page (user/index). When visitors access this page, they will be redirected to form login. Open "UserController.php" within application/controllers. Update indexAction(): 1 public function indexAction() 2 { 3 $request = $this->getRequest(); 4 $auth = Zend_Auth::getInstance(); 5 if(!$auth->hasIdentity()){ 6 $this->_redirect('/user/loginform'); 7 }else{ 8 $this->_redirect('/user/userpage'); 9 } 10 } view plain | print Ok, below complete code for controller for this session: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 getRequest(); $auth = Zend_Auth::getInstance(); if(!$auth->hasIdentity()){ $this->_redirect('/user/loginform'); }else{ $this->_redirect('/user/userpage'); } } public function userPageAction(){ $auth = Zend_Auth::getInstance(); if(!$auth->hasIdentity()){ $this->_redirect('/user/loginform'); } 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 $request = $this->getRequest(); $user = $auth->getIdentity(); $real_name = $user->real_name; $username = $user->username; $logoutUrl = $request->getBaseURL().'/user/logout'; $this->view->assign('real_name', $real_name); $this->view->assign('urllogout',$logoutUrl); } public function loginFormAction() { $request = $this->getRequest(); $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'); } public function authAction(){ $request = $this->getRequest(); $registry = Zend_Registry::getInstance(); $auth = Zend_Auth::getInstance(); $DB = $registry['DB']; $authAdapter = new Zend_Auth_Adapter_DbTable($DB); $authAdapter->setTableName('users') ->setIdentityColumn('username') ->setCredentialColumn('password'); // Set the input credential values $uname = $request->getParam('username'); $paswd = $request->getParam('password'); $authAdapter->setIdentity($uname); $authAdapter->setCredential(md5($paswd)); // Perform the authentication query, saving the result $result = $auth->authenticate($authAdapter); if($result->isValid()){ //print_r($result); $data = $authAdapter->getResultRowObject(null,'password'); $auth->getStorage()->write($data); $this->_redirect('/user'); }else{ $this->_redirect('/user/loginform'); } 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 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 127 } public function logoutAction() { $auth = Zend_Auth::getInstance(); if(!$auth->hasIdentity()){ $this->_redirect('/user/loginform'); } $auth->clearIdentity(); $this->_redirect('/user'); } public function nameAction() { $auth = Zend_Auth::getInstance(); if(!$auth->hasIdentity()){ $this->_redirect('/user/loginform'); } $request = $this->getRequest(); $this->view->assign('name', $request->getParam('username')); $this->view->assign('gender', $request->getParam('gender')); $this->view->assign('title', 'User Name'); } public function registerAction() { $auth = Zend_Auth::getInstance(); if(!$auth->hasIdentity()){ $this->_redirect('/user/loginform'); } $request = $this->getRequest(); $this->view->assign('action',"process"); $this->view->assign('title','Member Registration'); $this->view->assign('label_fname','First Name'); $this->view->assign('label_lname','Last Name'); $this->view->assign('label_uname','User Name'); $this->view->assign('label_pass','Password'); 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 $this->view->assign('label_submit','Register'); $this->view->assign('description','Please enter this form completely:'); } public function editAction() { $auth = Zend_Auth::getInstance(); if(!$auth->hasIdentity()){ $this->_redirect('/user/loginform'); } $registry = Zend_Registry::getInstance(); $DB = $registry['DB']; $request = $this->getRequest(); $id = $request->getParam("id"); $sql = "SELECT * FROM `user` WHERE id='".$id."'"; $result = $DB->fetchRow($sql); $this->view->assign('data',$result); $this->view->assign('action', $request->getBaseURL()."/user/processedit"); $this->view->assign('title','Member Editing'); $this->view->assign('label_fname','First Name'); $this->view->assign('label_lname','Last Name'); $this->view->assign('label_uname','User Name'); $this->view->assign('label_pass','Password'); $this->view->assign('label_submit','Edit'); $this->view->assign('description','Please update this form completely:'); } public function processAction() { $auth = Zend_Auth::getInstance(); if(!$auth->hasIdentity()){ $this->_redirect('/user/loginform'); } $registry = Zend_Registry::getInstance(); $DB = $registry['DB']; $request = $this->getRequest(); $data = array('first_name' => $request->getParam('first_name'), 'last_name' => $request->getParam('last_name'), 'user_name' => $request->getParam('user_name'), 'password' => md5($request->getParam('password')) 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 ); $DB->insert('user', $data); $this->view->assign('title','Registration Process'); $this->view->assign('description','Registration succes'); } public function listAction() { $auth = Zend_Auth::getInstance(); if(!$auth->hasIdentity()){ $this->_redirect('/user/loginform'); } $registry = Zend_Registry::getInstance(); $DB = $registry['DB']; $sql = "SELECT * FROM `user` ORDER BY user_name ASC"; $result = $DB->fetchAssoc($sql); $this->view->assign('title','Member List'); $this->view->assign('description','Below, our members:'); $this->view->assign('datas',$result); } public function processeditAction() { $auth = Zend_Auth::getInstance(); if(!$auth->hasIdentity()){ $this->_redirect('/user/loginform'); } $registry = Zend_Registry::getInstance(); $DB = $registry['DB']; $request = $this->getRequest(); $data = array('first_name' => $request->getParam('first_name'), 'last_name' => $request->getParam('last_name'), 'user_name' => $request->getParam('user_name'), 'password' => md5($request->getParam('password')) ); $DB->update('user', $data,'id = '.$request->getParam('id')); 228 $this->view->assign('title','Editing Process'); 229 $this->view->assign('description','Editing succes'); 230 231 } 232 233 public function delAction() 234 { 235 $auth = Zend_Auth::getInstance(); 236 237 if(!$auth->hasIdentity()){ 238 $this->_redirect('/user/loginform'); 239 } 240 241 242 $registry = Zend_Registry::getInstance(); 243 $DB = $registry['DB']; 244 245 $request = $this->getRequest(); 246 247 $DB->delete('user', 'id = '.$request->getParam('id')); 248 249 $this->view->assign('title','Delete Data'); 250 $this->view->assign('description','Deleting succes'); 251 $this->view->assign('list',$request->getBaseURL()."/user/list"); 252 253 } 254 255} 256?> view plain | print   ======================================================================================    ======================================================================================    Zend Framework Session: Introduction Tag: framework, zend, zend framework, session Category: PHP Framework post: 23 Apr 2008 read: 1,621 Zend Framework Session Step By Step Tutorial - Part 1: Session is a familiar word in PHP programming. When we build complex application, usually, we involved session. In PHP, we know $_SESSION. It represent a logical, one to one connection between server-site, persistent state data, and user agent client (e.g, web browser). At Zend Framework, we can use Zend_Session. Zend_Session manage server-side data stored in $_SESSION. And a important class that we must know is Zend_Session_Namespace. Session namespaces provide access to session data using classic namespaces implemented logically as named groups of associative arrays, keyed by strings (similar to normal PHP arrays). Below, is quotes from zend framework manual: Zend_Session_Namespace instances are accessor objects for namespaced slices of $_SESSION. The Zend_Session component wraps the existing PHP ext/session with an administration and management interface, as well as providing an API for Zend_Session_Namespace to persist session namespaces. Zend_Session_Namespace provides a standardized, object-oriented interface for working with namespaces persisted inside PHP's standard session mechanism. Support exists for both anonymous and authenticated (e.g., "login") session namespaces. Zend_Auth, the authentication component of the Zend Framework, uses Zend_Session_Namespace to store some information associated with authenticated users. Since Zend_Session uses the normal PHP ext/session functions internally, all the familiar configuration options and settings apply (see http://www.php.net/session), with such bonuses as the convenience of an objectoriented interface and default behavior that provides both best practices and smooth integration with the Zend Framework. Thus, a standard PHP session identifier, whether conveyed by cookie or within URLs, maintains the association between a client and session state data. Still confuse? Don't worry, next post we will try to practice about session at Zend Framework. We will begin with Zend_Session_namespace. Zend Framework Session: Using Namespace Tag: framework, zend, zend framework, session, namespace Category: PHP Framework post: 23 Apr 2008 read: 2,005 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: To do this, open your UserController.php within application/controllers. Include Zend/Session/Namespace.php at first line after delimeter. 1require_once 'Zend/Session/Namespace.php'; view plain | print Edit loginFormAction() method become like this: 1 public function loginFormAction() 2 { 3 $request = $this->getRequest(); 4 5 $ns = new Zend_Session_Namespace('HelloWorld'); 6 7 if(!isset($ns->yourLoginRequest)){ 8 $ns->yourLoginRequest = 1; 9 }else{ 10 $ns->yourLoginRequest++; 11} 12 13$this->view->assign('request', $ns->yourLoginRequest); 14$this->view->assign('action', $request->getBaseURL()."/user/auth"); 15 $this->view->assign('title', 'Login Form'); 16 $this->view->assign('username', 'User Name'); 17 $this->view->assign('password', 'Password'); 18 19 } view plain | print 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: 1 2

escape($this->title);?>

3 You have entered this page: escape($this->request);?> time(s). 4
5 6 7 8 9 10 11 12 13 14
escape($this->username);?>
escape($this->password);?>
15 16
17 view plain | print 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: 1 public function loginFormAction() 2 { 3 $request = $this->getRequest(); 4 5 $ns = new Zend_Session_Namespace('HelloWorld'); 6 7 if(!isset($ns->yourLoginRequest)){ 8 $ns->yourLoginRequest = 1; 9 }else{ 10 $ns->yourLoginRequest++; 11} 12 13$this->view->assign('request', $ns->yourLoginRequest); 14$this->view->assign('action', $request->getBaseURL()."/user/auth"); 15 $this->view->assign('title', 'Login Form'); 16 $this->view->assign('username', 'User Name'); 17 $this->view->assign('password', 'Password'); 18 19 } view plain | print Then, update its view at application/views/scripts/user/userpage.phtml: 1 2

Hello, escape($this->real_name);?>

3You have entered this page: escape($this->request);?> time(s). 4Logout 5 view plain | print   Zend Framework Session: Accessing Session Data Tag: framework, zend, zend framework, session Category: PHP Framework post: 23 Apr 2008 read: 1,327 Zend Framework Session Step By Step Tutorial - Part 3: In this post, we learn how to access session data. Now, we talk about how to access the session. Please, give attention a code at previous post: 1$ns = new Zend_Session_Namespace('HelloWorld'); 2 3if(!isset($ns->yourLoginRequest)){ 4 $ns->yourLoginRequest = 1; 5}else{ 6 $ns->yourLoginRequest++; 7} view plain | print From above, we know how to set session at Zend framework. Just give properties such as: 1$ns->yourLoginRequest = 1; 2$ns->thisIsSession = "Oke"; 3$ns->nameOfSession = "MySession"; 4$ns->foo = 10; view plain | print Now, how to access the values? Just simple: 1echo $ns->yourLoginRequest; 2echo $ns->thisIsSession; 3echo $ns->nameOfSession; 4echo $ns->foo; view plain | print   Zend Framework Session: Seing All Values at Namespace Tag: Category: PHP Framework post: 23 Apr 2008 read: 814 Zend Framework Session Step By Step Tutorial - Part 4: This is a little tips to show all value at a namespace. As we know, session can have behavior like array. So, we can show values like array. Look this sample: 1$ns = new Zend_Session_Namespace('HelloWorld'); 2foreach ($ns as $index => $value) { 3 echo "ns->$index = '$value';"; 4} view plain | print Ok, try to test it. We have put session at loginform and userpage page. Now, we want to show request statistic for that page. Open your "UserController.php" within application/controllers. Add following method: 1public function statsAction() 2{ 3 $ns = new Zend_Session_Namespace('HelloWorld'); 4 foreach ($ns as $index => $value) { 5 echo "ns->$index = '$value';"; 6 echo "
"; 7 } 8 9} view plain | print Then, create a view for this action controller. Create a file named "stats.phtml" within application/views/scripts/user. Leave blank. Ok, try to point your browser to http://localhost/test/zend/helloworld/web_root/user/stats. You may get such as: 1ns->yourLoginRequest = '2'; 2ns->yourUserPageRequest = '7'; view plain | print Values depend on number of visiting. Zend Framework Session: Locking and Unlocking Namespace Tag: framework, zend, zend framework, session, namespace Category: PHP Framework post: 23 Apr 2008 read: 1,029 Zend Framework Session Step By Step Tutorial - Part 5: Session namespaces can be locked. What's that mean? When a namespace session is locked, we can not update or delete value of session. it will be readonly. We can use lock() to lock namespace. Then, if want to check whether locked or not, use isLocked(). Look this sample: 1 public function loginFormAction() 2 { 3 4 $ns = new Zend_Session_Namespace('HelloWorld'); 5 $ns->lock(); 6 7 if (!$ns->isLocked()) { 8 if(!isset($ns->yourLoginRequest)){ 9 $ns->yourLoginRequest = 1; 10 }else{ 11 $ns->yourLoginRequest++; 12 } 13 } 14 15 $request = $this->getRequest(); 16$this->view->assign('request', $ns->yourLoginRequest); 17$this->view->assign('action', $request->getBaseURL()."/user/auth"); 18 $this->view->assign('title', 'Login Form'); 19 $this->view->assign('username', 'User Name'); 20 $this->view->assign('password', 'Password'); 21 22 } view plain | print To unlock, you can use unLock() method. 1$ns= new Zend_Session_Namespace('HelloWorld'); 2 3// marking session as read only locked 4$ns->lock(); 5 6// unlocking read-only lock 7if ($ns->isLocked()) { 8 $ns->unLock(); 9} view plain | print   Zend Framework Session: Automatic Expiration Tag: framework, zend, zend framework, session, namespace Category: PHP Framework post: 24 Apr 2008 read: 1,268 Zend Framework Session Step By Step Tutorial - Part 6: We can set limited time for a namespace. For this feature, we called namespace expiration. Example, we want to count number of page request by a person in one minute. After 1 minute, he will be counted as new person. So, the code like this: We try at loginform. Open UserController.php within application/controllers. Update loginFormAction(): 1 public function loginFormAction() 2 { 3 4 $ns = new Zend_Session_Namespace('HelloWorld'); 5 6 if(!isset($ns->yourLoginRequest)){ 7 $ns->yourLoginRequest = 1; 8 }else{ 9 $ns->yourLoginRequest++; 10} 11 12$ns->setExpirationSeconds(60); 13 14 $request = $this->getRequest(); 15$this->view->assign('request', $ns->yourLoginRequest); 16$this->view->assign('action', $request->getBaseURL()."/user/auth"); 17 $this->view->assign('title', 'Login Form'); 18 $this->view->assign('username', 'User Name'); 19 $this->view->assign('password', 'Password'); 20 21 } view plain | print You can set only at a particular key (e.g, yourLoginRequest): 1$ns->setExpirationSeconds(60,'yourLoginRequest'); view plain | print  

Shared by: Harsh Gupta
Other docs by Harsh Gupta
PayPal_Sandbox_UserGuide
Views: 110  |  Downloads: 12
An Introduction to URL Rewriting - PHP
Views: 238  |  Downloads: 22
Protect you from session hacking
Views: 179  |  Downloads: 15
wright Portable Php code
Views: 393  |  Downloads: 7
PayPal_Sandbox_UserGuide
Views: 136  |  Downloads: 6
Writing Portable PHP Code.doc
Views: 202  |  Downloads: 5
An Introduction to URL Rewriting - PHP
Views: 185  |  Downloads: 11
How to protect Session Hacking
Views: 824  |  Downloads: 21
PHP and XML with SimpleXml Object
Views: 371  |  Downloads: 24
PHP and JAVA Conference Presentation
Views: 155  |  Downloads: 16
Related docs
Tutorial-Awal-memakai-Zend-Framework
Views: 213  |  Downloads: 19
PHP & Zend Framework Tutorial
Views: 1197  |  Downloads: 142
zend framework
Views: 827  |  Downloads: 44
Zend PHP Certification Tutorial
Views: 769  |  Downloads: 156
Zend Studio 5.5 User Guide
Views: 132  |  Downloads: 0
Visio-Zend Framework Workflow Chart.vsd
Views: 71  |  Downloads: 14
Zend Studio 5.5 Reviewer Guide
Views: 44  |  Downloads: 0
Framework for Action
Views: 18  |  Downloads: 0
Framework –
Views: 7  |  Downloads: 0
FRAMEWORK
Views: 1  |  Downloads: 0