Additional actions in Zend Rest Controller besides the default actions
I know the REST API can be implemented with Zend_Rest_Controller and has 5 abstract methods indexAction, getAction, postAction, putAction, deleteAction to do return, create, update, etc.
My question is, can I have more API fictions with a controller besides these default functions to perform various operations?
For example:
indexAction - returns a list of available books,
searchAction - Returns a list of books based on search criteria. (I know it can be done in indexAction with some parameters, but then the code will look more complicated, I need to avoid it)
source to share
Yes, you can create custom action methods in your controller. Although you are extending the abstract Zend_Rest_Controller class, as long as you define these 5 abstract methods (the ones you already mentioned), you can customize the rest of the classes.
The only similar method you can learn is getAction()
. This expects a parameter named ID and will retrieve the record based on the primary key.
You probably need to define your routing in a config file:
routes.archive.route = "search/:keyword"
routes.archive.defaults.controller = books
routes.archive.defaults.action = search
routes.archive.defaults.year = "Hamlet"
routes.archive.reqs.year = "\s+"
And then you need to pass these configuration parameters to your router:
$config = new Zend_Config_Ini('/path/to/config.ini', 'production');
$router = new Zend_Controller_Router_Rewrite();
$router->addConfig($config, 'routes');
Read the Zend documentation for a more in-depth look.
source to share