CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Team
    • Issues (Github)
    • YouTube Channel
    • Get Involved
    • Bakery
    • Featured Resources
    • Newsletter
    • Certification
    • My CakePHP
    • CakeFest
    • Facebook
    • Twitter
    • Help & Support
    • Forum
    • Stack Overflow
    • IRC
    • Slack
    • Paid Support
CakePHP

C CakePHP 3.8 Red Velvet API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 3.8
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

Namespaces

  • Cake
    • Auth
      • Storage
    • Cache
      • Engine
    • Collection
      • Iterator
    • Command
    • Console
      • Exception
    • Controller
      • Component
      • Exception
    • Core
      • Configure
        • Engine
      • Exception
      • Retry
    • Database
      • Driver
      • Exception
      • Expression
      • Schema
      • Statement
      • Type
    • Datasource
      • Exception
    • Error
      • Middleware
    • Event
      • Decorator
    • Filesystem
    • Form
    • Http
      • Client
        • Adapter
        • Auth
      • Cookie
      • Exception
      • Middleware
      • Session
    • I18n
      • Formatter
      • Middleware
      • Parser
    • Log
      • Engine
    • Mailer
      • Exception
      • Transport
    • Network
      • Exception
    • ORM
      • Association
      • Behavior
        • Translate
      • Exception
      • Locator
      • Rule
    • Routing
      • Exception
      • Filter
      • Middleware
      • Route
    • Shell
      • Helper
      • Task
    • TestSuite
      • Fixture
      • Stub
    • Utility
      • Exception
    • Validation
    • View
      • Exception
      • Form
      • Helper
      • Widget
  • None

Classes

  • CacheShell
  • CommandListShell
  • CompletionShell
  • I18nShell
  • OrmCacheShell
  • PluginShell
  • RoutesShell
  • SchemaCacheShell
  • ServerShell
  1: <?php
  2: /**
  3:  * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4:  * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5:  *
  6:  * Licensed under The MIT License
  7:  * For full copyright and license information, please see the LICENSE.txt
  8:  * Redistributions of files must retain the above copyright notice.
  9:  *
 10:  * @copyright     Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
 11:  * @link          https://cakephp.org CakePHP(tm) Project
 12:  * @since         3.1.0
 13:  * @license       https://opensource.org/licenses/mit-license.php MIT License
 14:  */
 15: namespace Cake\Shell;
 16: 
 17: use Cake\Console\Shell;
 18: use Cake\Http\ServerRequest;
 19: use Cake\Routing\Exception\MissingRouteException;
 20: use Cake\Routing\Router;
 21: 
 22: /**
 23:  * Provides interactive CLI tools for routing.
 24:  */
 25: class RoutesShell extends Shell
 26: {
 27:     /**
 28:      * Override main() to handle action
 29:      * Displays all routes in an application.
 30:      *
 31:      * @return void
 32:      */
 33:     public function main()
 34:     {
 35:         $output = [
 36:             ['Route name', 'URI template', 'Defaults']
 37:         ];
 38:         foreach (Router::routes() as $route) {
 39:             $name = isset($route->options['_name']) ? $route->options['_name'] : $route->getName();
 40:             ksort($route->defaults);
 41:             $output[] = [$name, $route->template, json_encode($route->defaults)];
 42:         }
 43:         $this->helper('table')->output($output);
 44:         $this->out();
 45:     }
 46: 
 47:     /**
 48:      * Checks a url for the route that will be applied.
 49:      *
 50:      * @param string $url The URL to parse
 51:      * @return bool Success
 52:      */
 53:     public function check($url)
 54:     {
 55:         try {
 56:             $request = new ServerRequest(['url' => $url]);
 57:             $route = Router::parseRequest($request);
 58:             $name = null;
 59:             foreach (Router::routes() as $r) {
 60:                 if ($r->match($route)) {
 61:                     $name = isset($r->options['_name']) ? $r->options['_name'] : $r->getName();
 62:                     break;
 63:                 }
 64:             }
 65: 
 66:             unset($route['_matchedRoute']);
 67:             ksort($route);
 68: 
 69:             $output = [
 70:                 ['Route name', 'URI template', 'Defaults'],
 71:                 [$name, $url, json_encode($route)]
 72:             ];
 73:             $this->helper('table')->output($output);
 74:             $this->out();
 75:         } catch (MissingRouteException $e) {
 76:             $this->warn("'$url' did not match any routes.");
 77:             $this->out();
 78: 
 79:             return false;
 80:         }
 81: 
 82:         return true;
 83:     }
 84: 
 85:     /**
 86:      * Generate a URL based on a set of parameters
 87:      *
 88:      * Takes variadic arguments of key/value pairs.
 89:      * @return bool Success
 90:      */
 91:     public function generate()
 92:     {
 93:         try {
 94:             $args = $this->_splitArgs($this->args);
 95:             $url = Router::url($args);
 96:             $this->out("> $url");
 97:             $this->out();
 98:         } catch (MissingRouteException $e) {
 99:             $this->err('<warning>The provided parameters do not match any routes.</warning>');
100:             $this->out();
101: 
102:             return false;
103:         }
104: 
105:         return true;
106:     }
107: 
108:     /**
109:      * Get the option parser.
110:      *
111:      * @return \Cake\Console\ConsoleOptionParser
112:      */
113:     public function getOptionParser()
114:     {
115:         $parser = parent::getOptionParser();
116:         $parser->setDescription(
117:             'Get the list of routes connected in this application. ' .
118:             'This tool also lets you test URL generation and URL parsing.'
119:         )->addSubcommand('check', [
120:             'help' => 'Check a URL string against the routes. ' .
121:                 'Will output the routing parameters the route resolves to.'
122:         ])->addSubcommand('generate', [
123:             'help' => 'Check a routing array against the routes. ' .
124:                 "Will output the URL if there is a match.\n\n" .
125:                 'Routing parameters should be supplied in a key:value format. ' .
126:                 'For example `controller:Articles action:view 2`'
127:         ]);
128: 
129:         return $parser;
130:     }
131: 
132:     /**
133:      * Split the CLI arguments into a hash.
134:      *
135:      * @param array $args The arguments to split.
136:      * @return array
137:      */
138:     protected function _splitArgs($args)
139:     {
140:         $out = [];
141:         foreach ($args as $arg) {
142:             if (strpos($arg, ':') !== false) {
143:                 list($key, $value) = explode(':', $arg);
144:                 if (in_array($value, ['true', 'false'])) {
145:                     $value = $value === 'true';
146:                 }
147:                 $out[$key] = $value;
148:             } else {
149:                 $out[] = $arg;
150:             }
151:         }
152: 
153:         return $out;
154:     }
155: }
156: 
Follow @CakePHP
#IRC
OpenHub
Rackspace
  • Business Solutions
  • Showcase
  • Documentation
  • Book
  • API
  • Videos
  • Logos & Trademarks
  • Community
  • Team
  • Issues (Github)
  • YouTube Channel
  • Get Involved
  • Bakery
  • Featured Resources
  • Newsletter
  • Certification
  • My CakePHP
  • CakeFest
  • Facebook
  • Twitter
  • Help & Support
  • Forum
  • Stack Overflow
  • IRC
  • Slack
  • Paid Support

Generated using CakePHP API Docs