javascript - Triggering different actions on same route based on request type in Zend Framework 2 -
i trying make zf2 respond in rest way different request type.
in module.config.php have router config.
'router' => array( 'routes' => array( 'student' => array( 'type' => 'segment', 'options' => array( 'route' => '/student[/:action][/:id]', 'constraints' => array( 'action' => '[a-za-z][a-za-z0-9_-]*', 'id' => '[0-9]+', ), 'defaults' => array( 'controller' => 'student\controller\student', 'action' => 'index', ), ), ), ), ),
on frontend using backbone send get, post, delete requests server based on user interactions. when user trigger action delete student id of n, backbone send /somepath/student/n using delete request. when user trigger action student id of n, backbone send /somepath/student/n using request.
if want current setup work have change backbone request , change url student/n student/delete/n if want delete student id , get.
this did on client side avoid.
define(['backbone'], function(backbone){ return backbone.model.extend({ defaults:{ //set default model values }, initialize: function(){ //initialize }, methodtourl: { 'delete': '/student/delete' }, sync: function(method, model, options) { options = options || {}; var id = arguments[1]['id'] options.url = model.methodtourl[method.tolowercase()] + '/' + id; return backbone.sync.apply(this, arguments); } }); });
in controller on server side have different action methods run different request types.
public function deleteaction() { //some code } public function getaction() { //some code }
i don't want change default backbone behaviour (intercepting , altering requests).
is there way configure zf2 router use same route trigger different actions based on request method type?
you can use method
route child route of segment route. http://framework.zend.com/manual/2.3/en/modules/zend.mvc.routing.html
there example named a complex example child routes
author blog
literal route create child routes each of different type.
simply can create child routes in student route type method
each method want use , change action method types.
'router' => array( 'routes' => array( 'student' => array( 'type' => 'segment', 'options' => array( 'route' => '/student[/:action][/:id]', 'constraints' => array( 'action' => '[a-za-z][a-za-z0-9_-]*', 'id' => '[0-9]+', ), 'defaults' => array( 'controller' => 'student\controller\student', 'action' => 'index', ), ), 'may_terminate' => true, 'child_routes' => array( 'delete' => array( 'type' => 'method', 'options' => array( 'verb' => 'delete', 'defaults' => array( 'action' => 'delete' ), ), ), 'put' => array( 'type' => 'method', 'options' => array( 'verb' => 'put', 'defaults' => array( 'action' => 'put' ), ), ),//and on... ), ), ), ),
Comments
Post a Comment