六:使用$_SESSION,$_GET,$_POST等全局变量
新增baseController控制器,继承ViewController
<?php /** * Created by PhpStorm. * User: tioncico * Date: 2018/7/19 * Time: 16:21 */ namespace App\\Base; use EasySwoole\\Config; use EasySwoole\\Core\\Http\\AbstractInterface\\Controller; use EasySwoole\\Core\\Http\\Request; use EasySwoole\\Core\\Http\\Response; use EasySwoole\\Core\\Http\\Session\\Session; use think\\Template; class BaseController extends ViewController { use Tool; protected $config; protected $session; public function __construct(string $actionName, Request $request, Response $response) { parent::__construct($actionName, $request, $response); $this->header(); } public function defineVariable() { } protected function onRequest($action): ?bool { return parent::onRequest($action); // TODO: Change the autogenerated stub } public function init($actionName, $request, $response) { $class_name = static::class; $array = explode('\\\', $class_name); $GLOBALS\['base'\]\['MODULE_NAME'\] = $array\[2\]; $GLOBALS\['base'\]\['CONTROLLER_NAME'\] = $array\[3\]; $GLOBALS\['base'\]\['ACTION_NAME'\] = $actionName; $this->session($request, $response)->sessionStart(); $_SESSION\['user'\] = $this->session($request, $response)->get('user');//session $_GET = $request->getQueryParams(); $_POST = $request->getRequestParam(); $_REQUEST = $request->getRequestParam(); $_COOKIE = $request->getCookieParams(); $this->defineVariable(); parent::init($actionName, $request, $response); // TODO: Change the autogenerated stub $this->getView()->assign('session', $_SESSION\['user'\]); } public function header() { $this->response()->withHeader('Content-type', 'text/html;charset=utf-8'); } /** * 首页方法 * @author : evalor <master@evalor.cn> */ public function index() { return false; } function session($request = null, $response = null): Session //重写session方法 { $request == null && $request = $this->request(); $response == null && $response = $this->response(); if ($this->session == null) { $this->session = new Session($request, $response); } return $this->session; } }
在EasySwooleEvent.php afterAction中,进行销毁全局变量
public static function afterAction(Request $request, Response $response): void { unset($GLOBALS); unset($_GET); unset($_POST); unset($_SESSION); unset($_COOKIE); }
七:使用fastRoute自定义路由
1:在App/HttpController下新增文件Router.php
<?php /** * Created by PhpStorm. * User: tioncico * Date: 2018/7/24 * Time: 15:20 */ namespace App\\HttpController; use EasySwoole\\Config; use EasySwoole\\Core\\Http\\Request; use EasySwoole\\Core\\Http\\Response; use FastRoute\\RouteCollector; class Router extends \\EasySwoole\\Core\\Http\\AbstractInterface\\Router { function register(RouteCollector $routeCollector) { $configs = Config::getInstance()->getConf('web.FAST\_ROUTE\_CONFIG');//直接取web配置文件的配置 foreach ($configs as $config){ $routeCollector->addRoute($config\[0\],$config\[1\],$config\[2\]); } } }
web.config配置
<?php /** * Created by PhpStorm. * User: tioncico * Date: 2018/7/24 * Time: 9:03 */ return array( 'FAST\_ROUTE\_CONFIG' => array( array('GET','/test', '/Index/Index/test'),//本人在HttpController目录下分了模块层,所以是Index/Index ), );
访问xx.cn/test 即可重写到/Index/Index/test方法
八:现成源码
本人组装好轮子的源码已经开源,可以直接下载开撸,代码与教程有一点点的不同,有问题可以加qq群633921431提问