PHP code example of ntch / pocoapoco
1. Go to this page and download the library: Download ntch/pocoapoco library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
ntch / pocoapoco example snippets
use Ntch\Pocoapoco\WebRestful\Routing\Router;
$router = new Router();
$router->controller($uri, $path, $file, $method);
$router->view($uri, $path, $file, $data);
$router->public($path, $file);
$router->mix($uri,
[
'controller' => [$path, $file],
'libraries' => [$libraryName],
'log' => ['$logName'],
'mails' => ['$mailName'],
'aws' => ['$awsName'],
'models' => ['$nickname'],
]);
// example
$router->controller('/uri/:parameter', '/path', 'class', 'method');
$router->view('/uri/:parameter', '/path', 'class', ["pocoapoco" => "framework"]);
$router->public('/path', 'index.html');
$router->mix('/uri/:parameter',
[
'controller' => ['/path', 'class', 'method'],
'libraries' => ['name'],
'mails' => ['name'],
'aws' => ['name'],
'models' => ['name'],
]);
use Ntch\Pocoapoco\WebRestful\Controllers\Controller;
# 如要導向 view 請把 Router 導入
use Ntch\Pocoapoco\WebRestful\Routing\Router;
class xxx extends Controller
{
public function index()
{
$router = new Router();
$router->view(null, '/path', 'class', array("name" => "Pocoapoco"));
}
}
# 依據 PSR-4 命名規則,給予路徑
namespace libraries\la\lb\lc;
class xxx
{
public function index()
{
echo 'library import success!' . PHP_EOL;
}
}
$router->mvc('/uri',
[
'controller' => ['/path', 'class'],
'libraries' => ['lib']
]);
use Ntch\Pocoapoco\WebRestful\Controllers\Controller;
use la\lb\lc\xxx;
class test extends Controller
{
public function index()
{
$lib = new xxx();
$lib->index();
}
}
use Ntch\Pocoapoco\WebRestful\Models\Model;
class model_demo extends Model
{
public function schema()
{
$schema['COLUMN_NAME'] = [
'DATA_TYPE' => '',
'DATA_SIZE' => '',
'NULLABLE' => '',
'DATA_DEFAULT' => '',
'KEY_TYPE' => '',
'COMMENT' => '',
'SYSTEM_SET' => ''
];
return $schema;
}
}
$router->mvc('/uri',
[
'controller' => ['/path', 'class'],
'models' => ['nickname']
]);
use Ntch\Pocoapoco\WebRestful\Controllers\Controller;
class test extends Controller
{
public function index()
{
# model 導入
$model = $this->model['nickname'];
# ORM 架構
# createTable 範例
$sql = $model->createTable();
# commentTable 範例
$sql = $model->commentTable();
# select 範例
$data = $model->select(['a', 'b', 'SUM(c)' => 'c'])->
where(['b' => 1])->groupby(['a', 'b'])->
orderby(['a'])->query();
# insert 範例
$data = $model->insert()->values(['a' => 1])->query();
# update 範例
$data = $model->update()->set(['a' => 1])->where(['b' => 2])->query();
# delete 範例
$data = $model->delete()->where(['a' => [1, '>']])->query();
# merge 範例 - 僅提供 Oracle 使用
$data = $model->merge()->using('user', 'table')->on("a", "b")->
matched()->update()->set(['c' => 'c', 'd' => 'd'])->
not()->insert()->value()->query();
# commit 範例
$model->commit();
# rollback 範例
$model->rollback();
# 筆數限制 - 第 5 筆開始顯示 8 筆
# Oracle 範例 - 若兩個方法皆要使用 offset() limit() 順序可顛倒
$data = $model->select()->offset(5)->limit(8)->query();
# Mysql 範例 - 若兩個方法皆要使用 limit() offset() 順序可顛倒
$data = $mysql->select()->limit(8)->offset(5)->query();
# Mssql 範例 - 兩種方法
$data = $mssql->select()->top(8)->query();
$data = $mssql->select()->groupby()->offset(5)->fetch(8)->query();
# Postgres 範例 - 若兩個方法皆要使用 limit() offset() 順序可顛倒
$data = $postgres->select()->limit(8)->offset(5)->query();
}
}
# 依據 PSR-3 規則,給予層級
$this->log($level, $message, $info);
// example
$this->log('INFO', 'message', ['name' => 'pocoapoco']);
$res = $mailer->header($header)->
from($from)->
to($to)->
subject($subject)->
content($source, $type, $content, $data)->
send();
// example
$mailer = $this->mail['mailer'];
$header = ['ID' => 'xxxxxxxx'];
$from = ['[email protected] ' => 'POCOAPOCO'];
$to = ['[email protected] ' => 'ROY LEE'];
$res = $res = $mailer->header($header)->
from($from)->
to($to)->
subject('test')->
content('local', 'html', '/path/error.php', ['error' => 'message'])->
send();
$aws = $this->aws['name'];
// example
$aws->s3_exist('bucket', '/aws_path', 'aws_file.txt', ['key' => 'xxxxxxxxxxxx', 'md5' => 'oooooooooooo']);
$aws->s3_list('bucket');
$aws->s3_upload('bucket', '/aws_path', 'aws_file.txt', '/local_path', 'local_file.txt', 2, 1)
$aws->s3_read('bucket', '/aws_path', 'aws_file.txt', ['key' => 'xxxxxxxxxxxx', 'md5' => 'oooooooooooo'])
$aws->s3_download('bucket', '/aws_path', 'aws_file.txt', '/local_path', 'local_file.txt', ['key' => 'xxxxxxxxxxxx', 'md5' => 'oooooooooooo'])
$aws->s3_copy('source_bucket', '/source_path', 'source_file.txt', 'target_bucket', '/target_path', 'target_file.txt')
$aws->s3_delete('bucket', '/aws_path', 'aws_file.txt', ['key' => 'xxxxxxxxxxxx', 'md5' => 'oooooooooooo']);
$aws->s3_get('bucket', '/aws_path', 'aws_file.txt', 5, ['key' => 'xxxxxxxxxxxx', 'md5' => 'oooooooooooo']);
bash
# 需自行調整參數
# web_basic -> 專案名稱(執行 composer 的地方)
server {
listen 60000;
root /{web_basic}/src/public/;
index index.html index.htm index.php;
location /NTCH/ {
alias /{web_basic}/vendor/ntch/pocoapoco/src/;
location ~ \.php$ {
fastcgi_pass 127.0.0.1:30001;
fastcgi_index index.php;
ini
[lib]
path = /la