PHP code example of roki / more
1. Go to this page and download the library: Download roki/more 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/ */
roki / more example snippets
use \More\Src\Core\Route\RouteRule;
// 路由到闭包
RouteRule::get('/', function (\More\Src\Core\Http\Request $request, \More\Src\Core\Http\Response $response, \duncan3dc\Laravel\BladeInstance $view) {
$response->redirect('/hello/' . $request->get('name'));
});
// 路由到控制器,支持路由变量,使用:key形式注册路由变量,可在控制器中通过 `request` 获取路由变量参数值
RouteRule::get('/hello/:name', \App\Http\Controller\IndexController::class . '@hello');
RouteRule::get('/view/:name', \App\Http\Controller\IndexController::class . '@view');
RouteRule::get('/db', \App\Http\Controller\IndexController::class . '@db');
RouteRule::get('/container/:id', \App\Http\Controller\IndexController::class . '@container');
namespace App\Http\Controller;
use App\Model\Member;
use \More\Src\Core\Http\Controller;
class IndexController extends Controller
{
// 返回普通字符串
public function hello()
{
$name = $this->request()->get('name');
$this->write("<h1>Hello! {$name}</h1>");
}
// 返回 view 模板页面
public function view()
{
$name = $this->request->get('name');
$this->assign('name', $name);
$this->fetch('index');
}
// 返回 json 格式数据
public function json()
{
// 注意:在 writeJson 方法与 write 方法不要在一次 response 中同时使用,否则会破坏数据的 json 格式
$this->writeJson([
'msg' => '获取json成功',
'code' => 2,
'data' => 'json'
], 200);
}
// Model
public function db()
{
$memberModel = new Member();
$data = $memberModel->find(340);
//$data = $memberModel->where('signature', 'LIKE', '%一批%')->first();
var_dump($data->signature);
$this->writeJson([
'msg' => '获取json成功',
'code' => 2,
'data' => $data->signature
], 200);
}
// 依赖注入(只支持对象依赖注入)
public function container(Member $member)
{
$data = $member->find($this->request()->get('id'));
$this->writeJson([
'msg' => '获取json成功',
'code' => 2,
'data' => $data->id
], 200);
}
// 协程
public function coroutine(Member $member, Shop $shop)
{
$data = [];
$userId = $this->request()->get('id');
go(function () use ($member, &$data, $userId) {
$data['member'] = $member->find($userId);
});
go(function () use ($shop, &$data, $userId) {
$data['shop'] = $shop->where('user_id', $userId)->first();
});
$this->writeJson([
'msg' => '获取json成功',
'code' => 2,
'data' => $data
], 200);
}
public function model(Member $memberModel)
{
$memberModel->find(1);
$memberModel->name = '小余';
$memberModel->save();
$data = $memberModel->count('id');
$data2 = $memberModel->with('card', ['*'], function ($row, Builder $card) {
$card->where('title', $row['name']);
})->get();
$this->writeJson([
'msg' => '获取json成功',
'code' => 2,
'data' => $data,
'data2' => $data2
], 200);
}
public function join(Member $memberModel)
{
/*$memberModel->where('t_user.id', 2)->leftJoin('card', 't_user.id', '=', 't_card.user_id')
->first();*/
$memberModel->where('t_user.id', 1)->leftJoin('card', function (Builder $builder) {
$builder->on('t_user.id', '=', 't_card.user_id')
->where('t_card.id', 1);
})->first();
$this->writeJson([
'msg' => '获取json成功',
'code' => 2,
'data' => $memberModel->title
], 200);
}
public function redis()
{
$cache = $this->app->cache;
$result = $cache->set('name', 'Weekii');
$this->writeJson([
'msg' => '获取json成功',
'code' => 2,
'data' => [
'result' => $result,
'cache' => $cache->get('name')
]
], 200);
}
}
public function model(Member $memberModel)
{
// 获取主键为340的记录
$memberModel->find(340);
// 通过当前数据对象(主键为340的记录)修改数据
$memberModel->signature = '帅的一批的人';
$data = $memberModel->save();
// 通过键值对数组修改数据
$memberModel->where('type', 1)->update(['signature' => '帅的一批的人']);
// 通过当前数据对象添加数据
$memberModel = new Member();
$memberModel->signature = '帅的一批的人';
$memberModel->add();
// 通过键值对数组添加数据
$memberModel->insert(['signature' => '帅的一批的人']);
}