API: Controllers


1Introduction

As seen in the API overview page, we're going to create two controllers, one for returning the tag list, the other offering several note-related actions.

As seen in the API configuration, we're using the API plugin, which handles API access authentication (with public/private key pairs). After authentication, this plugin provides the template variables currentUser and currentUserId, which can be used in controllers.

In conjunction with the plugin, we'll use the Auth attribute to ensure that all controller accesses are correctly authenticated.


2Tag controller

This controller is quite simple, offering just one action.

<?php

use \Temma\Atttributes\Auth as TµAuth;

/** Tag controlleur. */
#[TµAuth]
class Tag extends \Temma\Web\Controller {
    /** Action that returns the list of tags for the current user. */
    public function list() {
        $this['json'] = $this->_loader->NoteDao->getTags($this['currentUserId']);
    }
}
  • Line 3: Definition of a shortcut for the Auth attribute.
  • Line 6: Use of the Auth attribute, so that this controller can only be used with correct authentication.
  • Line 7: Definition of the Tag controller.
  • Line 9: Definition of the list action.
  • Line 10: Retrieve tags linked to the current user's notes. These tags are passed to the JSON view.

3Note controller

This controller contains all actions relating to note management.

<?php

use \Temma\Attributes\Auth as TµAuth;

/** Note controlleur. */
#[TµAuth]
class Note extends \Temma\Web\Controller {
    /** Returns the list of notes for the current user. */
    public function list() {
        $this['json'] = $this->_loader->NoteDao->getNotes($this['currentUserId']);
    }
    /** Returns the result of a search based on criteria. */
    public function search() {
        $tag = $_GET['tag'] ?? null;
        $title = $_GET['title'] ?? null;
        $this['json'] = $this->_loader->NoteDao->searchNotes($this['currentUserId'],
                                                             $tag, $title);
    }
    /**
     * Returns note data.
     * @param   int    $id   Note identifier.
     */
    public function get(int $id) {
        $note = $this->_loader->NoteDao->getNote($id);
        if ($note['userId'] != $this['currentUserId']) {
            return $this->_httpError(403);
        }
        $this['json'] = $note;
    }
    /** Add a new note. */
    public function add() {
        // parameters retrieval
        $title = $_GET['title'] ?? null;
        $content = $_GET['content'] ?? null;
        $tag = $_GET['tag'] ?? null;
        if (isset($tag) && !is_array($tag))
            $tag = [$tag];
        // tags standardization
        $tags = array_map(function($t) {
            return \Temma\Utils\Text::urlize($t);
        }, $tags);
        // add note
        $this['json'] = $this->_loader->NoteDao->create($this['currentUserId'],
                                                        $title, $content, $tag);
    }
    /**
     * Updates a note.
     * @param   int   $noteId   Note identifier.
     */
    public function update(int $noteId) {
        // note retrieval
        $note = $this->_loader->NoteDao->getNote($id);
        // authorization check
        if ($note['userId'] != $this['currentUserId']) {
            return $this->_httpError(403);
        }
        // parameters retrieval
        $title = $_GET['title'] ?? null;
        $content = $_GET['content'] ?? null;
        $tag = $_GET['tag'] ?? null;
        if (isset($tag) && !is_array($tag))
            $tag = [$tag];
        // note update
        $this->_loader->NoteDao->update($noteId, $title, $content, $tag);
        $this['json'] = true;
    }
    /**
     * Delete a note.
     * @param   int   $noteId   Note identifier.
     */
    public function remove(int $noteId) {
        // note retrieval
        $note = $this->_loader->NoteDao->getNote($id);
        // authorization check
        if ($note['userId'] != $this['currentUserId']) {
            return $this->_httpError(403);
        }
        // note deletion
        $this->_loader->NoteDao->remove($noteId);
        $this['json'] = true;
    }
}