How i use glue

143 views
Skip to first unread message

plume

unread,
Sep 22, 2011, 4:24:33 AM9/22/11
to GluePHP
first of all nice work Joe Topjian!

It was so easy to me to glue 3 diffrent PHP product with glue!

Im not gonna go deep into the code, just say how i like to use it (and
like to share it).

As long as glue is mostly like a "router" (IMO) i find useful to
replace glue throw error with the sending back correct HTTP header:

* method not exists -> HTTP 405 Method Not Allowed
* class not found -> HTTP 500 Internal Server Error
* !found -> HTTP 404 Not Found

Like a post i saw in this group (like but not exactly) i switch from
calling the method $class->method($matches) with a
call_user_func_array, that's why i feel more confortable to declare
function like GET($a, $b, $c) instead of GET($matches) and switch
inside.

I also find useful that every class that glue will call to fulfill a
request extends a base class. The base class has a template engine
inside and the regex match by glue. Also an empty "after" method which
glue call before call the HTTP method request.

This way you can easily achieve a quite cute MVC within the component
you prefer! and guess what glue is born for!

One last, for those who (madly) care about the M of the MVC i
implemented i simply write to class. One is a Bridge to put inside a
famous DBAL, and one base class Model (extend the bridge) with common
CRUD method. It's something that is an ORM but it's not; and it's an
active record but it's not. A model so will look like an empty class
defining tablename and table column (and other method if you need it).

So, thanks GluePHP (joe!) it inspired me a lot! And i am really
enjoying it

php<3

unread,
Nov 7, 2011, 10:04:38 AM11/7/11
to GluePHP
this is incredibly interesting. if you have more examples, can you
please share them?

plume

unread,
Nov 8, 2011, 2:02:38 PM11/8/11
to GluePHP
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

plume

unread,
Nov 8, 2011, 2:03:58 PM11/8/11
to GluePHP
WTF happend to the code xD lol im terrible sorry for you all!!

i will repost later.. really really sorry!

meem

unread,
Nov 21, 2011, 2:32:53 AM11/21/11
to glu...@googlegroups.com
I'm ussing a similar approach but mapping the controllers to a file
can you explain a little more about the method you use to get your $a, $b, $c vars on the GET event?

btw, you can use pastie.org and only share the link, that way your code will be clear and with syntax color
Reply all
Reply to author
Forward
0 new messages