啟動方式

1
2
3
4
5
6
7
8
9
10
$di->set('router', function () {
$router = new \Phalcon\Mvc\Router();
$router->add("/:controller/:action/:params", array(
'controller' => 1,
'action' => 2,
'params' => 3
));

return $router;
});

Defining Route

Placeholder Regular Expression Usage
/:module /([a-zA-Z0-9_-]+) Matches a valid module name with alpha-numeric characters only
/:controller /([a-zA-Z0-9_-]+) Matches a valid controller name with alpha-numeric characters only
/:action /([a-zA-Z0-9_]+) Matches a valid action name with alpha-numeric characters only
/:params (/.) Matches a list of optional words separated by slashes. Use only this placeholder at the end of a route
/:namespace /([a-zA-Z0-9_-]+) Matches a single level namespace name
/:int /([0-9]+) Matches an integer parameter

/:params : 只能放在router的最後面

#controller內取得route裡面參數的方式

1
2
3
$this->dispatcher->getParam(0);
$this->dispatcher->getParam('paraName');
$this->dispatcher->getParams(); <=array

#如果是/?var=value

就用原本$_GET[『』]的方式取值就可以了

  1. commit your code
1
2
git add .
git commit -m "xxxx"
  1. merge your branch to master 以master為主要branch
1
2
3
4
5
git checkout master
git merge [module name]<= this should be branch name
```

3. update

git pull origin master

1
2
3
檢查是否有conflict要修

4. push至origin

git push origin master:[module name]

1
2
3
4

5. 通知alvin合併

6. 拉別人的code

git add remote kyo /home/kyo/web/mc2

1
2

7. 直接合併於目前的branch EX. 拉KYO的master

git pull remote kyo master

1
2

8. 拉成新的branch EX. 拉KYO的master為自己的kyo branch

git fetch remote kyo master:kyo

1
2
3
4

9. git 環境設定

###設定個人資訊

git config --global user.name 「Your Name」

git config --global user.email [email protected]


###設定git環境

cd ~ #進入個人home目錄 
ex./home/fish
vi .gitconfig 

####編輯git環境檔,貼上以下環境設定變數

[color]
 
 branch = auto
 
 diff = auto
 
 status = auto

[color "branch"]

  current = yellow reverse
 
  local = yellow 

  remote = green

[color "diff"]
 
 meta = yellow bold
 
 frag = magenta bold
 
 old = red bold
 
 new = green bold

[color "status"]

  added = green
 
  changed = red

In Phalcon Model system. 是根據ORM的架構,讓資料表物件化,讓在資料讀取或是寫入的動作上,可以利用物件的特性去操作。

初始設定

1
2
3
4
5
6
7
8
9
10
11
/**
* Database connection is created based in the parameters defined in the configuration file
*/
$di->set('db', function() use ($config) {
return new DbAdapter(array(
'host' => $config->database->host,
'username' => $config->database->username,
'password' => $config->database->password,
'dbname' => $config->database->dbname
));
});

上列的動作會開啟與資料庫中間的連線關係,$config會去讀取所指定的設定文件

Model文件預設的命名方式為table名稱,例如:

1
2
3
class Rebots extends \Phalcon\Mvc\Model
{
}

官方文件建議,要在Model文件中描述資料表的欄位,以減少使用伺服器的記憶體資源。

如果Model的名稱與資料庫資料表名稱不同時,可以透過以下的方式重新設定所關聯的資料表名稱

1
2
3
4
5
6
7
8
9
10
public function initialize()
{
$this->setSource('Rebots');
}
```

#於Controller中使用Model讀取資料

讀取清單的方式有幾種

ModelName::find(condition)

1
2
3

當找到筆資料時,所回傳的物件可以直接修改後,並儲存(Update)

$result = ModelName::find(『id=1』); $result->fieldName = 「something something」; $result->save(); <=將資料回傳至資料庫中

1
2
3

新增動作

$result = new ModelName(); $result->fieldName = 「something something」; $result->save();

1
2
3

刪除動作

$result = ModelName::find(condition); if ($result != false) { // 執行刪除 if ($result->delete() == false) {
echo 「Delete Failed」; foreach ($result->getMessages() as $message) { echo $message, 「\n」; } } else { echo 「Deleted successfully!」; } }

#Hierarchical Rendering Phalcon當在使用View的時候,執行的順序是

  1. app/views/index.volt
  2. app/views/layouts/指定.volt(有[指定時][1],才會發生)
  3. app/views/layouts/{{controllerName}}.volt
  4. app/views/{{controllerName}}/xxx.volt

依造這種順序,

在index.volt裡面的{{content()}},就會去呼叫app/views/layouts裡面的內容, 然後再app/views/layouts裡面的{{content()}}就是呼叫app/views/{{controllerName}}/xxx.volt的內容

在Controller裡面所設定的變數,則上述三種xxx.volt都可以用

[1]: 如果要指定template時, 可以用下列的方式設定, 這樣子就會去讀取app/views/layouts/common.volt的內容了,

1
2
3
4
public function initialize()
{
$this->view->setTemplateAfter('common');
}
這一個指定template的動作,執行順序會插在app/views/index.volt跟app/views/layouts/{{controllerName}}.volt中間

#Using Partials

如果想要在view中間插入partials的方式很簡單

如果是使用volt語法的:

1
2
3
{{ partial(view path) }}
//with parameters
{{ partial(view path, ['links': $links]) }}

一般的語法

1
2
3
<?php $this->partial(view path) ?>
//with parameters
<?php $this->partial(view path, array('id' => $site->id, 'size' => 'big')) ?>