CodeIgniter3-同时支持API和控制器中的Web请求? [英] CodeIgniter3 - supporting both API & Web requests in Controllers?

查看:60
本文介绍了CodeIgniter3-同时支持API和控制器中的Web请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用CodeIgniter3应用程序,并且已经为标准的类似博客的系统奠定了基础。

I'm currently working with a CodeIgniter3 application, and have built the basis for a standard blog-like system.

结构是标准CI-用户要求页面,页面加载控制器方法,控制器方法从其模型中调用任何相关的数据库函数,然后加载视图。

The structure is standard CI - User requests a page, page loads controller method, controller method calls any relevant DB functions from its model, and then a view is loaded.

但是,我也希望这样做可通过API访问的视图。因此,我不是将数据填充到$ data数组中以填充为HTML,而是将其传递到另一个视图,该视图将输出JSON结果。

However, I'm looking to also make this view accessible via an API. So, instead of the $data array being filled with information to populate into HTML, I would instead pass it to a different view which would output a JSON result.

我想编写两个不同的控制器将是一个不好的步骤-是否可以做任何路由选择,还是任何允许控制器识别出api端点被击中的标准实践(例如访问路径中的目录 api) ),然后基于此加载其他视图?

I imagine writing two different controllers would be a bad step to take - is there any routing I can do, or any standard practice to allow for the controllers to recognise an api endpoint has been hit (such as the directory 'api' being in the access path), and then load up a different view based on this?

推荐答案

对于API,我不会使用视图。传统上,视图是用于HTML的。我建议不要像这样将$ data传递给视图,而只需在控制器的末尾将其回显即可。

For an API, I wouldn't use a view. Views are traditionally for HTML. I'd suggest instead of passing $data to a view, simply echo it out at the end of the controller like so.

echo json_encode($data);

我创建了这个包装器,以灵活地扩展返回数据的方式。这是一个基本示例。

I've created this wrapper to make extending how I return data flexible. Here's a basic example.

function api_response($data = array()) {
    $data['error'] = false;
    function filter(&$value) {
        if (is_string($value)) {
            $value = htmlspecialchars($value, ENT_QUOTES, 'UTF-8');
            $value = nl2br($value);
        }
    }
    array_walk_recursive($data, "filter");
    return json_encode($data);
}

我使用类似这样的API错误

And I use something like this for API errors

function api_error_response($error_code, $error_message) {
    log_message('error', $error_code . ' - ' . $error_message);
    $data['error'] = true;
    $data['error_code'] = $error_code;
    $data['error_message'] = $error_message;
    return json_encode($data);
}

然后在控制器的末端这样称呼它。

And then I call it like so at the end of a controller.

echo api_response($data);

此外,该API可以使用与Web GUI相同的控制器方法,仅复制路由,并且在控制器方法中使用类似以下内容。

Additionally, to be able to use the same controller methods for the API as you do for the web GUI, only duplicate the routes, and in the controller methods use something like this.

// Return here for API
if (strpos($_SERVER['REQUEST_URI'], '/api/') !== false) {
    // Unset any data you don't want through the API
    unset($data['user']);
    echo api_response($data);
    return false;
}
// Else, load views here

这篇关于CodeIgniter3-同时支持API和控制器中的Web请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆