JSON API example

Let's create a simple JSON API to update our user "type", that can be ADMIN or STAFF or CUSTOMER.
The API URL will be:
POST /api/users/update-type

Model & Repository definition

<?xml version="1.0" encoding="UTF-8"?>
<context namespace="myproject.model">
	<objects>
        <object id="myproject.model.UsersRepository" class="metadigit\core\db\orm\Repository">
            <constructor>
                <arg name="class">myproject\model\User</arg>
            </constructor>
        </object>
	</objects>
</context>
<?php
namespace myproject\model;
/**
 * @orm(source="users")
 */
class User {
	use \metadigit\core\db\orm\EntityTrait;

	/** @orm(type="integer", primarykey, autoincrement) */
	protected $id;
	/** @orm
   * @validate(regex="/^(ADMIN|STAFF|CUSTOMER)$/") */
	protected $type;
	/** @orm */
	protected $name;
}
CREATE TABLE users (
	id		INT UNSIGNED NOT NULL AUTOINCREMENT,
  type	ENUM('ADMIN', 'STAFF', 'CUSTOMER') NOT NULL,
  name	VARCHAR(20),
  PRIMARY KEY (id)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;

APP definition

Define APP "API" inside metadigit-core.ini
The APP is called "API", and is mapped on

  • port: 80
  • URL: /api/
  • namespace: myproject.apps.api
[apps-http]
API = "80|/api/|myproject.apps.api"

Define APP context, with Dispatcher and Controller.
The Context namespace is: myproject.apps.api
We need to include the namespace "myproject.model" to inject the UsersRepository inside the Controller.

<?xml version="1.0" encoding="UTF-8"?>
<context namespace="myproject.apps.api">
	<includes>
		<include namespace="myproject.model"/>
	</includes>
	<objects>
		<!-- Dispatcher -->
		<object id="myproject.apps.api.Dispatcher" class="metadigit\core\web\Dispatcher">
			<properties>
				<property name="defaultViewEngine">json</property>
				<property name="routes" type="array">
					<item key="/users/*">myproject.apps.api.UsersController</item>
				</property>
			</properties>
		</object>
		<!-- controllers -->
		<object id="myproject.apps.api.UsersController" class="myproject\apps\api\UsersController">
			<properties>
				<property name="UsersRepository" type="object">myproject.model.UsersRepository</property>
			</properties>
		</object>
	</objects>
</context>

Controller

The Controller has been injected with UsersRepository, and can use his ORM API to perform data modification on DB.

<?php
namespace myproject\apps\api;
use metadigit\core\http\Request,
	metadigit\core\http\Response;

class UsersController extends \metadigit\core\web\controller\ActionController {
	use \metadigit\core\db\PdoTrait;

	/** UsersRepository
	 * @var \metadigit\core\db\orm\Repository */
	protected $UsersRepository;

	/**
	 * @routing(method="POST", pattern="users/update-type")
	 * @param Request $Req
	 * @param Response $Res
	 * @param integer $id
	 * @param string $type
	 */
	function updateTypeAction(Request $Req, Response $Res, $id, $type) {
		$response['success'] = false;
		try {
			if($this->UsersRepository->update($id, ['type'=>$type], false, null))
				$response['success'] = true;
		} catch(\Exception $Ex) {
			$response['errorMsg'] = 'System Exception';
		}
		$Res->set($response)->setView('json:');
	}
}