COOLPHP使用到的第三方包如下:
"filp/whoops": #优雅的错误展示
"catfan/medoo": #数据库操作框架
"smarty/smarty": #模板引擎
1. Whoops是一款现代的PHP组件,为PHP错误和异常提供了设计精美且易于阅读的诊断页面,提高查找问题Bug的效率。
GitHub地址,后期可自行升级新版本 https://github.com/filp/whoops
在index.php框架入口文件注册使用Whoops
// 注册使用Whoops提供的优美错误展示界面 $whoops = new Run; $whoops->pushHandler(new PrettyPageHandler); $whoops->register();
2. Medoo用于加速开发的轻量 PHP 数据库框架,强大支持各种常见的SQL查询,防止SQL注入,支持 MySQL、MSSQL、SQLite、MariaDB、PostgreSQL、Sybase、Oracle 等等。
GitHub地址,后期可自行升级新版本 https://github.com/catfan/Medoo
namespace core\app\common\model;
use core\plugin\Code;
use core\plugin\Model;
use core\plugin\RESTful;
class User extends Model
{
// 表名
protected string $table = 'user';
/**
* 注册
* @param array $data
* @return array
* @author 2daye
*/
public function register(array $data): array
{
$count = $this->database->count($this->table, ['phone' => $data['phone']]);
// 如果用户存在就拒绝注册
if ($count > 0) {
return (new RESTful())->response(Code::FORBIDDEN, '手机号已经存在')->array();
}
$result = $this->database->insert($this->table, $data);
// 判断用户是否新增成功
if ($result->rowCount() > 0) {
return (new RESTful())->response(Code::OK)->array();
}
return (new RESTful())->response(Code::INTERNAL_SERVER_ERROR)->array();
}
}
3. Smarty是一个PHP开发的模板引擎,虽然现在的市场份额降低很多,但也是业界非常著名的PHP模板引擎之一。
GitHub地址,后期可自行升级新版本 https://github.com/smarty-php/smarty
框架我编写了一个Template.php类,用来重写载入Smarty
1. 重写Smarty模板数据传输符号,避免使用时和JavaScript的{}符号冲突。
2. 重写Smarty模板文件夹和缓存文件夹。
在框架Controller父类中初始化,Smarty模板引擎的assign方法和display方法,也可以自定义特殊初始化参数
/**
* 调用模板类assign方法/传递数据到模板
* @param $parameter
* @param $value
* @return void
* @author 2daye
*/
protected function assign($parameter, $value): void
{
$template = Template::getInstance();
$template->assign($parameter, $value);
}
/**
* 调用模板类的display方法/渲染html模板
* @param string $html 要渲染的html模板
* @return void
* @throws \SmartyException
* @author 2daye
*/
protected function display(string $html = ''): void
{
$request = Request::getInstance();
if ($html === '') {
$html = $request->module . '/view/' . strtolower($request->controller) . '/' . strtolower($request->methods) . '.html';
}
$template = Template::getInstance();
$template->display($html);
}