使用示例¶

文件目录¶

  1. ├── Controller
  2. ├── Index
  3. └── IndexController.php
  4. └── Test
  5. └── ExceptionController.php
  6. ├── Demo
  7. ├── Service
  8. └── HttpCall.php
  9. └── View
  10. └── test
  11. └── test.html
  12. └── Model
  13. └── Index
  14. └── GetAllDemoData.php

IndexController.php¶

  1. <?php
  2.  
  3. namespace Com\Youzan\ZanHttpDemo\Controller\Index;
  4.  
  5. use Com\Youzan\ZanHttpDemo\Demo\Service\HttpCall;
  6. use Zan\Framework\Foundation\Domain\HttpController as Controller;
  7. use Com\Youzan\ZanHttpDemo\Model\Index\GetAllDemoData;
  8.  
  9. class IndexController extends Controller {
  10.  
  11. //字符串输出示例
  12. public function index()
  13. {
  14. $response = $this->output('success');
  15. //设置响应信息头部
  16. $response->withHeaders(['Content-Type' => 'text/javascript']);
  17. yield $response;
  18. }
  19.  
  20. //json输出示例
  21. public function json()
  22. {
  23. yield $this->r(0, 'json string', ["Hello" => "World"]);
  24. }
  25.  
  26. //模板输出示例
  27. public function showTpl()
  28. {
  29. //给模板中的变量赋值
  30. $this->assign("str", "Zan Framework");
  31. //输出模板页面
  32. yield $this->display("Demo/test/test");
  33. }
  34.  
  35. //操作数据库示例
  36. public function dbOperation()
  37. {
  38. $demo = new GetAllDemoData();
  39. //执行sql语句
  40. yield $demo->doSql();
  41. }
  42.  
  43. //Http服务调用示例
  44. public function rpcRemoteService()
  45. {
  46. $http = new HttpCall();
  47. yield $http->visit();
  48. }
  49. }

ExceptionController¶

  1. <?php
  2.  
  3. namespace Com\Youzan\ZanHttpDemo\Controller\Test;
  4.  
  5. use Zan\Framework\Foundation\Domain\HttpController as Controller;
  6. use Zan\Framework\Network\Http\Exception\InvalidRouteException;
  7. use Zan\Framework\Network\Http\Exception\PageNotFoundException;
  8. use Zan\Framework\Network\Http\Exception\RedirectException;
  9.  
  10. class ExceptionController extends Controller
  11. {
  12. public function testInvalidRoute()
  13. {
  14. throw new InvalidRouteException();
  15. }
  16.  
  17. public function testPageNotFound()
  18. {
  19. throw new PageNotFoundException('This Page is not Found');
  20. }
  21.  
  22. public function testRedirect()
  23. {
  24. throw new RedirectException('https://youzan.com', 'Test Redirect');
  25. }
  26. }

HttpCall.php¶

  1. <?php
  2.  
  3. namespace Com\Youzan\ZanHttpDemo\Demo\Service;
  4.  
  5. use Zan\Framework\Network\Common\Client;
  6.  
  7. class HttpCall {
  8.  
  9. public function visit()
  10. {
  11. $option = [
  12. 'order_no' => 1,
  13. 'kdt_id' => 1,
  14. 'format_order' => false,
  15. 'with_items' => false,
  16. 'with_peerpay' => false,
  17. 'with_source' => false
  18. ];
  19.  
  20. //trade.order.detail.byOrderNo对应包zan-config/zan/Apiconfig.php中的配置,新增url时需要修改配置
  21. yield Client::call('trade.order.detail.byOrderNo', $option);
  22. }
  23. }

Test.html¶

  1. <?php
  2. echo "Hello World $str"; //$str在调用时使用assgin方法赋值
  3. ?>

GetAllDemoData.php¶

  1. <?php
  2.  
  3. namespace Com\Youzan\ZanHttpDemo\Model\Index;
  4.  
  5. use Zan\Framework\Store\Facade\Db;
  6.  
  7. class GetAllDemoData {
  8. public function doSql()
  9. {
  10. $data = [
  11. 'limit' => 2
  12. ];
  13. //demo.demo_sql_id1_1对应resource/sql/demo.php中的配置
  14. $result = (yield Db::execute("demo.demo_sql_id1_1", $data));
  15. var_dump($result);
  16. }
  17. }

原文: http://zanphpdoc.zanphp.io/MVC/use_case.html