Sign In


Home | Posts | Entry 5

rest web services plus content-negotiation equals dbscript

Posted February 22nd, 2007 by brian

dbscript is a new framework for PHP, it features REST-style web services, content-negotiation and a Rails-style template arrangement.

step 1

visit dbscript.net to download the latest version of dbscript, unzip it into a folder on your Web server, you'll find these files:
screenshot

step 2

dbscript comes with a few data models in db/models, these can be deleted but for the Entry model, which enables metadata (eTag/last_modified) about a record in any table.

you'll notice that data model names are always singular, and tables and controllers are plural. the Entry model corresponds with a table named entries, the Person model goes with the people table, etc.

here is a simple data model for the posts table, it should be saved as db/models/Post.php

the framework will perform the CREATE TABLE from the data model information

screenshot

step 3

the following controller for the posts resource includes get, post and _index functions. the _index function is triggered before the partial template _index.xhtml is loaded, and it passes a single named variable, $collection, to the template.

we could have a index function as well, which could pass variables to the layout template index.xhtml. templates and controller functions that begin with an underscore are "partials" which means they are intended to be rendered inside a layout template.

save this file as db/controllers/posts.php

screenshot

step 4

this layout gets its doctype from dbscript's included templates by calling render_partial, which will look for a template named _doctype.xhtml -- if the template is not found in /views/posts then the template in /views can be used.

content_for_layout is a sort of alias for render_partial, it will render a partial for the current $request->action -- so a request to http://host/?posts/new would render the partial _new.xhtml in this space. otherwise the _index.xhtml partial would be rendered.

save this as views/posts/index.xhtml

screenshot

step 5

this is the partial template, which loops over each Post in the collection. save it as views/posts/_index.xhtml

screenshot

step 6

an input form for creating new posts, save it as views/posts/_new.xhtml

the URL for this form would be http://host/?posts/new

screenshot

step 7

to enter your database settings, open the file db/config.yml -- currently only postgresql and mysql are supported

screenshot

step 8

now you can browse to http://host/?posts/new to submit a new post, BUT first you'll have to change the data model from let_read to let_access to allow new records to be created

idb

dbscript includes an interactive shell called idb, which is adapted from Jan Kneschke's PHP Shell, here are a few examples:

screenshot
screenshot
screenshot
screenshot

if save() produces errors, use post->set_value( field, value ) and post->save_changes()

Contributor:

Brian Hendrickson

Created:

Mon Sep 17 02:51 AM





* * * * * Wed Sep 19 03:37 PM
* * * * * Wed Sep 19 03:37 PM





Sign In to post comments







Bookmark this page:







feed
[Valid Atom 1.0]   Valid HTML 4.01 Transitional