Actually i do not complete the work but i got some working code.
I will post here some hints and if you will be still interested i will
upload what i actually have somewhere and why not.. you can finish the
work on your own ;)
Let's start from glue itself. How i manage to call HTTP method with
every single parameter instead of the single $matches preg_match
array_shift($matches);$_args = array();foreach(array_keys($matches) as
$k => $v){ if(gettype($v) === gettype((string)" ")) { $_args[$v] =
$matches[$v][0]; }}$obj->after();call_user_func_array(array($obj,
$method), $_args);
which is slighty usless as long as you use pattern like "/admin/role/(?
P<rolename>[a-z0-9]+)"
anyway...
$obj->after.. it's actually a typo, it should be "before" and after
the call_user_func_array you can call $obj->after. This mean that
EVERY mapped class in glue must(*should*!) have methods after() and
before(). They will be called after matching a URI (a resource) and
after the response (like a hook in other famous frameworks).
That's why i choose to have a common controller to extend:empty after
and before method (to overwrite) and with and embedded template
engine: public function Controller() { $this->tpl = new RainTPL();
$this->tpl->configure('tpl_dir', __DIR__.'/../view/'); $this->tpl-
>configure('cache_dir', __DIR__.'/../cache/'); $this->tpl-
>configure('base_url', 'baseurl'); $this->tpl-
>configure('check_template_update', true);
ALSO in a mapped class (extending the base controller) you can do
something like (using RainTPL):class MyMappedClass { public function
GET() {
$this->tpl->assign('template', 'myTemplate'); }}
ok here you got: glue like a router, RainTPL as V, custom controller
as C let's move to the model:i decided to use doctrine dbal and a
custom basic CRUD class.
Here some of the methods to understand the mood of the workclass
Model ..... public function create($data) { $s = $this->_create();
$s = $this->_bind($s, $data); try { return $s->execute(); } catch
(Exception $e) { header("HTTP/1.1 500 Internal Server Error");
echo json_encode(array("message" => $e->getMessage())); die(); } }
protected function _bind($s, $data, $id = null) { foreach($data as
$field => $value) { $this->_columnCheck($field, $id); $s-
>bindValue(':' . $field, $value); } return $s; } protected function
_columnCheck($column, $id = null) { if(!in_array($column, $this-
>field)) if(!is_null($id)) throw new Exception("Column $field
does not exists in table {$this->tablename}."); } private function
_create() { $sql = 'INSERT INTO ' . $this->tablename . '(';
foreach($this->field as $field) { $this->_columnCheck($field);
$sql .= $this->connection->quoteIdentifier($field) . ', '; } $sql =
substr($sql, 0, -2); $sql .= ') VALUES('; $_count = count($this-
>field); foreach($this->field as $field) $sql .= ':' . $field . ',
'; $sql = substr($sql, 0, -2) . ')'; return $this->connection-
>prepare($sql); }
So finally a ORM for a simple table would look like:
class UserModel extends Model{ protected $tablename = 'user';
protected $field = array('name', 'role');}
and here how a controller (using that model) POST method
public function POST() { $u = new UserModel(); $result = $u-
>create(array( "name" => $_POST['username'], "role" =>
$_POST['userrole'] )); $message = $result ? "Utente
{$_POST['username']} ({$_POST['userrole']}) aggiunto con successo." :
"Errore durante l'aggiunta dell'utente ${$_POST['username']}
({$_POST['userrole']})."; echo json_encode(array("message" =>
$message)); }
$message is italian like my unluky country ;)!
Well if you are still interested in this i will put the code somewhere
maybe with a deeper how to =D