├── README.md ├── application ├── Bootstrap.php ├── controllers │ ├── Error.php │ ├── Form.php │ └── Index.php ├── favicon.ico.png ├── library │ └── readme.txt ├── models │ └── Sample.php ├── plugins │ └── Sample.php └── views │ ├── error │ └── error.phtml │ ├── form │ ├── form.phtml │ └── index.phtml │ └── index │ └── index.phtml ├── conf └── application.ini ├── index.php ├── server └── server.php ├── test.txt └── test.txt~ /README.md: -------------------------------------------------------------------------------- 1 | ##**swoole-yaf** 2 | 结合PHP的Yaf框架和Swoole扩展的高性能PHP Web框架 3 | 4 | ##**描述** 5 | 底层使用Swoole内置的swoole_http_server提供服务 6 | 上层应用使用Yaf框架搭建 7 | 8 | 9 | ##**发起人** 10 | Lancelot(李丹阳 ID:会敲代码的喵) from **LinkedDestiny**(**牵机工作室**) 11 | 12 | ##**使用说明** 13 | 打开终端 14 | cd swoole-yaf 15 | php server/server.php 16 | 17 | 打开浏览器,输入http://localhost:9501 18 | 19 | 20 | ##**swoole版本** 21 | swoole-1.7.8+版本 22 | 23 | ##**yaf版本** 24 | 任意stable版本 25 | -------------------------------------------------------------------------------- /application/Bootstrap.php: -------------------------------------------------------------------------------- 1 | getConfig(); 15 | Yaf_Registry::set('config', $arrConfig); 16 | } 17 | 18 | public function _initPlugin(Yaf_Dispatcher $dispatcher) { 19 | //注册一个插件 20 | $objSamplePlugin = new SamplePlugin(); 21 | $dispatcher->registerPlugin($objSamplePlugin); 22 | } 23 | 24 | public function _initRoute(Yaf_Dispatcher $dispatcher) { 25 | //在这里注册自己的路由协议,默认使用简单路由 26 | } 27 | 28 | public function _initView(Yaf_Dispatcher $dispatcher){ 29 | //在这里注册自己的view控制器,例如smarty,firekylin 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /application/controllers/Error.php: -------------------------------------------------------------------------------- 1 | getView()->assign("exception", $exception); 14 | //5. render by Yaf 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /application/controllers/Form.php: -------------------------------------------------------------------------------- 1 | getRequest()->getPost ("fname"); 15 | //var_dump( $post ); 16 | $content['fname'] = $post["fname"]; 17 | $content['lname'] = $post["lname"]; 18 | 19 | //2. fetch model 20 | $model = new SampleModel(); 21 | 22 | //3. assign 23 | $this->getView()->assign("content", $content); 24 | //$this->getView()->assign("name", $name); 25 | $this->display('index'); 26 | //4. render by Yaf, 如果这里返回FALSE, Yaf将不会调用自动视图引擎Render模板 27 | return FALSE; 28 | } 29 | 30 | public function FormAction() { 31 | $get = HttpServer::$get; 32 | $this->getView()->assign("name", $get['name']); 33 | $this->display('form'); 34 | return FALSE; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /application/controllers/Index.php: -------------------------------------------------------------------------------- 1 | getRequest()->getQuery("get", "default value"); 18 | // $get = HttpServer::$get; 19 | 20 | //2. fetch model 21 | $model = new SampleModel(); 22 | 23 | //3. assign 24 | $this->getView()->assign("name", "test"); 25 | 26 | //4. render by Yaf, 如果这里返回FALSE, Yaf将不会调用自动视图引擎Render模板 27 | return TRUE; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /application/favicon.ico.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LinkedDestiny/swoole-yaf/4f1e200f1c095704233cfa9470b31d01198a0ca5/application/favicon.ico.png -------------------------------------------------------------------------------- /application/library/readme.txt: -------------------------------------------------------------------------------- 1 | 项目库文件放在这里 -------------------------------------------------------------------------------- /application/models/Sample.php: -------------------------------------------------------------------------------- 1 | getMessage(); 3 | ?> 4 | -------------------------------------------------------------------------------- /application/views/form/form.phtml: -------------------------------------------------------------------------------- 1 | 2 |
3 |Hello
4 | 9 | 10 |请单击确认按钮
11 | click 12 | 13 | 14 | -------------------------------------------------------------------------------- /conf/application.ini: -------------------------------------------------------------------------------- 1 | [common] 2 | application.directory = APPLICATION_PATH "/application" 3 | application.dispatcher.catchException = TRUE 4 | 5 | [product : common] 6 | -------------------------------------------------------------------------------- /index.php: -------------------------------------------------------------------------------- 1 | bootstrap()->run(); 8 | ?> 9 | -------------------------------------------------------------------------------- /server/server.php: -------------------------------------------------------------------------------- 1 | set( 18 | array( 19 | 'worker_num' => 16, 20 | 'daemonize' => true, 21 | 'max_request' => 10000, 22 | 'dispatch_mode' => 1 23 | ) 24 | ); 25 | 26 | $http->on('WorkerStart' , array( $this , 'onWorkerStart')); 27 | 28 | $http->on('request', function ($request, $response) { 29 | if( isset($request->server) ) { 30 | HttpServer::$server = $request->server; 31 | }else{ 32 | HttpServer::$server = []; 33 | } 34 | if( isset($request->header) ) { 35 | HttpServer::$header = $request->header; 36 | }else{ 37 | HttpServer::$header = []; 38 | } 39 | if( isset($request->get) ) { 40 | HttpServer::$get = $request->get; 41 | }else{ 42 | HttpServer::$get = []; 43 | } 44 | if( isset($request->post) ) { 45 | HttpServer::$post = $request->post; 46 | }else{ 47 | HttpServer::$post = []; 48 | } 49 | 50 | // TODO handle img 51 | 52 | ob_start(); 53 | try { 54 | $yaf_request = new Yaf_Request_Http( 55 | HttpServer::$server['request_uri']); 56 | 57 | $this->application 58 | ->getDispatcher()->dispatch($yaf_request); 59 | 60 | // unset(Yaf_Application::app()); 61 | } catch ( Yaf_Exception $e ) { 62 | var_dump( $e ); 63 | } 64 | 65 | $result = ob_get_contents(); 66 | 67 | ob_end_clean(); 68 | 69 | // add Header 70 | 71 | // add cookies 72 | 73 | // set status 74 | $response->end($result); 75 | }); 76 | 77 | $http->start(); 78 | } 79 | 80 | public function onWorkerStart() { 81 | define('APPLICATION_PATH', dirname(__DIR__)); 82 | $this->application = new Yaf_Application( APPLICATION_PATH . 83 | "/conf/application.ini"); 84 | ob_start(); 85 | $this->application->bootstrap()->run(); 86 | ob_end_clean(); 87 | } 88 | 89 | public static function getInstance() { 90 | if (!self::$instance) { 91 | self::$instance = new HttpServer; 92 | } 93 | return self::$instance; 94 | } 95 | } 96 | 97 | HttpServer::getInstance(); 98 | -------------------------------------------------------------------------------- /test.txt: -------------------------------------------------------------------------------- 1 | sdfsdf 2 | -------------------------------------------------------------------------------- /test.txt~: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LinkedDestiny/swoole-yaf/4f1e200f1c095704233cfa9470b31d01198a0ca5/test.txt~ --------------------------------------------------------------------------------