Sign In


"Alien Mind" by Paul Iannotti, Kevin Hendrickson and John Morgan




generate restful scaffolding with code stylist for win/mac/lin

Posted February 25th, 2007 by brian

Code Stylist is a graphical development utility which can generate model, view and controller code for Web-database applications. The program is designed to work with the dbscript open source framework, which combines REST-style Web services with content-negotiation. This tutorial will walk you through generating an edit form for records in an SQL database.

step 1

to install code stylist on your workstation, browse to codestylist.com and choose your desktop platform. installation is quick and painless, and it can be completely uninstalled just by deleting the application folder. (windows versions include an uninstaller)

step 2

the codestylist.php file must be placed in your Web server folder. to copy the file, launch the code stylist application and choose the menu item: File > Copy to... pick a destination folder to copy the file to.

in the same folder where codestylist.php is installed, create a folder named site.

screenshot

on linux & mac systems you must set the permissions on the site folder to allow write-access.

screenshot

step 3

click the Setup button in code stylist.

set the Base URL to match the location of your codestylist.php file.

punch in your database settings and click the Verify Setup button to test your configuration.

screenshot

step 4

if you've gotten this far, you're ready to generate some application code. at the bottom left of the code stylist window, click the + button to add a new page.

choose a database table to begin the process.

screenshot

step 5

the mvc generation templates are written in php and can be customized to suit your taste, you will find a range of built-in templates to experiment with.

some templates, such as index.html, can render an unlimited number of database fields, while others such as image.jpg are limited to just one or a few fields.

the file extensions of the templates are designed to tie into the content-negotiation features of the dbscript framework, representations can be chosen automatically by client preference, or explicitly by adding an extension to the url.

screenshot

step 6

check the boxes for the fields to appear on the new page.

drag fields up and down to change their order of appearance.

screenshot

step 7

click on each of the fields to choose what type of form control to use for that field.

screenshot

step 8

choose a name for the new page, in most cases you can accept the default.

screenshot

step 9

now you can see the page that has been generated from the template.

screenshot

and here is the page controller, which will execute before the page is rendered.

screenshot

step 10

click the Publish button to send the generated files to the site folder.

this will forward your browser to the introspection (list of tables) page, from which you can navigate to your generated page.

screenshot

on your desktop you will find a folder named Untitled with all of the generated files. this folder is monitored by code stylist, and any changes you make to the files can be propagated to your site folder by clicking the Publish button in code stylist.

screenshot

updating dbscript

new releases of dbscript are coming out all the time, these instructions will help you update your code stylist installation to use the latest version.

  • download the latest release from http://dbscript.net
  • unzip the archive
  • in your code stylist application folder, find the folder named packages
  • copy the new dbscript files into the packages/dbscript-x.x.x/includes directory
  • in code stylist, choose the menu item: Tools > Erase Site Files
  • click the Setup button, and click Verify Setup to load the updated files




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()






feed
[Valid Atom 1.0]   Valid HTML 4.01 Transitional