2

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

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.

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.

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?

3
  • 1
    if you want a clean approach you should consider using a rest library for that github.com/chriskacerguis/codeigniter-restserver, however it doesn't change the fact that you need 2 different controllers anyway Commented Mar 23, 2018 at 12:11
  • Fair enough, so if we consider I set up a subdirectory for say, controllers/api/ and insert a Users controller in there, is there any way for me to access the methods under controllers/Users.php ? Commented Mar 23, 2018 at 12:17
  • whats the purplose behind of that ? if you've so many redundant code show an example of that - maybe its a problem because you have code in your controller which belongs to a model - i'm not sure ;) Commented Mar 23, 2018 at 12:36

1 Answer 1

3

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);
}

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);

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.