/** * Checks to see if the given URL can be parsed by this route. * If the route can be parsed an array of parameters will be returned if not * false will be returned. String urls are parsed if they match a routes regular expression. * * @param string $url The url to attempt to parse. * @return mixed Boolean false on failure, otherwise an array or parameters * @access public */ public function parse($url) { $params = parent::parse($url); if (empty($params)) { return false; } if (isset($this->options['models']) && isset($params['_args_'])) { $index = -1; foreach ($this->options['models'] as $checkNamed => $slugField) { $index++; if (is_numeric($checkNamed)) { $checkNamed = $slugField; $slugField = null; } $slugSet = $this->_Sluggable->getSlugs($checkNamed, $slugField); if ($slugSet === false) { continue; } $slugSet = array_flip($slugSet); $passed = explode('/', $params['_args_']); foreach ($passed as $key => $pass) { if (isset($slugSet[$pass])) { unset($passed[$key]); $passed[$index] = $slugSet[$pass]; } } $params['_args_'] = implode('/', $passed); } return $params; } return false; }
public function testGetSlugs() { $Sluggable = new SluggableRoute(null, null, null); $Sluggable->options['models'] = array('RouteTest' => array()); $Sluggable->config(); $results = $Sluggable->getSlugs($this->RouteTest->alias); $expected = array(1 => 'a-page-title', 2 => 'another-title', 3 => 'i-love-cakephp'); $this->assertEquals($expected, $results); SlugCache::invalidate('RouteTest'); $Sluggable->options['models'] = array('RouteTest' => array('slugField' => 'name')); $Sluggable->config(); $results = $Sluggable->getSlugs($this->RouteTest->alias); $expected = array(1 => 'page-title', 2 => 'routing-is-fun', 3 => 'cake-rocks'); $this->assertEquals($expected, $results); }
public function testGetSlugs() { $SluggableRoute = new SluggableRoute(null, null, null); $results = $SluggableRoute->getSlugs($this->RouteTest->alias); $expected = array(1 => 'a-page-title', 2 => 'another-title', 3 => 'i-love-cakephp'); $this->assertEquals($results, $expected); unset($SluggableRoute->RouteTest_slugs); Cache::clear(false, 'Slugger'); $results = $SluggableRoute->getSlugs($this->RouteTest->alias, 'name'); $expected = array(1 => 'page-title', 2 => 'routing-is-fun', 3 => 'cake-rocks'); $this->assertEquals($results, $expected); }