/** * Expand resource handlers to routeset, * in here, we define how to expand RESTful URLs from resource id, * and which is customizable. * * @param RouteSet $routes * @param string $r resource identifier. */ static function expand($routes, $h, $r) { $class = is_object($h) ? get_class($h) : $h; $routes->add("/{$r}(.:format)", array($class, 'handleFind'), array('get' => true, 'default' => array('format' => 'json'))); $routes->add('/' . $r . '(.:format)', array($class, 'handleCreate'), array('post' => true, 'default' => array('format' => 'json'))); $routes->add('/' . $r . '/:id(.:format)', array($class, 'handleLoad'), array('get' => true, 'default' => array('format' => 'json'))); $routes->add('/' . $r . '/:id(.:format)', array($class, 'handleUpdate'), array('put' => true, 'default' => array('format' => 'json'))); $routes->add('/' . $r . '/:id(.:format)', array($class, 'handleDelete'), array('delete' => true, 'default' => array('format' => 'json'))); }
public function testAddRouteSet() { $blogSet = new RouteSet(); $blogSet->setSegmentSeparators(array('/', '-')); $blogSet->addRoute('posts/:id-:slug', array('controller' => 'blog', 'action' => 'view'), array('id' => '\\d+', 'slug' => '[a-zA-Z_]+')); $blogSet->addRoute('archives/:year/:month/:day', array('controller' => 'blog', 'action' => 'by_date', 'month' => null, 'day' => null), array('year' => '\\d{4}', 'day' => '\\d{1,2}', 'month' => '\\d{1,2}')); $set = new RouteSet(); $set->addRoute('', array('controller' => 'home')); $set->addRouteSet('blog/', $blogSet); $this->assertEquals(array('controller' => 'blog', 'action' => 'by_date', 'year' => 2006, 'month' => 02, 'day' => 14), $set->recognizePath('/blog/archives/2006/02/14')); $this->assertEquals(array('controller' => 'blog', 'action' => 'by_date', 'year' => 2006, 'month' => 02, 'day' => null), $set->recognizePath('/blog/archives/2006/02')); $this->assertEquals(array('controller' => 'blog', 'action' => 'by_date', 'year' => 2006, 'month' => null, 'day' => null), $set->recognizePath('/blog/archives/2006')); $this->assertEquals(array('controller' => 'blog', 'action' => 'view', 'id' => 45, 'slug' => 'foo_bar'), $set->recognizePath('/blog/posts/45-foo_bar')); $this->assertEquals(array('controller' => 'blog', 'action' => 'view', 'id' => 45), $set->recognizePath('/blog/posts/45')); $this->assertEquals(array('controller' => 'home'), $set->recognizePath('/')); /*$this->assertEquals( 'blog/posts/45-foo_bar', $set->generate(array('controller'=>'blog', 'action' => 'view', 'id' => 45, 'slug' => 'foo_bar')) );*/ }