Initial
This commit is contained in:
61
system/RESTful/BaseResource.php
Normal file
61
system/RESTful/BaseResource.php
Normal file
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of CodeIgniter 4 framework.
|
||||
*
|
||||
* (c) CodeIgniter Foundation <admin@codeigniter.com>
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace CodeIgniter\RESTful;
|
||||
|
||||
use CodeIgniter\Controller;
|
||||
use CodeIgniter\HTTP\RequestInterface;
|
||||
use CodeIgniter\HTTP\ResponseInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
abstract class BaseResource extends Controller
|
||||
{
|
||||
/**
|
||||
* @var string|null The model that holding this resource's data
|
||||
*/
|
||||
protected $modelName;
|
||||
|
||||
/**
|
||||
* @var object|null The model that holding this resource's data
|
||||
*/
|
||||
protected $model;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*/
|
||||
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
|
||||
{
|
||||
parent::initController($request, $response, $logger);
|
||||
$this->setModel($this->modelName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set or change the model this controller is bound to.
|
||||
* Given either the name or the object, determine the other.
|
||||
*
|
||||
* @param object|string|null $which
|
||||
*/
|
||||
public function setModel($which = null)
|
||||
{
|
||||
if ($which) {
|
||||
$this->model = is_object($which) ? $which : null;
|
||||
$this->modelName = is_object($which) ? null : $which;
|
||||
}
|
||||
|
||||
if (empty($this->model) && ! empty($this->modelName) && class_exists($this->modelName)) {
|
||||
$this->model = model($this->modelName);
|
||||
}
|
||||
|
||||
if (! empty($this->model) && empty($this->modelName)) {
|
||||
$this->modelName = get_class($this->model);
|
||||
}
|
||||
}
|
||||
}
|
||||
110
system/RESTful/ResourceController.php
Normal file
110
system/RESTful/ResourceController.php
Normal file
@@ -0,0 +1,110 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of CodeIgniter 4 framework.
|
||||
*
|
||||
* (c) CodeIgniter Foundation <admin@codeigniter.com>
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace CodeIgniter\RESTful;
|
||||
|
||||
use CodeIgniter\API\ResponseTrait;
|
||||
|
||||
/**
|
||||
* An extendable controller to provide a RESTful API for a resource.
|
||||
*/
|
||||
class ResourceController extends BaseResource
|
||||
{
|
||||
use ResponseTrait;
|
||||
|
||||
/**
|
||||
* Return an array of resource objects, themselves in array format
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
return $this->fail(lang('RESTful.notImplemented', ['index']), 501);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the properties of a resource object
|
||||
*
|
||||
* @param mixed $id
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function show($id = null)
|
||||
{
|
||||
return $this->fail(lang('RESTful.notImplemented', ['show']), 501);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a new resource object, with default properties
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function new()
|
||||
{
|
||||
return $this->fail(lang('RESTful.notImplemented', ['new']), 501);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new resource object, from "posted" parameters
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
return $this->fail(lang('RESTful.notImplemented', ['create']), 501);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the editable properties of a resource object
|
||||
*
|
||||
* @param mixed $id
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function edit($id = null)
|
||||
{
|
||||
return $this->fail(lang('RESTful.notImplemented', ['edit']), 501);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add or update a model resource, from "posted" properties
|
||||
*
|
||||
* @param mixed $id
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function update($id = null)
|
||||
{
|
||||
return $this->fail(lang('RESTful.notImplemented', ['update']), 501);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete the designated resource object from the model
|
||||
*
|
||||
* @param mixed $id
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function delete($id = null)
|
||||
{
|
||||
return $this->fail(lang('RESTful.notImplemented', ['delete']), 501);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set/change the expected response representation for returned objects
|
||||
*/
|
||||
public function setFormat(string $format = 'json')
|
||||
{
|
||||
if (in_array($format, ['json', 'xml'], true)) {
|
||||
$this->format = $format;
|
||||
}
|
||||
}
|
||||
}
|
||||
110
system/RESTful/ResourcePresenter.php
Normal file
110
system/RESTful/ResourcePresenter.php
Normal file
@@ -0,0 +1,110 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of CodeIgniter 4 framework.
|
||||
*
|
||||
* (c) CodeIgniter Foundation <admin@codeigniter.com>
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace CodeIgniter\RESTful;
|
||||
|
||||
/**
|
||||
* An extendable controller to help provide a UI for a resource.
|
||||
*/
|
||||
class ResourcePresenter extends BaseResource
|
||||
{
|
||||
/**
|
||||
* Present a view of resource objects
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
return lang('RESTful.notImplemented', ['index']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Present a view to present a specific resource object
|
||||
*
|
||||
* @param mixed $id
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function show($id = null)
|
||||
{
|
||||
return lang('RESTful.notImplemented', ['show']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Present a view to present a new single resource object
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function new()
|
||||
{
|
||||
return lang('RESTful.notImplemented', ['new']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Process the creation/insertion of a new resource object.
|
||||
* This should be a POST.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
return lang('RESTful.notImplemented', ['create']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Present a view to edit the properties of a specific resource object
|
||||
*
|
||||
* @param mixed $id
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function edit($id = null)
|
||||
{
|
||||
return lang('RESTful.notImplemented', ['edit']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Process the updating, full or partial, of a specific resource object.
|
||||
* This should be a POST.
|
||||
*
|
||||
* @param mixed $id
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function update($id = null)
|
||||
{
|
||||
return lang('RESTful.notImplemented', ['update']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Present a view to confirm the deletion of a specific resource object
|
||||
*
|
||||
* @param mixed $id
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function remove($id = null)
|
||||
{
|
||||
return lang('RESTful.notImplemented', ['remove']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Process the deletion of a specific resource object
|
||||
*
|
||||
* @param mixed $id
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function delete($id = null)
|
||||
{
|
||||
return lang('RESTful.notImplemented', ['delete']);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user