PHP code example of dkkevincheng / vinc
1. Go to this page and download the library: Download dkkevincheng/vinc 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/ */
dkkevincheng / vinc example snippets bash
# 项目目录下新建public/index.php
# 代码如下
// 定义根目录
define("VINC_PATH", __DIR__);
//引入启动文件
/', function () {
echo 'Hello world!';
});
# 重新加载下composer类
composer dump-autoload
# 启动php-server
cd public/
php -S 127.0.0.1:5000
bash
# 新建app文件夹在项目目录,添加controllers、models、views三个文件夹。
# controllers增加两个文件BaseController.php HomeController.php
# BaseController.php设置一些继承的属性
class BaseController
{
public function __construct()
{ }
}
# HomeController.php
class HomeController extends BaseController{
public function index(){
echo "this is Vinc!";
}
}
# 修改Route.php,增加路由
Macaw::get('/', 'HomeController@index');
# 修改composer.json,增加自动加载
"autoload": {
"classmap": [
"app/controllers",
"app/models"
],
#重新加载类
composer dump-autoload
bash
# 新建文件 Data.php,这就是操作数据库的model文件。以后介绍使用ORM操作数据库
# 编辑文件,请查看GitHub内容。
# 修改HomeController.php
class HomeController extends BaseController{
public function index(){
// echo "this is Vinc!";
$db = Data::getIntance();
$sql = "select * from articles limit 0,1";
$list = $db->getAll($sql);
$db->p($list);
}
}
#重新加载类
composer dump-autoload
bash
# 添加view目录下的home.php
foreach ($list as $key => $value) {
echo "<h1>". $value['title'].'</h1>';
echo "<p>". $value['content'].'</p>';
}
# 引入文件到控制器
class HomeController extends BaseController
{
public function index()
{
// echo "this is Vinc!";
$db = Data::getIntance();
$sql = "select * from articles limit 0,1";
$list = $db->getAll($sql);
bash
# 编辑composer.json
" "noahbuscher/macaw": "dev-master",
"illuminate/database": "*",
"filp/whoops": "*"
}
# illuminate/database这个是Laravel的ORM 包。我试用了几个著名的ORM,发现还是 Laravel 的 Eloquent 好用!filp/whoops 是我们的错误提示组件包。也是laravel正在使用的。
# 编辑view/home.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> echo $title
bash
# 编辑BaseController.php
class BaseController
{
public function __construct()
{ }
public function __destruct()
{
$view = $this->view;
if ($view instanceof View) {
extract($view->data);
vinc')->withContent('vinc框架');
}
}
# 编辑models/View.php
class View
{
public $view;
public $data;
public function __construct($view)
{
$this->view = $view;
}
public static function make($viewName = null)
{
if (!$viewName) {
throw new InvalidArgumentException("视图名称不能为空!");
} else {
$viewFilePath = self::getFilePath($viewName);
if (is_file($viewFilePath)) {
return new View($viewFilePath);
} else {
throw new UnexpectedValueException("视图文件不存在!");
}
}
}
public function with($key, $value = null)
{
$this->data[$key] = $value;
return $this;
}
private static function getFilePath($viewName)
{
$filePath = str_replace('.', '/', $viewName);
return VINC_PATH . '/../app/views/' . $filePath . '.php';
}
public function __call($method, $parameters)
{
if (starts_with($method, 'with')) {
return $this->with(snake_case(substr($method, 4)), $parameters[0]);
}
throw new BadMethodCallException("方法 [$method] 不存在!.");
}
}
# 编辑Article.php
class Article extends Illuminate\Database\Eloquent\Model
{
public $timestamps = false;
}
# 刷新类文件
composer dump-autoload
bash
# 修改composer.json 添加
"ahbuscher/macaw": "dev-master",
"illuminate/database": "*",
"filp/whoops": "*",
"nette/mail": "*"
}
# 装载插件
composer update
# 引入插件,增加app/models/Mail.php
# 修改BaseController.php
$mail = $this->mail;
if ($mail instanceof Mail) {
$mailer = new Nette\Mail\SmtpMailer($mail->config);
$mailer->send($mail);
}
# 调用发送邮件,发送邮件需要邮箱开通smtp
$this->mail = Mail::to(['[email protected] ', '[email protected] '])->from('yourname <[email protected] >')->title('this is your good news!')->content('<h1>Hello~~</h1>');
bash
# 修改composer.json 添加
"ahbuscher/macaw": "dev-master",
"illuminate/database": "*",
"filp/whoops": "*",
"nette/mail": "*",
"predis/predis": "*"
}
# 装载插件
composer update
# 引入插件,增加app/models/Redis.php
# 修改HomeController.php
Redis::set('key', 'value', 5, 's');
echo Redis::get('key');
# 运行一次后将上面一行注释掉,不断刷新看'value'是否会在设定的时间结束后从页面上消失。