/**
  * @return void
  */
 public function main()
 {
     self::_loadRoutes();
     $formatter = "%15s  %-20s  %-20s  %-10s";
     $this->out(sprintf($formatter, 'Method', 'URI Pattern', 'Controller/Action', 'extensions'));
     foreach (Router::routes() as $route) {
         if (isset($route->defaults['_method'])) {
             if (is_array($route->defaults['_method'])) {
                 $outputMethod = implode(',', $route->defaults['_method']);
             } else {
                 $outputMethod = $route->defaults['_method'];
             }
         } else {
             $outputMethod = 'GET';
         }
         $uriPattern = $route->template;
         if (isset($route->options['_ext']) && $route->options['_ext']) {
             $ext = implode(',', $route->options['_ext']);
         } else {
             $ext = "none";
         }
         //そもそもControllerが定義されていないものに関しては表示しないようにしている。
         if (isset($route->defaults['controller'])) {
             $this->out(sprintf($formatter, $outputMethod, $uriPattern, $route->defaults['controller'] . '/' . $route->defaults['action'], $ext));
         }
     }
 }
Пример #2
0
 /**
  * Checks a url for the route that will be applied.
  *
  * @param string $url The URL to parse
  * @return null|false
  */
 public function check($url)
 {
     try {
         $route = Router::parse($url);
         foreach (Router::routes() as $r) {
             if ($r->match($route)) {
                 $name = isset($r->options['_name']) ? $r->options['_name'] : $r->getName();
                 break;
             }
         }
         $output = [['Route name', 'URI template', 'Defaults'], [$name, $url, json_encode($route)]];
         $this->helper('table')->output($output);
         $this->out();
     } catch (MissingRouteException $e) {
         $this->err("<warning>'{$url}' did not match any routes.</warning>");
         $this->out();
         return false;
     }
 }
Пример #3
0
 /**
  * Get summary data for the routes panel.
  *
  * @return int
  */
 public function summary()
 {
     return count(Router::routes());
 }
Пример #4
0
 public static function setupBeforeClass()
 {
     static::$routes = Router::routes();
 }
Пример #5
0
 /**
  * Test that redirect() works with another route class.
  *
  * @return void
  */
 public function testRedirectWithAnotherRouteClass()
 {
     $route1 = $this->getMockBuilder('Cake\\Routing\\Route\\RedirectRoute')->setConstructorArgs(['/mobile\''])->getMock();
     $class = '\\' . get_class($route1);
     Router::redirect('/mobile', '/', ['status' => 301, 'routeClass' => $class]);
     $routes = Router::routes();
     $route = $routes[0];
     $this->assertInstanceOf($class, $route);
 }
 /**
  * Get urls from Router
  */
 protected function _router()
 {
     // go through each defined route
     foreach (Router::routes() as $route) {
         // not supposed to be in sitemap? exclude
         if (!isset($route->options['sitemap'])) {
             continue;
         }
         // defaults
         $changefreq = isset($route->options['sitemap']['frequency']) ? $route->options['sitemap']['frequency'] : 'monthly';
         $priority = isset($route->options['sitemap']['priority']) ? $route->options['sitemap']['priority'] : '0.6';
         // dynamic? execute callback if it exists
         if (strpos($route->template, '*') !== false || strpos($route->template, ':slug') !== false) {
             $callback = $this->_callback($route, true);
             if ($callback) {
                 if (is_array($callback)) {
                     foreach ($callback as $item) {
                         $this->_url[] = ['loc' => $item['url'], 'lastmod' => $item['modified'], 'changefreq' => $changefreq, 'priority' => $priority];
                     }
                 }
             }
             // ignore "/*"
             continue;
         }
         // in exclude array?
         if (in_array($route->template, $this->exclude)) {
             continue;
         }
         // calculate lastmod
         if (isset($route->options['sitemap']['modified'])) {
             switch ($route->options['sitemap']['modified']) {
                 case 'callback':
                     $lastmod = $this->_callback($route);
                     break;
                 case 'ctrl':
                     $lastmod = $this->_ctrl($route);
                     break;
                 default:
                     $lastmod = $route->options['sitemap']['modified'];
             }
         } else {
             $lastmod = $this->_file($route);
         }
         $lastmod = (new Time($lastmod ?: time()))->timezone(date_default_timezone_get())->toAtomString();
         // add record
         $this->_url[] = ['loc' => Router::url($route->template, true), 'lastmod' => $lastmod, 'changefreq' => $changefreq, 'priority' => $priority];
     }
 }
 /**
  * Test that redirect() works.
  *
  * @return void
  */
 public function testRedirect()
 {
     Router::redirect('/mobile', '/', ['status' => 301]);
     $routes = Router::routes();
     $route = $routes[0];
     $this->assertInstanceOf('Cake\\Routing\\Route\\RedirectRoute', $route);
 }
Пример #8
0
 /**
  * Build prefixes for App and Plugins based on configured routes
  *
  * @return void
  */
 protected function _buildPrefixes()
 {
     $routes = Router::routes();
     foreach ($routes as $key => $route) {
         if (isset($route->defaults['prefix'])) {
             $prefix = Inflector::camelize($route->defaults['prefix']);
             if (!isset($route->defaults['plugin'])) {
                 $this->prefixes[$prefix] = true;
             } else {
                 $this->pluginPrefixes[$route->defaults['plugin']][$prefix] = true;
             }
         }
     }
 }
 public function setRoutes()
 {
     $routes_info = array();
     $all_routes = Router::routes();
     $ignore = array('index\\');
     foreach ($all_routes as $route) {
         $reflection = implode('\\', $route->defaults);
         if (!in_array($reflection, $ignore)) {
             $routes_info[$route->template] = json_encode($route->defaults);
             $routes[$route->template] = $reflection . ' -> ' . $route->template;
         }
     }
     $this->controller->set('routes', $routes);
     $this->controller->set('routes_info', $routes_info);
 }
Пример #10
0
 /**
  * Gets a CamelizedList of all existing router prefixes.
  *
  * @return array
  */
 protected function _routerPrefixes()
 {
     $cache = static::cache('_routerPrefixes');
     if (!$cache) {
         $prefixes = [];
         foreach (Router::routes() as $route) {
             if (empty($route->defaults['prefix'])) {
                 continue;
             } else {
                 $prefix = Inflector::camelize($route->defaults['prefix']);
                 if (!in_array($prefix, $prefixes)) {
                     $prefixes[] = $prefix;
                 }
             }
         }
         $cache = static::cache('_routerPrefixes', $prefixes);
     }
     return $cache;
 }