所以,我成功地建立了自己的發電機。
根據你提供數組的事實(「parent_controller」=>「child_controller」)。
這種方式可以產生超人=>超級大國
超級英雄/ 1 /超級大國/(這裏根據你的路由有或withouth的IDS。
參數數量mappeds到方法ARGS所以功能指數($ superhero_id )
function generateNestedResources($array)
{
foreach($array as $key=>$value)
{
//Index -GET/
Router::connect('/'.$key.'/:'.$key.'_id/'.$value,
array('controller' => $value,'action' => 'index','method' => 'GET'),
array($key.'_id' => '[0-9]+','pass' => array($key.'_id')));
// View -GET /id
Router::connect('/'.$key.'/:'.$key.'_id/'.$value.'/:id',
array('controller' => $value,'action' => 'view','method' => 'GET'),
array('id' => '[0-9]+', $key.'_id' => '[0-9]+','pass' => array($key.'_id','id')));
//add -POST/
Router::connect('/'.$key.'/:'.$key.'_id/'.$value,
array('controller' => $value,'action' => 'add','method' => 'POST'),
array($key.'_id' => '[0-9]+','pass' => array($key.'_id')));
//edit -PUT/ID
Router::connect('/'.$key.'/:'.$key.'_id/'.$value.'/:id',
array('controller' => $value,'action' => 'edit','method' => 'PUT'),
array('id' => '[0-9]+', $key.'_id' => '[0-9]+','pass' => array($key.'_id','id')));
//delete -DELEte /ID
Router::connect('/'.$key.'/:'.$key.'_id/'.$value.'/:id',
array('controller' => $value,'action' => 'delete','method' => 'DELETE'),
array('id' => '[0-9]+', $key.'_id' => '[0-9]+','pass' => array($key.'_id','id')));
}
}
難道你只是定義一個新的路線? – Evert