Resource Jam module for Kohana 3.3
Resources act as a bridge between routes, models and requests., (*1)
The jam-resource
module works with the mighty Jam ORM for Kohana 3.3, (*2)
Main Features
- Define resources in your bootstrap and routes for them would automatically be created
- Generate urls from model objects or collections
- Nest resources
- Access the model object, the model collection or the parent object for the current request
- Supports slugs with the sluggable behavior in Jam ORM
- Restrict routes (and actions) to certain HTTP methods
- Easily build a RESTful API
Defining resources
The simplest way to define a resource:, (*3)
``` php
, (*4)
This would generate seven routes which would serve these purposes:
HTTP Verb |
path |
action |
used for |
GET |
/users |
index |
display a list of all users |
GET |
/users/new |
new |
return an HTML form for creating a new user |
POST |
/users |
create |
create a new user |
GET |
/users/1 |
show |
display a specific user |
GET |
/users/1/edit |
edit |
return an HTML form for editing a user |
PUT |
/users/1 |
update |
update a specific user |
DELETE |
/users/1 |
destroy |
delete a specific user |
As you can see every action has a very specific purpose.
Something you might not be used to in the PHP world.
Everything is derived from [Ruby on Rails routing](http://guides.rubyonrails.org/routing.html).
---
You can easily limit the creation of these default routes or add more:
**Only specific routes**
``` php
array(
'index',
'show'
)
));
```
**Default routes except some**
``` php
array(
'destroy',
'edit',
'update'
)
));
```
**Adding additional routes**
``` php
array(
'picture',
'collection' => array(
'featured'
)
)
));
```
This would make accessible the following URLs (in addition to the default ones):
* /users/1/picture
* /users/featured
Of course you can use these options together to define those routes your application would need.
The routes which a resource would generate are separated in **member** routes and **collection** routes.
The collection routes do not have a specific id while the member routes are about a specific resource.
---
As said above the resources act as a glue between routes, models and controllers.
The model, the controller and the URI paths are derived from the resource name.
The `users` resource would guess the controller is `Controller_Users` and the model is `Model_User`.
You can easily specify these explicitly:
``` php
'pictures',
'model' => 'image'
));
```
This would still create routes to access the photos on `/photos` and `/photos/1`.
But it would use the actions in `Controller_Pictures` and the image model.
Changing the path string is achieved using the `path` option:
``` php
'people'
));
```
This would create routes for URIs like: `/people`, `/people/1` etc. while still using the users controller and user model.
Accessing resources in controllers
----------------------------------
When you visit `/users` the generated routes would open `Controller_Users::action_index()`.
From there you would be able to access a Jam_Collections for the user model with:
`$this->request->resource()->collection()`
You could also access a Jam_Builder with:
`$this->request->resource()->builder()`
---
When you visit `/users/1` the routes would open `Controller_Users::action_show()`.
From there you could access the specified user model with:
`$this->request->resource()->object()`
There is no need to check if it is loaded. If there is no user model with the specified id
`Jam_Exception_Notfound` would be thrown.
Generating URLs
---------------
You could also generate the resourceful URLs for a specific model or a collection.
Use the
``` php
'edit'));
// /users
Resource::url('users', array('action' => 'create'));
?>
Child resources
TODO: explain child resources - defining, usage and application, (*5)
Singular resources
TODO: explain what singular resources are and how they should be used, (*6)
Sluggable
You could use the sluggable
(TRUE
|FALSE
) option and the slug_regex
to set up
the routes to use slugs instead of primary keys., (*7)
TODO: explain sluggable implementation here, (*8)
TODO: explain formats here, (*9)
LICENSE
© Copyright Despark Ltd. 2012, (*10)
License, (*11)