parse() public method

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.
public parse ( string $url, string $method = '' ) : array | false
$url string The URL to attempt to parse.
$method string The HTTP method of the request being parsed.
return array | false An array of request parameters, or false on failure.
Example #1
0
 /**
  * Parses a string URL into an array. Parsed URLs will result in an automatic
  * redirection.
  *
  * @param string $url The URL to parse.
  * @param string $method The HTTP method being used.
  * @return false|null False on failure. An exception is raised on a successful match.
  * @throws \Cake\Routing\Exception\RedirectException An exception is raised on successful match.
  *   This is used to halt route matching and signal to the middleware that a redirect should happen.
  */
 public function parse($url, $method = '')
 {
     $params = parent::parse($url, $method);
     if (!$params) {
         return false;
     }
     $redirect = $this->redirect;
     if (count($this->redirect) === 1 && !isset($this->redirect['controller'])) {
         $redirect = $this->redirect[0];
     }
     if (isset($this->options['persist']) && is_array($redirect)) {
         $redirect += ['pass' => $params['pass'], 'url' => []];
         if (is_array($this->options['persist'])) {
             foreach ($this->options['persist'] as $elem) {
                 if (isset($params[$elem])) {
                     $redirect[$elem] = $params[$elem];
                 }
             }
         }
         $redirect = Router::reverse($redirect);
     }
     $status = 301;
     if (isset($this->options['status']) && ($this->options['status'] >= 300 && $this->options['status'] < 400)) {
         $status = $this->options['status'];
     }
     throw new RedirectException(Router::url($redirect, true), $status);
 }
 /**
  * Parses a string URL into an array. If a plugin key is found, it will be copied to the
  * controller parameter
  *
  * @param string $url The URL to parse
  * @return mixed false on failure, or an array of request parameters
  */
 public function parse($url)
 {
     $params = parent::parse($url);
     if (!$params) {
         return false;
     }
     $params['controller'] = $params['plugin'];
     return $params;
 }
 public function makeRouteDetails(Entity $entity)
 {
     if (!empty($entity->link)) {
         $InflectedRoute = new Route($entity->route);
         $connection = $InflectedRoute->parse($entity->link);
         print_r($connection);
         exit;
     } else {
     }
 }
 /**
  * Parses a string URL into an array. If it matches, it will convert the
  * controller and plugin keys to their CamelCased form and action key to
  * camelBacked form.
  *
  * @param string $url The URL to parse
  * @return array|false An array of request parameters, or false on failure.
  */
 public function parse($url, $method = '')
 {
     $params = parent::parse($url, $method = '');
     if (!$params) {
         return false;
     }
     $tableSlugs = TableRegistry::get('Slug.Slugs');
     $params = $tableSlugs->loadBySlug($params['slug']);
     return $params;
 }
 /**
  * Parses a string URL into an array. If it matches, it will convert the prefix, controller and
  * plugin keys to their camelized form
  *
  * @param string $url The URL to parse
  * @return mixed false on failure, or an array of request parameters
  */
 public function parse($url)
 {
     $params = parent::parse($url);
     if (!$params) {
         return false;
     }
     $this->Posts = TableRegistry::get('Posts');
     $query = $this->Posts->find('all', ['conditions' => ['Posts.slug' => $params['slug']]]);
     if (!$query->count()) {
         return false;
     }
     return $params;
 }
Example #6
0
 /**
  * Parses a string URL into an array. If it matches, it will convert the prefix, controller and
  * plugin keys to their camelized form
  *
  * @param string $url The URL to parse
  * @return mixed false on failure, or an array of request parameters
  */
 public function parse($url)
 {
     $params = parent::parse($url);
     if (!$params) {
         return false;
     }
     if (!empty($params['controller'])) {
         $params['controller'] = Inflector::camelize($params['controller']);
     }
     if (!empty($params['plugin'])) {
         $params['plugin'] = Inflector::camelize($params['plugin']);
     }
     return $params;
 }
Example #7
0
 /**
  * Parses a string URL into an array. If it matches, it will convert the
  * controller and plugin keys to their CamelCased form and action key to
  * camelBacked form.
  *
  * @param string $url The URL to parse
  * @return array|false An array of request parameters, or false on failure.
  */
 public function parse($url)
 {
     $params = parent::parse($url);
     if (!$params) {
         return false;
     }
     if (!empty($params['controller'])) {
         $params['controller'] = Inflector::camelize(str_replace('-', '_', $params['controller']));
     }
     if (!empty($params['plugin'])) {
         $params['plugin'] = $this->_camelizePlugin($params['plugin']);
     }
     if (!empty($params['action'])) {
         $params['action'] = Inflector::variable(str_replace('-', '_', $params['action']));
     }
     return $params;
 }
Example #8
0
 /**
  * Parses a string URL into an array. If it matches, it will convert the slug keys to their slugerize form
  *
  * @param string $url The URL to parse.
  * @param string $method The HTTP method.
  *
  * @return false|array False on failure, or an array of request parameters.
  */
 public function parse($url, $method = '')
 {
     $params = parent::parse($url, $method);
     if (!$params) {
         return false;
     }
     if (!empty($params['slug'])) {
         $params['slug'] = strtolower(Inflector::slug($params['slug']));
     }
     if (!empty($params['controller'])) {
         $params['controller'] = Inflector::camelize($params['controller']);
     }
     if (!empty($params['plugin'])) {
         $params['plugin'] = Inflector::camelize($params['plugin']);
     }
     return $params;
 }
Example #9
0
 /**
  * Parses a string URL into an array. If it matches, it will convert the prefix, controller and
  * plugin keys to their camelized form.
  *
  * @param string $url The URL to parse
  * @param string $method The HTTP method being matched.
  * @return array|false An array of request parameters, or false on failure.
  */
 public function parse($url, $method = '')
 {
     $params = parent::parse($url);
     if (!$params) {
         return false;
     }
     if (!empty($params['controller'])) {
         $params['controller'] = Inflector::camelize($params['controller']);
     }
     if (!empty($params['plugin'])) {
         if (strpos($params['plugin'], '/') === false) {
             $params['plugin'] = Inflector::camelize($params['plugin']);
         } else {
             list($vendor, $plugin) = explode('/', $params['plugin'], 2);
             $params['plugin'] = Inflector::camelize($vendor) . '/' . Inflector::camelize($plugin);
         }
     }
     return $params;
 }
Example #10
0
 /**
  * Testing that patterns on the :action param work properly.
  *
  * @return void
  */
 public function testPatternOnAction()
 {
     $route = new Route('/blog/:action/*', array('controller' => 'blog_posts'), array('action' => 'other|actions'));
     $result = $route->match(array('controller' => 'blog_posts', 'action' => 'foo'));
     $this->assertFalse($result);
     $result = $route->match(array('controller' => 'blog_posts', 'action' => 'actions'));
     $this->assertEquals('/blog/actions/', $result);
     $result = $route->parse('/blog/other');
     $expected = array('controller' => 'blog_posts', 'action' => 'other', 'pass' => []);
     $this->assertEquals($expected, $result);
     $result = $route->parse('/blog/foobar');
     $this->assertFalse($result);
 }
 /**
  * test that utf-8 patterns work for :section
  *
  * @return void
  */
 public function testUTF8PatternOnSection()
 {
     $route = new Route('/:section', ['plugin' => 'blogs', 'controller' => 'posts', 'action' => 'index'], ['persist' => ['section'], 'section' => 'آموزش|weblog']);
     $result = $route->parse('/%D8%A2%D9%85%D9%88%D8%B2%D8%B4');
     $expected = ['section' => 'آموزش', 'plugin' => 'blogs', 'controller' => 'posts', 'action' => 'index', 'pass' => []];
     $this->assertEquals($expected, $result);
     $result = $route->parse('/weblog');
     $expected = ['section' => 'weblog', 'plugin' => 'blogs', 'controller' => 'posts', 'action' => 'index', 'pass' => []];
     $this->assertEquals($expected, $result);
 }
Example #12
-1
 /**
  * Parses a string URL into an array. Parsed URLs will result in an automatic
  * redirection
  *
  * @param string $url The URL to parse
  * @return bool False on failure
  */
 public function parse($url)
 {
     $params = parent::parse($url);
     if (!$params) {
         return false;
     }
     if (!$this->response) {
         $this->response = new Response();
     }
     $redirect = $this->redirect;
     if (count($this->redirect) === 1 && !isset($this->redirect['controller'])) {
         $redirect = $this->redirect[0];
     }
     if (isset($this->options['persist']) && is_array($redirect)) {
         $redirect += ['pass' => $params['pass'], 'url' => []];
         if (is_array($this->options['persist'])) {
             foreach ($this->options['persist'] as $elem) {
                 if (isset($params[$elem])) {
                     $redirect[$elem] = $params[$elem];
                 }
             }
         }
         $redirect = Router::reverse($redirect);
     }
     $status = 301;
     if (isset($this->options['status']) && ($this->options['status'] >= 300 && $this->options['status'] < 400)) {
         $status = $this->options['status'];
     }
     $this->response->header(['Location' => Router::url($redirect, true)]);
     $this->response->statusCode($status);
     $this->response->send();
     $this->response->stop();
 }