指導教授:柯博昌 教授 組員: 1101345102 黃聖哲 1101345103 張宜富 1101345109...
TRANSCRIPT
Solar Framework指導教授:柯博昌 教授組員:
1101345102 黃聖哲1101345103 張宜富1101345109 鄒佩珊1101345110 涂雯婷1101345117 杜姿蓉
2
怎麼開始使用 Solar Framework
使用心得
Solar 的概念與操作 Solar 的 mvc 架構實作 DEMO
2
3
4
5
6
Solar 簡介1
目錄
3
Paul M. Jones 創辦 正式發布於 2010 年 11 月 9 號最新版本 1.1.2 版 發布於 2011- 12-20
Solar 簡介
4
2. 怎麼開始使用 Solar Framework?
5
6
3.Solar 的概念與操作3.1 命名空間與規則3.2 資料庫操作介紹3.3 整體架構
7
命名規則 Ex: Controller 的 action
命名 actionIndex, actionRead
Class-to-file 規則 Ex:
類別名稱:Vender_Model_Blogs
存放路徑: Vender/Model/Blogs
URL rewriter 127.0.0.1/controller/action/param
3.1 制定命名空間與規則
8
SQL 轉接器 支援 Mysql, Pgsql, Sqlite, Sqlite2, Oracle, Mssql
SQL Query 物件
內建防止 SQL injection
3.2 資料庫操作介紹
'eager' => array('author', 'summary', 'comments', 'tags'),'where' => array('blogs.status = ?' => ‘public’),'group' => ...'order' => 'blogs.created DESC','page' =>1, // 當前頁數'paging' => 8, // 每頁顯示頁數'count_pages' => true, // 自動計算頁數
9
3.3 整體架構project-system/
config/docroot/ # vhost document include/ # PHP 系統檔案位置script/ # 安裝命令檔source/ # Solar 函式庫sqlite/ # sqlite databasestmp/ # sessions, logs, caches, etcconfig.php # config fileindex.php # bootstrap
10
$config["Solar_Sql"] = array("adapter" => "Solar_Sql_Adapter_Mysql","host" => "localhost","user" => "admin","pass" => "admin","name" => "pzblog",);
Config.php Database 設定
11
Index.php Bootstrap 啟動器$system = dirname(dirname(__FILE__));set_include_path("$system/include");require "Solar.php";$config = "$system/config.php";Solar::start($config);$front = Solar_Registry::get("controller_front");$front->display();Solar::stop();
12
系統結構
13
Solar 的 MVC 架構
14
15
主要 Controller
class Acme_App_Blog extends Acme_Controller_Page { protected function _setup() { ........ } public function actionIndex() { ........ } public function actionRead() { ........ }
路徑: /var/www/source/acme/Acme/App/Blog.php
16
Controller-actioIndex public function actionIndex() { $fetch = array( 'where' => array('blogs.status = ?' => 'public'), 'order' => 'blogs.created DESC', 'page' => 'all', ); // fetch all matching records $this->list = $this->_model->blogs->fetchAll($fetch);
}
17
Model
class Acme_Model_Blogs extends Acme_Sql_Model{ protected function _preSetup() { // chain to parent parent::_preSetup(); // use metadata generated from make-model $metadata = Solar::factory('Acme_Model_Blogs_Metadata'); $this->_table_name = $metadata->table_name; $this->_table_cols = $metadata->table_cols; $this->_index_info = $metadata->index_info; }
路徑: /var/www/source/acme/Acme/App/Blog/model/Blog.php
18
protected function _setup() { // chain to parent parent::_setup();// add a validation filter on the status column
$this->_addFilter('status', 'validateInList', array( 'draft', 'public', ))
Model
19
View-index
<?php if (! $this->list): ?><p><?php echo $this->getText('ERR_NO_RECORDS'); ?></p><?php else: ?> <ul> <?php foreach ($this->list as $item): ?> <li><?php echo $this->escape($item->title); echo " "; echo $this->action( "{$this->controller}/read/{$item->id}", 'ACTION_READ' ); ?></li><?php endforeach; ?></ul><?php endif; ?>
路徑: /var/www/source/acme/Acme/App/Blog/view/index.php
20
5. 系統實作5.1 實作 DEMO5.2 實作介紹
22
實作了什麼 ? Controller : Acme/App/Blog.php
actionIndex() actionDrafts() actionRead() actionEdit() actionDelete()
Model : Acme/Model/Blogs.php View : Acme/App/Blog/View/…
◦ 新增、修改、刪除、閱讀
23
實作功能自動生成表單SQL 操作方法
24
自動生成表單 (View)
<?phpecho $this->form() ->auto($this->form) ->decorateAsDivs() ->addProcess('save') ->fetch();?>
<?phpecho $this->form() ->addProcess('delete') ->fetch();?>
25
$this->form = $this->item->newForm(array( 'body' => array( 'type' => 'textarea', 'label' => '內容 ', 'attribs' => array( 'style' => 'border:solid 1px black;', 'cols' => '60', 'rows' => '10', ), ), )); $this->_response->setNoCache();
自動產生表單 (Contoller)
26
加入按鈕事件save() 方法if($this->_isProcess('save')) { $data = $this->_request->post('blog'); $this->item->load($data); $this->item->save();}
delete() 方法if($this->_isProcess('delete')){ $this->item->delete(); $this->_view = 'deleteSuccess';}
自動產生表單 (Contoller)
27
SQL 操作方法 (FK 關聯 ) DB 關聯方法 $this->_hasOne('summary'); $this->_belongsTo('author'); $this->_hasMany('taggings'); $this->_hasManyThrough('tags', 'taggings'); $this->_hasMany('comments', array( 'order' => array('`id` DESC'), ));
28
SQL 操作方法 (Fetch 進階 )
Fetch 其他方法透過 author id 取得 author name
將取得的資料轉化成 Array
取得單筆資料
透過 id 取得 blogs 內這筆 id 的所有欄位
資料
$author = $model->authors->fetchPairs('id', 'name');$this->_addFilter('author_id', 'validateInKeys',$author);
$array = $model->invaders->fetchAllAsArray(array(...));
$record = $model->invaders->fetchOne(array(...));
$item = $this->_model->blogs->fetch(id);
29
Solar 內建提供的樣版
30
31
樣板路徑: ./layout/CSS 路徑: ./styles/bolg.css樣板架構:
blog.php
_head.php
_head.php
_nav.php
_content.php
_footer.php
_body.php
32
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"> <?php
// generate the <head>include $this->template('_head.php');
?> <?php
// generate the <body>include $this->template('_body.php');
?></html>
blog.php
33
<head> <?php // use the Solar_View_Helper_Head::head() helper // add a base stylesheet, then set any other head
elements echo
$this->head()->addStyleBase('/Acme/styles/blog.css') ->fetch(); ?>
</head>
_head.php
echo $this->head()->addScript('/Acme/Controller/Page/Pubic/libs/js/menu-js.js')
->fetch(); ?>
34
<?php // set the id of the body tag. Handy for css and javascript ?><body id="<?php echo "{$this->controller}-page" ?>"> <div id="wrap">
<div id="header"> <?php include $this->template('_header.php'); ?>
</div> <div id="nav">
<?php include $this->template('_nav.php'); ?> </div> <div id="main">
<?php // Add the content from the application controller ?>
<?php echo $this->layout_content; ?> </div> <div id="footer">
<?php include $this->template('_footer.php'); ?> </div>
</div></body>
_body.php
35
<div id="branding"> <h1>Solar PHP Demo</h1>
</div>
_head.php
<div id="nav"> <?php echo $this->action('blog', 'Blog Home'); ?><?php echo $this->action('blog/add', 'ACTION_ADD'); ?><?php echo $this->action('blog/drafts', 'View Drafts'); ?>
</div>
_nav.php
36
<?php // allow for extra local navigation include $this->template('_local.php');
?>
_content.php
<div id="footer"> <p>Copyright © 2013 Group 1</p>
</div>
_footer.php
37
如何自行套版
38
_header.php
<?php echo $this->layout_content; ?>
_nav.php
_footer.php
39
6. 小組使用心得
40
1. 實作上遇到的困難• 花許多時間了解架構• 較少人使用參考文獻少
2. 使用後的心得• 命名規則和檔案目錄一致性• Solar 內建 SQL injection 和
Script 防止漏洞• 提供許多好用的 API
3. 建議
41
Thanks for your listening