首先安装好框架,
easyswoole的默认路由解析规则,则thinkphp很像,我写了一个Demo.php文件
该方法用于向客户响应数据,write方法
$this->response()->write('hello world');
redirect
该方法用于将请求重定向至指定的URL
$this->response()->redirect("https://www.baidu.com");
<?php
/**
* Created by PhpStorm.
* User: zq2020
* Date: 20-2-25
* Time: 下午6:22
*/
namespace App\HttpController\Demo;
use EasySwoole\Http\AbstractInterface\Controller;
class Demo extends Controller{
public function index(){
return $this->response()->write("HellowWorld");
}
public function test(){
return $this->response()->write("this is a test method");
}
public function href(){
return $this->response()->redirect("https://www.baidu.com");
}
}
进入项目根目录
启动项目
php easyswoole start #开发测试
访问方法一
http://192.168.80.129/Demo/Demo/index
访问方法二
http://192.168.80.129/Demo/Demo/test
访问方法三
http://192.168.80.129/Demo/Demo/href
重定向跳转测试
easyswoole框架中
App/HttpController下新建一个Router.php 路由文件
<?php
/**
* Created by PhpStorm.
* User: zq2020
* Date: 20-2-25
* Time: 下午6:27
*/
namespace App\HttpController;
use EasySwoole\Http\AbstractInterface\AbstractRouter;
use FastRoute\RouteCollector;
class Router extends AbstractRouter {
function initialize(RouteCollector $routeCollector){
$routeCollector->get('/demo', '/Demo/Test/index');
}
}
这里的路由配置跟larvel有点类似,实现了restfulApi,对请求方式的限制访问
请求获取数据
$request=$this->request();//获取请求对象
$data = $request->getRequestParam(参数key);//获取参数key
示例:
<?php
/**
* Created by PhpStorm.
* User: zq2020
* Date: 20-2-25
* Time: 下午7:29
*/
namespace App\HttpController\Demo;
use EasySwoole\Http\AbstractInterface\Controller;
class Test extends Controller{
public function index(){
$param=$this->request()->getRequestParam("name");
$data=[];
$data['code']=200;
$data['msg']="SUCCESS";
$data['data']=$param;
$result=json_encode($data);
return $this->response()->write($result);
}
}
访问测试