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.7 Red Velvet API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 3.7
      • 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:     /**
 29:      * Override main() to handle action
 30:      * Displays all routes in an application.
 31:      *
 32:      * @return void
 33:      */
 34:     public function main()
 35:     {
 36:         $output = [
 37:             ['Route name', 'URI template', 'Defaults']
 38:         ];
 39:         foreach (Router::routes() as $route) {
 40:             $name = isset($route->options['_name']) ? $route->options['_name'] : $route->getName();
 41:             ksort($route->defaults);
 42:             $output[] = [$name, $route->template, json_encode($route->defaults)];
 43:         }
 44:         $this->helper('table')->output($output);
 45:         $this->out();
 46:     }
 47: 
 48:     /**
 49:      * Checks a url for the route that will be applied.
 50:      *
 51:      * @param string $url The URL to parse
 52:      * @return bool Success
 53:      */
 54:     public function check($url)
 55:     {
 56:         try {
 57:             $request = new ServerRequest(['url' => $url]);
 58:             $route = Router::parseRequest($request);
 59:             $name = null;
 60:             foreach (Router::routes() as $r) {
 61:                 if ($r->match($route)) {
 62:                     $name = isset($r->options['_name']) ? $r->options['_name'] : $r->getName();
 63:                     break;
 64:                 }
 65:             }
 66: 
 67:             unset($route['_matchedRoute']);
 68:             ksort($route);
 69: 
 70:             $output = [
 71:                 ['Route name', 'URI template', 'Defaults'],
 72:                 [$name, $url, json_encode($route)]
 73:             ];
 74:             $this->helper('table')->output($output);
 75:             $this->out();
 76:         } catch (MissingRouteException $e) {
 77:             $this->warn("'$url' did not match any routes.");
 78:             $this->out();
 79: 
 80:             return false;
 81:         }
 82: 
 83:         return true;
 84:     }
 85: 
 86:     /**
 87:      * Generate a URL based on a set of parameters
 88:      *
 89:      * Takes variadic arguments of key/value pairs.
 90:      * @return bool Success
 91:      */
 92:     public function generate()
 93:     {
 94:         try {
 95:             $args = $this->_splitArgs($this->args);
 96:             $url = Router::url($args);
 97:             $this->out("> $url");
 98:             $this->out();
 99:         } catch (MissingRouteException $e) {
100:             $this->err('<warning>The provided parameters do not match any routes.</warning>');
101:             $this->out();
102: 
103:             return false;
104:         }
105: 
106:         return true;
107:     }
108: 
109:     /**
110:      * Get the option parser.
111:      *
112:      * @return \Cake\Console\ConsoleOptionParser
113:      */
114:     public function getOptionParser()
115:     {
116:         $parser = parent::getOptionParser();
117:         $parser->setDescription(
118:             'Get the list of routes connected in this application. ' .
119:             'This tool also lets you test URL generation and URL parsing.'
120:         )->addSubcommand('check', [
121:             'help' => 'Check a URL string against the routes. ' .
122:                 'Will output the routing parameters the route resolves to.'
123:         ])->addSubcommand('generate', [
124:             'help' => 'Check a routing array against the routes. ' .
125:                 "Will output the URL if there is a match.\n\n" .
126:                 'Routing parameters should be supplied in a key:value format. ' .
127:                 'For example `controller:Articles action:view 2`'
128:         ]);
129: 
130:         return $parser;
131:     }
132: 
133:     /**
134:      * Split the CLI arguments into a hash.
135:      *
136:      * @param array $args The arguments to split.
137:      * @return array
138:      */
139:     protected function _splitArgs($args)
140:     {
141:         $out = [];
142:         foreach ($args as $arg) {
143:             if (strpos($arg, ':') !== false) {
144:                 list($key, $value) = explode(':', $arg);
145:                 if (in_array($value, ['true', 'false'])) {
146:                     $value = $value === 'true';
147:                 }
148:                 $out[$key] = $value;
149:             } else {
150:                 $out[] = $arg;
151:             }
152:         }
153: 
154:         return $out;
155:     }
156: }
157: 
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