Back to ApiController class

Method delete

public void
delete
(mixed $id = null)
Removes an item.
Parameters
  • int $id The primary key to delete item.
Returns
  • void
Since
  • 4.0.0
Class: ApiController
Project: Joomla

Method delete - Source code

/**
 * Removes an item.
 *
 * @param   integer  $id  The primary key to delete item.
 *
 * @return  void
 *
 * @since   4.0.0
 */
public function delete($id = null)
{
    if (!$this->app->getIdentity()->authorise('core.delete', $this->option)) {
        throw new NotAllowed('JLIB_APPLICATION_ERROR_DELETE_NOT_PERMITTED', 403);
    }
    if ($id === null) {
        $id = $this->input->get('id', 0, 'int');
    }
    $modelName = $this->input->get('model', Inflector::singularize($this->contentType));
    /** @var \Joomla\CMS\MVC\Model\AdminModel $model */
    $model = $this->getModel($modelName, '', ['ignore_request' => true]);
    if (!$model) {
        throw new \RuntimeException(Text::_('JLIB_APPLICATION_ERROR_MODEL_CREATE'));
    }
    // Remove the item.
    if (!$model->delete($id)) {
        if ($model->getError() !== false) {
            throw new \RuntimeException($model->getError(), 500);
        }
        throw new \RuntimeException(Text::_('JLIB_APPLICATION_ERROR_DELETE'), 500);
    }
    $this->app->setHeader('status', 204);
}