Category Archives: Zend Framework

How to disable layout for some controller

There are some case we need call controller(action)-view with out Zend Layout (Layout.phtml)

In that case, add this line to your action.

$this->_helper->layout->disableLayout();
Posted in Tech Note, Zend Framework | Leave a comment

Error message for ‘Resource matching “view” not found’

Error Message ::

Fatal error: Uncaught exception ‘Zend_Application_Bootstrap_Exception’ with message ‘Resource matching “view” not found’ in /var/www/vhosts/zend/library/Zend/Application/Bootstrap/BootstrapAbstract.php:694 Stack trace: #0 /var/www/vhosts/zend/library/Zend/Application/Bootstrap/BootstrapAbstract.php(629): Zend_Application_Bootstrap_BootstrapAbstract->_executeResource(‘view’) #1 /var/www/vhosts/zend/library/Zend/Application/Bootstrap/BootstrapAbstract.php(586): Zend_Application_Bootstrap_BootstrapAbstract->_bootstrap(‘view’) #2 /var/www/vhosts/zend/application/Bootstrap.php(8): Zend_Application_Bootstrap_BootstrapAbstract->bootstrap(‘view’) #3 /var/www/vhosts/zend/library/Zend/Application/Bootstrap/BootstrapAbstract.php(669): Bootstrap->_initViewSetting() #4 /var/www/vhosts/zend/library/Zend/Application/Bootstrap/BootstrapAbstract.php(622): Zend_Application_Bootstrap_BootstrapAbstract->_executeResource(‘viewsetting’) #5 /var/www/vhosts/zend/library/Zend/Application/Bootstrap/BootstrapAbstract.php(586): Zend_Application_Boot in /var/www/vhosts/zend/library/Zend/Application/Bootstrap/BootstrapAbstract.php on line 694

Add

resources.view[] = ""

into application.ini file.

Posted in Error Note, Tech Note, Zend Framework | Leave a comment

Zend Framework, Create modules!

Create Module

[root@testLinux bin]# ./zf.sh create module admin

Creating the following module and artifacts:
/var/www/vhosts/zend/application/modules/admin/controllers
/var/www/vhosts/zend/application/modules/admin/models
/var/www/vhosts/zend/application/modules/admin/views
/var/www/vhosts/zend/application/modules/admin/views/scripts
/var/www/vhosts/zend/application/modules/admin/views/helpers
/var/www/vhosts/zend/application/modules/admin/views/filters
Added a key for path module directory to the application.ini file
Updating project profile ‘/var/www/vhosts/zend/.zfproject.xml’

Create Controller

[root@testLinux bin]# ./zf.sh create controller index index-action-included=1 admin

Note: PHPUnit is required in order to generate controller test stubs.
Note: The canonical controller name that is used with other providers is “Index”; not “index” as supplied
Creating a controller at /var/www/vhosts/zend/application/modules/admin/controllers/IndexController.php
Creating an index action method in controller Index
Creating a view script for the index action method at /var/www/vhosts/zend/application/modules/admin/views/scripts/index/index.phtml
Updating project profile ‘/var/www/vhosts/zend/.zfproject.xml’

Wow, Zend Framework is very nice. It is better than I thought.

Posted in Tech Note, Zend Framework | Tagged , | Leave a comment

Play with Zend Framework Model

I will make a ultra simple GuestBoard with Zend Framework.

(This is just for having taste the Zend Model ^^)

Firstly, create database and table

CREATE TABLE `guestBoard` (
`id` INT NOT NULL AUTO_INCREMENT ,
`name` VARCHAR( 100 ) NOT NULL ,
`note` TEXT NOT NULL ,
`date` DATETIME NOT NULL ,
PRIMARY KEY ( `id` )
)
INSERT INTO `guestBoard` (`name`,`note`,`date`)
VALUES
(‘Zend’,’Welcome to Zend World!’,now()),
(‘Codeigniter’,’I am the Best!’,now()),
(‘Symfony’,’Do not forget me!’,now())

Create Zend DbTable

[root@testLinux bin]# ./zf.sh create db-table GuestBoard guestBoard

Creating a DbTable at /var/www/vhosts/zend/application/models/DbTable/GuestBoard.php
Updating project profile ‘/var/www/vhosts/zend/.zfproject.xml’

Modify Model File

We just created /application/models/DbTable/GuestBoard.php file.

We need to modify the file to Select, Insert, Update and Delete.

Open GuestBoard.php and modify like this,

class Application_Model_DbTable_GuestBoard extends Zend_Db_Table_Abstract
{

    protected $_name = 'guestBoard'; //Table Name

	public function getGuestBoard($id){
		$id = (int)$id;
		$row = $this->fetchRow('id = '.$id);
		if(!$row){
			throw new Exception("Could not find row $id");
		}
		return $row->toArray();
	}

	public function setGuestBoard($name,$note){
		$data = array(
			"name" => $name,
			"note" => $note,
			"date" => new Zend_db_Expr("Now()"));
		$this->insert($data);
	}

	public function updateGuestBoard($id,$name,$note){
		$data = array(
			"name" => $name,
			"note" => $note);
		$this->update($data, 'id = '.$id);
	}

	public function deleteGuestBoard($id){
		$this->delete('id = '.(int)$id);
	}
}

Listing GuestBoard

Modify IndexAction in /application/controllers/indexController.php

public function indexAction(){
	$guestBoard = new Application_Model_DbTable_GuestBoard();
	$select = $guestBoard->select()->order('id DESC');
	$this->view->guestBoard = $guestBoard->fetchAll($select);
}

Modify View File in /application/views/scripts/index/index.phtml

Create (Zend) Form for Writing GuestBoard

Zend Form will create a form and validate the inputs.

[root@testLinux bin]# ./zf.sh create form GuestBoard

Creating a form at /var/www/vhosts/zend/application/forms/GuestBoard.php
Updating project profile ‘/var/www/vhosts/zend/.zfproject.xml’

It created /application/forms/GuestBoard.php

Modify Form

class Application_Form_GuestBoard extends Zend_Form
{
    public function init()
    {
        /* Form Elements & Other Definitions Here ... */
        $this->setName("GuestBoard");

        $id = new Zend_Form_Element_Hidden('id');
        $id->addFilter("Int");

        $name = new Zend_Form_Element_Text("name");
        $name -> setLabel("Name")
             -> setRequired(true)
             -> addFilter("StripTags")
             -> addFilter("StringTrim")
             -> addValidator("NotEmpty");

        $note = new Zend_Form_Element_Textarea("note");
        $note -> setLabel("Message")
             -> setAttrib('cols','20')
             -> setAttrib('rows','4')
             -> setRequired(true)
             -> addFilter("StringTrim")
             -> addValidator("NotEmpty");

        $submit = new Zend_Form_Element_Submit("submit");
        $submit->setAttrib("id","submitbutton");

        $this->addElements(array($id,$name,$note,$submit));
    }
}

Writing new GuestBoard

Add Action to controller (application/controllers/indexController.php)

public function addAction(){
	$form = new Application_Form_GuestBoard();
	$form->submit->setLabel("Add");
	$this->view->form = $form;

	if($this->getRequest()->isPost()){
		$formData = $this->getRequest()->getPost();
		if($form->isValid($formData)){
			$name = $form->getValue("name");
			$note = $form->getValue("note");
			$guestBoard = new Application_Model_DbTable_GuestBoard();
			$guestBoard->setGuestBoard($name,$note);

			$this->_helper->redirector("index");
		}else{
			$form->populate($formData);
		}
	}
}

Create View

Create new (/application/views/index/)add.phtml file

<?php 
echo $this->form;
?>

Update GuestBoard

Add action to controller

public function editAction(){
	$form = new Application_Form_GuestBoard();
	$form->submit->setLabel("Save");
	$this->view->form= $form;

	if($this->getRequest()->isPost()){
		$formData = $this->getRequest()->getPost();
		if($form->isValid($formData)){
			$id = (int)$form->getValue('id');
			$name = $form->getValue('name');
			$note = $form->getValue('note');

			$guestBoard = new Application_Model_DbTable_GuestBoard();
			$guestBoard->updateGuestBoard($id,$name,$note);

			$this->_helper->redirector('index');
		}else{
			$form->populate($formData);
		}
	}else{
		$id = $this->_getParam('id',0);
		if($id > 0){
			$guestBoard = new Application_Model_DbTable_GuestBoard();
			$form->populate($guestBoard->getGuestBoard($id));
		}
	}
}

Create Edit View File (edit.phtml)

<?php 
echo $this->form;
?>

Delete GuestBoard

Add action into controller

public function deleteGuestBoard($id){
	$this->delete('id = '.(int)$id);
}

Make view ( I believe I don’t have to explain how to build view for delete :) )

Posted in Tech Note, Zend Framework | Tagged , , | Leave a comment

Say “Hello, World” with Zend Framework

zend/appplication/controllers/indexController.php

class IndexController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        // action body
	// pass message to VIEW
        $this->view->message = "Hello, World!";
    }

}

zend/application/views/scripts/index/index.phtml

<?php echo $this->message; ?>

OUTPUT : Hello, World!

Set up layout

# zf enable layout

Layouts have been enabled, and a default layout created at
application/layouts/scripts/layout.phtml
A layout entry has been added to the application config file.

Now layouts folder has been created.

let’s modify it.

open application/layouts/scripts/layout.phtml

and type doctype and meta tags.

ex)

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<title></title>
</head>

<body>
<?php echo $this->layout()->content; ?>
</body>
</html>
Posted in Tech Note, Zend Framework | Tagged , | Leave a comment

How to install Zend Framework

Let’s taste Zend Framework! (I hope it is yammy ^^)

Firstly, installing Zend Framework.

1. Download Zend Framework latest version

http://framework.zend.com/download/latest

(I download to my computer (windows) first and I will upload files to my server by FTP)

2. Upload Zend Framework to your server.

Extract downloaded file, we can see the bin and library folder. We need 2 folders to install Zend framework.

Upload bin and library folder to your server.

(In my case : /var/www/vhosts/zend)

If you are using WAMP, just replace to your right folder :)

3. Install Zend Framework

zend/bin# ./zf.sh create project

(For Windows, using zf.bat. Ex) c:/wamp/zend> zf.bat create project)

output : -bash: ./zf.sh: Permission denied

(Are you kidding me? I am a ROOT and your MASTER dude~)

Hmm, let’s change zf.sh file permission @.@;

zend/bin# chmod og=rwxr zf.*

and try again,

zend/bin# ./zf.sh create project

Please provide a value for $path
zf>

Type right path for Zend project,

zf> /var/www/vhosts/zend

Creating project at /var/www/vhosts/zendNote: This command created a web project, for more information setting up your VHOST, please see docs/READMETesting Note: PHPUnit was not found in your include_path, therefore no testing actions will be created.

ioi, it created. now you can see the application,docs,public,tests folder in your Zend folder.

Try to open the Zend project with your browser!

http://localhost/zend/public/

4. Symlinking Zend libarary into our libarary

Goto your libaray and linking it

Ex)

# cd /usr/lib

# ln -s /var/www/vhosts/zend/libaray/Zend

5. Set Alias zf and zf.sh for later

alias zf = /zend_path/bin/zf.sh

Ex)

# alias zf = /var/www/vhsosts/zend/bin/zf.sh

For test,

# zf show version

OUTPUT : Zend Framework Version: 1.11.11

Finally, update virtual host for Zend project

<VirtualHost *:80>
    ServerAdmin root@localhost
    DocumentRoot /var/www/vhosts/zend/public
    SetEnv APPLICATION_ENV "development"
    ServerName zend.test.com
    ErrorLog logs/zend_error_log
    <Directory /var/www/vhosts/zend/public>
        DirectoryIndex index.php
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>

Now,we can open Zend index page without /public in URL :)

Posted in Tech Note, Zend Framework | Tagged , , | 1 Comment