Zend Framework and PRADO - successful cooperation

 URL:http://www.alexatnet.com/Blog/Index/2006-06-28/zend-framework-and-prado-successfull-cooperation

Zend Framework and PRADO - successful cooperation

Written on June 28, 2006 in PHP, PRADO, Zend framework

Zend Framework user interface components are not developed yet and this is what forces developers to look for another component-based framework and try to use them together in one project. There are a few great frameworks for this, but an undoubtedly perfect one is the winner of Zend PHP 5 coding contest—the PRADO framework, which is an open-source, a component-based and event-driven framework, written in PHP5. It is free (BSD-licensed) and you can use it in eigher open-source and proprietary applications.

Best features selection is a key of almost any developing strategy and we are about to use it for this review. The implementation of Model-View-Controller patterns and data layer classes are Zend Framework’s strongest parts. The PRADO’s advantage is a component-oriented design of the application’s pages. So what we are about to do is to use Zend_Controller and Zend_Db for serving requests and data layer creating and create a couple of pages with PRADO’s components. These pages will be called in the corresponding controller’s actions.

In a few following paragraphs I will demonstrate you how to create a page that hosts basic PRADO’s components and run it from the particular Zend_Controller action. The page’s components will be based on the data layer classes, which are created with Zend_Db_Table class. But at first you need to read a little bit more about the technologies that are discussed here—an excellent guide about the Zend_Controller is the first necessary reading for the framework’s neophyte. The application’s data layer can be created in numerous ways, but a recommended approach is briefly described in the Zend_Db manual and I’ve write down a few thoughts about the data layer in Programming a data layer classes article. Actually, instead of duplicating of the content, I will use the classes that are defined in my article to provide the PRADO’s components with data.

The PRADO project design requires some description here. A typical PRADO project consists of pages’ files, located in “pages” folder and application state files, located in “runtime” folder. Optionally, “assets” folder may be created to store associated media files, such as pictures, flash animations, etc.

Two files defines a PRADO page: template file that defines which components are on the page and their design, and page code file with page’s events and related classes. Digg a little bite deeper and read an introduction to PRADO’s templates (Part I, Part II and Part III). The code file typically contais a page class, which is a subclass of the TPage class. You can read the My First PRADO Application to learn an example of the project structure and sample “Hello World!” page, which demonstrates basic PRADO’s concepts.

So now you have a fiew links that are related to this review (feel free to read the PRADO’s QuickStart Tutorial for more information) and we can focus on creating of the Zend Framework action, which will instantiates and runs a PRADO application class. Zend’s recommended project structure defines “view” folder with application’s presentation classes, which are intended to be used by Zend_View class. But what we need is to extend the presentation files with PRADO pages and place them into the “application/view/prado” folder. As mentioned in PRADO’s QuickStart Tutorial, we need to create two more folders, “pages” and “runtime”, for PRADO project. So, the structure of the application’s folder will be the following:

application
/controllers
/models
/views
/prado
/pages
/runtime

Our first PRADO page will be HelloWorld example. By adding this page we will check that we are moving in the right direction and our controller successfully runs simple pages. The source code for this page is following (the paths are relative to application’s root folder):

./application/views/prado/pages/Page1.page
<html>
<body>
<com:TForm>
<com:TButton Text="Click me" OnClick="buttonClicked" />
</com:TForm>
</body>
</html>

./application/views/prado/pages/Page1.php
<?php

class Page1 extends TPage
{
public function buttonClicked($sender,$param)
{
// $sender refers to the button component
$sender->Text = "Hello World!";
}
}

?>

./application/controllers/PradoController.php
<?php

// the path to prado.php from the PRADO framework
require_once './prado/framework/prado.php';

// defines an action for "http://myhost/Prado" url.
class PradoController extends Zend_Controller_Action {

function indexAction() {
// "page" query string parameters specifies the page name
// that the PRADO framework should process.
$_GET['page'] = 'Page1';

// Create and start PRADO application.
// The constructor's argument defines path to folder
// with prado pages.
$app = new TApplication('./application/views/prado');
$app->run();
}

}

?>

The example above shows how to create a simple page and now is a time for creating more advanced one. PRADO has a lot of useful components (assembled in System.Web.UI.WebControls namespace) and now we are interested in data bound components, such as the DataGrid or Repeater components.

As mentioned, DataGrid is a data bound component so we need to provide it with data. We will use for this the Users data layer class, which is defined in my previous article. The following example demostrates how to place the DataGrid component and show the data from “Users” table in it:

./application/views/prado/pages/Page1.page
<html>
<body>
<com:TDataGrid ID="Users">
</com:TDataGrid>
</body>
</html>

./application/views/prado/pages/Page1.php
<?php

class Page1 extends TPage
{
public function onInit($param)
{
if(!$this->IsPostBack)
{
$dbUsers = new DbUsers();
$users = array();
// copy data from rowset into the $users array
foreach ($dbUsers->fetchAll() as $dbUser) {
$users[] = array('id' => $dbUser->id,
'name' => $dbUser->login);
}
$this->Users->setDataSource($users);
$this->Users->dataBind();
}
}
}

?>

./application/controllers/PradoController.php
<?php

require_once './prado/framework/prado.php';

// The database.php file contains the "DbUsers" class
require_once './application/models/database.php';


class PradoController extends Zend_Controller_Action {

function indexAction() {
$_GET['page'] = 'Page1';
$app = new TApplication('./application/views/prado');
$app->run();
}

}

?>

So the fun begins and you now can learn more about the DataGrid component in the TDataGrid guide, which is a part of the PRADO QuickStart Tutorial. Stay tuned and read more about PHP programming and other interesting things.

Update: It may be confusing that two PRADO versions exists and they are hosted on different sites: on xisc.com and pradosoft.com. The right one is pradosoft.com—it hosts now the latest PRADO version. The xisc.com host is outdated.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值