1、安裝
(1)php環境安裝請移步:
windows下php環境安裝
linux下php環境配置
2、配置
Niginx 下配置 Phalcon
使用 Host 配置(Configuration by Host):
server{
listen 80;
server_name xxx.net;
charset utf-8;
set $root_path '/Library/xxx/xxx/public';
root $root_path;
index index.php index.html index.htm;
try_files $uri $uri/ @rewrite;
location @rewrite {
rewrite ^/(.*)$ /index.php?_url=/$1;
}
location ~ \.php {
# try_files $uri =404;
fastcgi_index /index.php;
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location ~* ^/(css|img|js|flv|swf|download)/(.+)$ {
root $root_path;
}
location ~ /\.ht {
deny all;
}
access_log log/operator.log;
3、配置文件與入口文件
方式一、直接在index文件中配置
// 初始化自動加載器
$loader = new Loader();
$loader->registerDirs(
array(
'../app/controllers/',
'../app/models/'
)
)->register();
// 創建DI(依賴注入器),初始化註冊組件
$di = new FactoryDefault();
// 數據庫配置
$di['db'] = function() {
return new DbAdapter(array(
'host' => 'localhost',
'username' => 'root',
'password' => '123456',
'dbname' => 'test'
'charset' => 'utf8'
));
};
// 設置視圖文件目錄
$di['view'] = function() {
$view = new View();
$view->setViewsDir(APP_PATH.'/app/views/');
$view->setLayoutsDir('/layouts/');
$view->setTemplateBefore('before');
$view->setLayout('main');
$view->setTemplateAfter('after');
$view->setMainView('index');
$view->setPartialsDir('/partials/');
$view->start();
return $view;
};
// 註冊一個基URI,這樣所有使用Phalcon生成的URI裏就會包含我們定義的phalcon。
// 這在以後我們使用\Phalcon\Tag生成URI時是非常重要的。
$di['url'] = function() {
$url = new Url();
$url->setBaseUri('/phalcon/');
return $url;
};
// Setup the tag helpers
$di['tag'] = function() {
return new Tag();
};
// 處理請求
$application = new Application($di);
echo $application->handle()->getContent();
4、幾個不錯的入門教程
(1) [官網示例教程](http://docs.phalconphp.com/zh/latest/index.html)
(2) [部分源碼分析](http://avnpc.com/pages/phalcon-mvc-process)
5、頁面交互
(1)判斷是否是post:$this->request->isPost()
(2)獲取參數:$this->request->getPost('param')
(3)設置session: $this->session->set('auth', mix);
獲取session:$this->session['auth']或$this->session->get('auth')
(4)跳轉:return $this->forward('index/index');
(5)設置提示息:$this->flash->success('success');$this->flash->error('wrong');
接收提示信息:$this->flash->output();
(6)設置標題:\Phalcon\Tag::setTitle('hello');接收標題:\PhalconTag::getTitle();
6、數據的操作
(1)添加數據
$user = new Users();
$user->email = $this->request->getPost('email','email');
$user->password = $this->request->getPost('password','string');
$user->create();
更新數據:$user->update();
或者使用save方式,save方式有主鍵的時候會是更新操作
$robot = new Robots();
$robot->save($_POST, array('name', 'type'));
(2)查詢數據
//根據id獲取一條數據詳細信息
$user = Users::findFirstById($id);
//根據查詢條件獲取數據
$user = Users::findFirst(array(
"email = :email: AND password = :password:",
'bind' => array('email' => $email, 'password' =>$password)
));
//查詢多條數據
$users = Users::find(array('limit' => 2,'order'=>'id DESC'));
$robots = Robots::query()
->where("type = :type:")
->andWhere("year < 2000")
->bind(array("type" => "mechanical"))
->order("name")
->execute();
(3)更新數據
phalcon默認爲全部字段更新,動態更新則只更新修改的字段
(4)刪除數據
//刪除數據
$user = Users::findFirstById($id)->delete();
可以在model中initialize()方法中設置軟刪除字段
$this->addBehavior(new SoftDelete(
array(
'field' => 'state',
'value' => self::DELETED,
)
));
(5)條件參數
find(
鍵名 示例
conditions “conditions” => “name LIKE ‘steve%’”
columns “columns” => “id, name”
bind “bind” => array(“status” => “A”)
bindTypes “bindTypes” => array(status::BIND_TYPE_INT)
order “order” => “name DESC, status”
limit “limit” => 10 或“limit” => array(“number” => 10, “offset” => 5)
group “group” => “name, status”
for_update “for_update” => true
shared_lock “shared_lock” => true
cache “cache” => array(“lifetime” => 3600, “key” => “my-find-key”)
hydration 設置返回的結果集類型 “hydration” => Resultset::HYDRATE_OBJECTS
5、model
爲model指定數據表:
1、在 initialize()方法添加
$this->setSource(“the_robots”);
2、重寫getSource方法
public function getSource()
{
return ‘test’;
}
關係:
hasMany hasOne belongsTo hasManyToMany
驗證:
PresenceOf 非空
email 驗證字段包含一個有效的電子郵件格式
ExclusionIn 是否不在一個可能值數組中
InclusionIn 驗證一個值是否在一個可能值數組中
Numericality 驗證數字格式
StringLength 驗證一個字符串的長度
Url 驗證一個值有一個有效的URL格式
加入model: