Blog php tutorial
From n² wiki
Contents |
[edit] Get the example files + framework
Get the example app by checking the files out of svn: svn co http://n2.talis.com/svn/silkworm/trunk/blog-example/
[edit] Templates
Write your templates for each kind of page you need to have (view post, edit post, list posts, list posts for admin). Save them in the /templates directory. As you can see from the examples, I've just used regular PHP, rather than a templating language. If you want to use a templating language, such as Smarty, you can override or edit the Thing::render() method in the controller classes you use (these are found in /lib).
If you are writing HTML templates, give the files .html extensions. The file extensions that your app knows about are defined in /config/mimetypes.php; you can specify the mime-types associated with each file extension.
If you want the pages to be available in alternate formats, such as JSON, or RDF/XML, you can do two things:
- create a template with a different extension, but the same name as your html page, eg:
.json. Do this if your template format has to be specific to a particular type of resource. - create a file with your desired extension, called default. eg:
default.jsonDo this if you can transform your data generically to the format you want. Alternate default templates for JSON and RDF/XML are already in place.
[edit] Forms
Forms are submitted in the style explained here - ie: an associative array based on rdf/xml structure. This means that the POST array from your form can be easily transformed to RDF/XML and saved to your bigfoot store.
[edit] Resource Classes
For each different store you want to use in your application, create a class that extends Thing (for viewing single items), and a class that extends ThingList (for viewing lists of things), set the var $store_uri to the base uri of your store.
You can also override the methods of Thing and ThingList to customise the default behaviour. The most important methods are GET() and POST(), which will be called depending on the HTTP method of the request.
[edit] URL Design
In /config/routes.php, design your urls - mapping the regex to match your intended url space to the class that handles it, the template that will display the page, and any other parameters you want to pass in to the class.
Backreferences in the regex pattern can be used in the values of the array of parameters
if the class extends Thing (for viewing a single resource), you should also pass in a uri parameter, so that Thing can pass that uri to a describe, and pull the right data into your template.
eg:
'/Post/(\S+)' => array( 'class' => 'BlogThing', 'template' => 'blogpost', 'uri' => 'http://example.com/$1#thing', ),
so a GET request for /Post/foo will pass the uri "http://example.com/foo#thing" to the class BlogThing, and call the BlogThing->GET(), which will do a DESCRIBE <http://example.com/foo#thing>, parse the resulting RDF into an array (structured like this JSON format ), and pass that to the blogpost.html template
Note that you do not specify the template's file extension in the parameters array. File extensions are stripped off the requested url before the regex is run, and the application will use the requested extension if available. If no extension is given in the url, html will be used.
If a class extends ThingList, you should pass an 'rdf_type' parameter, which should be the uri of an RDF class type. a ThingList page will list all the resources of that type.

