1. Go to this page and download the library: Download woann/light-php 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/ */
namespace app\Middleware;
use Lib\Middleware;
class Test implements Middleware{
public function handle($request)
{
//在此处理中间件判断逻辑,
//...
//程序最后通过验证后,返回true;
return true;
}
}
namespace app\Controllers\Index;
use Lib\Controller;
class IndexController extends Controller {
//普通输出
public function index()
{
return 'hello world';
}
//输出json
public function index1()
{
return $this->json(["code" => 200, "msg" => "success"]);
}
//调用模板
public function index2()
{
$a = "test";
//输出/app/resources/views目录下index.blade.php模板,并携带参数$a。支持用 . 拼接模板路径(和laravel中模板引擎部分一样)
return $this->view("index",["a" => $a]);
//也可以直接调用view函数
return view("admin.index",["a" => $a]);
}
}
namespace app\Hook;
use Lib\BaseHook;
use Lib\Log;
class TestHook extends BaseHook {
public function start($name,$ip,$port)
{
//当server启动时执行此钩子
Log::getInstance()->write('INFO',$name,"启动成功","{$ip}:{$port}","at",date('Y-m-d H:i:s'));
}
public function open($server,$fd){
//可以在此执行websocket链接成功后绑定用户id和fd的操作
}
public function close($server,$fd){
//可以在此执行websocket关闭链接后解绑用户id和fd的操作
}
}
namespace app\Controllers\Index;
use Lib\WsController;
class WebSocketController extends WsController {
public function index()
{
//给客户端发送消息
//$this->>fd 客户端唯一标示
//$this->>server websocket server对象(此对象提供的功能参考swoole文档)
//
$data = "哈哈哈我是一条消息";
$data2 = "这是一条通过task任务群发消息";
$this->server->push($this->fd,$data);
//投递异步任务
$this->task->delivery (\app\Task\Notice::class,'ToAll',[$this->fd,$data2]);
}
}
namespace app\Controllers\Index;
use Lib\Controller;
use Lib\DB;
class IndexController extends Controller {
public function index()
{
$res = DB::table('user')->where('id',1)->first();
}
}
namespace app\Models;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
protected $table = 'user';
}
public function index(){
$res = DB::table('user')->where('id',"=","1")->first();
return $this->json($res);
}