ZF2系列 – Zend Framework 2 MVC實作 (Part 3)

我之所以這麼喜歡Zend Framework的關係,
其中一項就是它可以幫你很簡單的實現MVC的架構,
所以今天的任務就是要建立一個以MVC為架構的基本網頁。

首先當然要先建立一個測試用的資料庫,
因此我簡單的建立了一個名為Employee的資料表來進行展示,
該資料表如下:

很簡單的資料表吧,就三個欄位而已,
這我想我就應該不用多做解釋了,
只是拿來存放員工姓名與年紀資料而已。

好了,有了資料之後就能開始建立Model了,
首先在src\Application\下建了一個Model的資料夾,
並分別建立其ValueObject和Model的類別檔,
我先建立一個ValueObjec(src\Application\Model\Employee.php)給大家看:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
namespace Application\Model;
 
class Employee
{
	public $emp_id;
	public $emp_name;
	public $emp_age;
 
	public function exchangeArray($data)
	{
		$this->emp_id = (isset($data["emp_id"]))?$data["emp_id"]:null;
		$this->emp_name = (isset($data["emp_name"]))?$data["emp_name"]:null;
		$this->emp_age = (isset($data["emp_age"]))?$data["emp_age"]:null;
	}
}

這段ValueObject的程式碼應該沒有太大的問題,
唯一你會覺得怪怪的應該就是那個exchangeArray,
那個其實是要給之後Model用的方法,
所以你可以先暫時不理他,只要知道他的任務是用來為資料用的。

完成ValueObject之後,我們就可以開始介入建立一個新的Model,
所以建立一個新的Model(src\Application\Model\EmployeeTable.php):

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
namespace Application\Model;
 
use Zend\Db\Adapter\Adapter;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\AbstractTableGateway;
 
class EmployeeTable extends AbstractTableGateway
{
	protected $table = "Employee";
 
	public function __construct(Adapter $adapter)
	{
		$this->adapter = $adapter;
		$this->resultSetPrototype = new ResultSet();
		$this->resultSetPrototype->setArrayObjectPrototype(new Employee());
		$this->initialize();
	}
 
	public function fetchAll()
	{
		$resultSet = $this->select();
 
		return $resultSet;
	}
 
	public function getEmployee($id)
	{
		$id = (int)$id;
		$rowSet = $this->select(array("emp_id"=>$id));
		$row = $rowSet->current();
 
		return $row;
	}
 
	public function saveEmployee(Employee $employee)
	{
		$data = array("emp_name"=>$employee->emp_name,
					  "emp_age"=>$employee->emp_age);
		$this->insert($data);
	}
 
	public function deleteEmployee($id)
	{
		$this->delete(array("emp_id"=>$id));
	}
 
}

你有沒有看到建構函數時會執行setArrayObjectPrototype這個方法,
這時候裡面new了一個Employee作為其參數,
目的就是要將吐回來的資訊用ValueObject進行包裝的動作,
在這個Model中我們時做了幾個搜尋,新增,刪除的方法,
在今天我們只會用到fetchAll這個方法來取出所有資料,
若是有興趣的人可以自己參考我其他寫的資料操作方法。

Model完成後,就可以在Controller去嘗試取出資料,
我們拿出之前所寫好的IndexController來做一個小的展示,
其路徑放在 src\Application\Controller\IndexController.php

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
namespace Application\Controller;
 
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
 
class IndexController extends AbstractActionController
{
 
	protected $employeeTable;
 
    public function indexAction()
    {
    	$serviceManager = $this->getServiceLocator();
    	$this->employeeTable = $serviceManager->get("Application\Model\EmployeeTable");
 
    	$employees = $this->employeeTable->fetchAll();
 
        return new ViewModel(array("employees"=>$employees));
    }
 
    public function addAction()
    {
    	return new ViewModel();
    }
}

你可以看到我在indexAction的方法中,
先用ServiceManager把Model給載入進來,
接下來就可以使用背實作出來的Model,
透過fetchAll的方法來將資料庫所有員工資料取出,
最後由ViewModel來將資料交給View來進行呈現。

好啦,最後我們只要將View給實作出來就好,
這個應該是整段裡面最簡單的一環,
所以我就直接將code貼出來給大家看,
該View放在 src\view\application\index\index.phtml。

1
2
3
foreach ($employees as $employee)
{
printf("<li>NAME: %s, AGE: %d</li>", $employee->emp_name, $employee->emp_age); }

最後呈現的成果如下圖:

原文地址:https://www.cnblogs.com/xuyaoxiang/p/3039781.html