# Api

# 介绍

  • 类似spring boot、Laravel resource、Restful
  • 提供给前端Ajax请求、axios使用
  • 提供给jQuery、react、vuejs、angularjs、angular使用

# 分类

  • Api工具
  • MVC Api

# Api工具

  • api工具
    • 只需要引入 init.php 文件
    • 开发接近原生,无需使用MVC路由规则
    • 同时可以利用betterlife框架的好处。
    • 去除了MVC路由的复杂性,开发更简洁,代码更易读

# MVC Api

# MVC Api 示例

  • 示例文件: home/betterlife/action/Action_Ajax.php
  • 示例1: 返回字符串
    /**
     * 仅供测试:Ajax请求返回字符串
     */
    public function test()
    {
        return "Hello world!";
    }
    
  • 示例2: 返回json格式字符串,前端多数请求使用这种返回
    /**
     * 仅供测试:Ajax请求返回json格式字符串
     */
    public function index()
    {
        $result = array(
            "id" => 1,
            "ti" => "标题缩写",
            "hi" => "Welcome to ajax!",
            "ha" => "^_^"
        );
        return $result;
    }