tp5控制器内调用其他控制器方法并传参

//请求的主要内容
        $info = [
            $this->location['province'],
            $this->location['city'],
            $this->location['district']
        ];

        dump(action('api/agent' , $info));
\\被请求的控制器
<?php

namespace app\h5\controller;
use think\Controller;
class Api extends Controller{
    public function agent($province,$city,$district = ''){
        //dump(input('province'));
        /*$province = input('province');
        $city = input('city');
        $district = input('district');
        $lat = input('lat');
        $lng = input('lng');*/
        $where = "(province='".$province ."' and city='" . $city . "' and type = 2) or (province='" . $province . "' and city='" . $city . "' and area='" .$district . "' and type=3)";
        $res = db('agent')->where($where)->select();
//        return json($res);
        return $res;
    }
//
}

需要注意的是,请求的地方一定要传一维的参数,并且在被请求的控制器里面做绑定,绝对不能传二维参数

还看到另一种调用的方法(没有测试过,不知道能不能用):

//1.另一个控制器PayController在数据库中没有表  

  public function Confirm_order()
    {
        $a=new PayController();
        $b=$a->balance(1);
        echo $b;

        die;

  }

手册地址:
http://www.kancloud.cn/manual/thinkphp5/144731
看手册,有个助手函数action(用法和R,A类似)。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容