Zend Studio 6 + Zend Framework進階教程之二

主要內容:基本的mvc結構,基本的使用包括在控制器中傳遞數據到視圖、爲頁面設置js腳本、css樣式,如何在視圖中引用圖片

鑑於本節內容較少,工程代碼就不附上了。如果需要本節代碼,請在回覆中說明。

1、在IndexController.php控制器中加入如下代碼

public function indexAction()
    {
        // step 1 傳遞數據到視圖
        $books = array ();
        $books [] = array ('isbn' => 'No.001', 'title' => 'java' );
        $books [] = array ('isbn' => 'No.002', 'title' => 'PHP' );
        $this->view->books = $books;

    }

在視圖文件(index.phtml)顯示的代碼如下

<h3>Hello, world!</h3>
<table>
<?php
foreach ($this->books as $book) {
?&gt;
<tr><td><?php echo $book['isbn']?></td><td><?php echo $book['title']?></td></tr>
<?php
}
?>
</table>

2、在工程的public\index.php上右鍵選擇Run as\Php Web Page,彈出的對話框中的Launch URL如下:

http://zftest.localhost/zfstepbystep/public/index.php

確定後可以看到結果頁面

 

3、在public\styles\新建demo.css文件,內容如下

@CHARSET "UTF-8";
h3 {
    color: red;
}

下面我們如何爲視圖增加css鏈接。

<?php

/**
* Default home page view

* @author
* @version
*/

$this->headTitle('New Zend Framework Project');
$this-&gt;placeholder('title')-&gt;set('Welcome');

// 增加css代碼
//$this-&gt;headStyle()-&gt;appendStyle('h3 {color:red;}');

// 增加css樣式文件
$this-&gt;headLink()-&gt;appendStylesheet('styles/demo.css');

// 增加javascript腳本文件
$this-&gt;headScript()-&gt;appendFile('scripts/demo.js');

 

。。。以下省略

再次訪問http://zftest.localhost/zfstepbystep/public/index.php

發現h3標題部分顏色變紅色了

 

image

4、調用js腳本的類同,這裏就不細陳述了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章