currentRoute() public static method

Returns the route matching the current request (useful for requestAction traces)
public static currentRoute ( ) : CakeRoute
return CakeRoute Matching route object.
 public function error500($error)
 {
     if (Router::currentRoute()->defaults['plugin'] == 'api') {
         $this->controller->viewClass = 'Json';
     }
     parent::error500($error);
 }
Example #2
0
 public function testAssignsCurrentRoute()
 {
     $route = Router::get('/apath', function () {
     });
     $this->assertNull(Router::currentRoute());
     Router::run('GET', 'localboat.dev', '/apath');
     $this->assertNotNull(Router::currentRoute());
 }
 /**
  * Controllers initialize function.
  */
 public function initialize($Controller)
 {
     parent::initialize($Controller);
     $this->Controller = $Controller;
     Configure::write('CORE.current_route', Router::currentRoute());
     $this->__registerPlugins();
     $this->__paginationRecall();
     if (Configure::read('Website.force_www')) {
         $this->forceWwwUrl();
     }
 }
Example #4
0
 /**
  * beforeRender callback - grabs request params
  *
  * @param Controller $controller
  * @return array
  */
 public function beforeRender(Controller $controller)
 {
     $out = array();
     $out['params'] = $controller->request->params;
     $out['url'] = $controller->request->url;
     $out['query'] = $controller->request->query;
     $out['data'] = $controller->request->data;
     if (isset($controller->Cookie)) {
         $out['cookie'] = $controller->Cookie->read();
     }
     $out['get'] = $_GET;
     $out['currentRoute'] = Router::currentRoute();
     return $out;
 }
Example #5
0
 /**
  * Module Loader.
  *
  * This is used to load modules. it generates a wrapper div with the class
  * set as the module name for easy styleing, and will create a header if set
  * in the backend.
  *
  * @params string $position this is the possition that is to be loaded, can be anything from the database
  * @params bool $admin if true its a admin module that should be loaded.
  */
 function loadModules($position = null, $admin = false)
 {
     if (!$position) {
         $this->errors[] = 'No position selected to load modules';
         return false;
     }
     $modules = ClassRegistry::init('Management.Module')->getModules($position, $admin);
     $out = '<div class="modules ' . $position . '">';
     $currentRoute = Router::currentRoute();
     foreach ($modules as $module) {
         if ($module['Theme']['name'] != '' && $module['Theme']['name'] != $this->theme) {
             //skip modules that are not for the current theme
             continue;
         }
         $moduleOut = '<div class="module ' . $module['Module']['module'] . '">';
         if ($module['Module']['show_heading']) {
             $moduleOut .= '<h2>' . __($module['Module']['name'], true) . '</h2>';
         }
         if (!empty($module['Module']['module'])) {
             $View = ClassRegistry::getObject('View');
             $path = 'modules/';
             if ($admin) {
                 $path .= 'admin/';
             }
             $moduleOut .= $View->element($path . $module['Module']['module'], array('config' => $this->_moduleConfig($module['Module'])), true);
         } else {
             if (!empty($module['Module']['content'])) {
                 $moduleOut .= $module['Module']['content'];
             }
         }
         $moduleOut .= '</div>';
         if (!empty($module['Route']) && is_object($currentRoute)) {
             foreach ($module['Route'] as $route) {
                 if ($route['url'] == $currentRoute->template) {
                     $out .= $moduleOut;
                 }
             }
         } else {
             if (empty($module['Route'])) {
                 $out .= $moduleOut;
             }
         }
     }
     $out .= '</div>';
     return $out;
 }
Example #6
0
 /**
  * testCurentRoute
  *
  * This test needs some improvement and actual requestAction() usage
  * 
  * @return void
  * @access public
  */
 function testCurentRoute()
 {
     $url = array('controller' => 'pages', 'action' => 'display', 'government');
     Router::connect('/government', $url);
     Router::parse('/government');
     $route = Router::currentRoute();
     $this->assertEqual(array_merge($url, array('plugin' => false)), $route[3]);
 }
Example #7
0
 /**
  * Lets you do functional tests of a controller action.
  *
  * ### Options:
  *
  * - `data` Will be used as the request data. If the `method` is GET,
  *   data will be used a GET params. If the `method` is POST, it will be used
  *   as POST data. By setting `$options['data']` to a string, you can simulate XML or JSON
  *   payloads to your controllers allowing you to test REST webservices.
  * - `method` POST or GET. Defaults to POST.
  * - `return` Specify the return type you want. Choose from:
  *     - `vars` Get the set view variables.
  *     - `view` Get the rendered view, without a layout.
  *     - `contents` Get the rendered view including the layout.
  *     - `result` Get the return value of the controller action. Useful
  *       for testing requestAction methods.
  *
  * @param string $url The url to test
  * @param array $options See options
  * @return mixed
  */
 protected function _testAction($url = '', $options = array())
 {
     $this->vars = $this->result = $this->view = $this->contents = $this->headers = null;
     $options = array_merge(array('data' => array(), 'method' => 'POST', 'return' => 'result'), $options);
     $restore = array('get' => $_GET, 'post' => $_POST);
     $_SERVER['REQUEST_METHOD'] = strtoupper($options['method']);
     if (is_array($options['data'])) {
         if (strtoupper($options['method']) === 'GET') {
             $_GET = $options['data'];
             $_POST = array();
         } else {
             $_POST = $options['data'];
             $_GET = array();
         }
     }
     $request = $this->getMock('CakeRequest', array('_readInput'), array($url));
     if (is_string($options['data'])) {
         $request->expects($this->any())->method('_readInput')->will($this->returnValue($options['data']));
     }
     $Dispatch = new ControllerTestDispatcher();
     foreach (Router::$routes as $route) {
         if ($route instanceof RedirectRoute) {
             $route->response = $this->getMock('CakeResponse', array('send'));
         }
     }
     $Dispatch->loadRoutes = $this->loadRoutes;
     $Dispatch->parseParams(new CakeEvent('ControllerTestCase', $Dispatch, array('request' => $request)));
     if (!isset($request->params['controller']) && Router::currentRoute()) {
         $this->headers = Router::currentRoute()->response->header();
         return;
     }
     if ($this->_dirtyController) {
         $this->controller = null;
     }
     $plugin = empty($request->params['plugin']) ? '' : Inflector::camelize($request->params['plugin']) . '.';
     if ($this->controller === null && $this->autoMock) {
         $this->generate($plugin . Inflector::camelize($request->params['controller']));
     }
     $params = array();
     if ($options['return'] === 'result') {
         $params['return'] = 1;
         $params['bare'] = 1;
         $params['requested'] = 1;
     }
     $Dispatch->testController = $this->controller;
     $Dispatch->response = $this->getMock('CakeResponse', array('send'));
     $this->result = $Dispatch->dispatch($request, $Dispatch->response, $params);
     $this->controller = $Dispatch->testController;
     $this->vars = $this->controller->viewVars;
     $this->contents = $this->controller->response->body();
     if (isset($this->controller->View)) {
         $this->view = $this->controller->View->fetch('__view_no_layout__');
     }
     $this->_dirtyController = true;
     $this->headers = $Dispatch->response->header();
     $_GET = $restore['get'];
     $_POST = $restore['post'];
     return $this->{$options['return']};
 }
Example #8
0
 /**
  * Tests a controller action.
  *
  * ### Options:
  *
  * - `data` POST or GET data to pass
  * - `method` POST or GET
  *
  * @param string $url The url to test
  * @param array $options See options
  */
 private function _testAction($url = '', $options = array())
 {
     $this->vars = $this->result = $this->view = $this->contents = $this->headers = null;
     $options = array_merge(array('data' => array(), 'method' => 'POST', 'return' => 'result'), $options);
     if (strtoupper($options['method']) == 'GET') {
         $_GET = $options['data'];
         $_POST = array();
     } else {
         $_POST = array('data' => $options['data']);
         $_GET = array();
     }
     $request = new CakeRequest($url);
     $Dispatch = new ControllerTestDispatcher();
     foreach (Router::$routes as $route) {
         if (is_a($route, 'RedirectRoute')) {
             $route->response = $this->getMock('CakeResponse', array('send'));
         }
     }
     $Dispatch->loadRoutes = $this->loadRoutes;
     $request = $Dispatch->parseParams($request);
     if (!isset($request->params['controller'])) {
         $this->headers = Router::currentRoute()->response->header();
         return;
     }
     if ($this->controller !== null && Inflector::camelize($request->params['controller']) !== $this->controller->name) {
         $this->controller = null;
     }
     if ($this->controller === null && $this->autoMock) {
         $this->generate(Inflector::camelize($request->params['controller']));
     }
     $params = array();
     if ($options['return'] == 'result') {
         $params['return'] = 1;
         $params['bare'] = 1;
         $params['requested'] = 1;
     }
     $Dispatch->testController = $this->controller;
     $Dispatch->response = $this->getMock('CakeResponse', array('send'));
     $this->result = $Dispatch->dispatch($request, $params);
     $this->controller = $Dispatch->testController;
     if ($options['return'] != 'result') {
         $this->vars = $this->controller->View->viewVars;
         $this->view = $this->controller->View->_viewNoLayout;
         $this->contents = $this->controller->response->body();
     }
     $this->headers = $Dispatch->response->header();
     return $this->{$options['return']};
 }
Example #9
0
 /**
  * beforeRender callback - grabs request params
  *
  * @return array
  **/
 function beforeRender(&$controller)
 {
     $out = array();
     $out['params'] = $controller->params;
     if (isset($controller->Cookie)) {
         $out['cookie'] = $controller->Cookie->read();
     }
     $out['get'] = $_GET;
     $out['currentRoute'] = Router::currentRoute();
     return $out;
 }
Example #10
0
 public function beforeRender(Controller $Controller)
 {
     parent::beforeRender($Controller);
     if ($Controller->layout == 'ajax') {
         return;
     }
     if (!empty($Controller->viewVars['error']) && $Controller->viewVars['error'] instanceof Exception) {
         $error = $Controller->viewVars['error'];
         unset($Controller->viewVars['error']);
     }
     $layout = array_values($Controller->viewVars);
     $theme = current(Set::extract('/Layout/theme_id', $layout));
     $layout = current(Set::extract('/Layout/layout', $layout));
     if (!empty($error)) {
         $Controller->viewVars['error'] = $error;
         $layout = 'error';
     }
     if ($layout) {
         Configure::write('Themes.default_layout', $layout);
     }
     $event = $Controller->Event->trigger($Controller->plugin . '.setupThemeStart');
     if (isset($event['setupThemeStart'][$Controller->plugin])) {
         if (is_string($event['setupThemeStart'][$Controller->plugin])) {
             $Controller->theme = $event['setupThemeStart'][$Controller->plugin];
             return true;
         } else {
             if ($event['setupThemeStart'][$Controller->plugin] === false) {
                 return false;
             }
         }
     }
     $Controller->layout = Configure::read('Themes.default_layout');
     $theme = Cache::read('currentTheme');
     if ($theme === false) {
         $theme = ClassRegistry::init('Themes.Theme')->getCurrentTheme();
     }
     if (!empty($theme['Theme']['default_layout'])) {
         $Controller->layout = $theme['Theme']['default_layout'];
     }
     if (isset($Controller->request->params['admin']) && $Controller->request->params['admin']) {
         $Controller->layout = Configure::read('Themes.default_layout_admin');
     }
     $event = $Controller->Event->trigger($Controller->plugin . '.setupThemeLayout', array('layout' => $Controller->layout, 'params' => $Controller->request->params));
     if (isset($event['setupThemeLayout'][$Controller->plugin]) && is_string($event['setupThemeLayout'][$Controller->plugin])) {
         $Controller->layout = $event['setupThemeLayout'][$Controller->plugin];
     }
     if (!isset($theme['Theme']['name'])) {
         $theme['Theme'] = array('name' => null);
     } else {
         $event = $Controller->Event->trigger($Controller->plugin . '.setupThemeSelector', array('theme' => $theme['Theme'], 'params' => $Controller->request->params));
         if (isset($event['setupThemeSelector'][$Controller->plugin])) {
             if (is_array($event['setupThemeSelector'][$Controller->plugin])) {
                 $theme['Theme'] = $event['setupThemeSelector'][$Controller->plugin];
                 if (!isset($theme['Theme']['name'])) {
                     $this->cakeError('eventError', array('message' => 'The theme is invalid.', 'event' => $event));
                 }
             }
         }
     }
     $Controller->theme = $theme['Theme']['name'];
     Configure::write('Theme', $theme['Theme']);
     $event = $Controller->Event->trigger($Controller->plugin . '.setupThemeRoutes', array('params' => $Controller->request->params));
     if (isset($event['setupThemeRoutes'][$Controller->plugin]) && !$event['setupThemeRoutes'][$Controller->plugin]) {
         return false;
     }
     if (empty($routes)) {
         $routes = Classregistry::init('Routes.Route')->getRoutes();
     }
     $currentRoute = Router::currentRoute(Configure::read('CORE.current_route'));
     if (!empty($routes) && is_object($currentRoute)) {
         foreach ($routes as $route) {
             if ($route['Route']['url'] == $currentRoute->template) {
                 if (!empty($route['Route']['theme'])) {
                     $Controller->theme = $route['Route']['theme'];
                 }
                 if (!empty($route['Route']['layout'])) {
                     $Controller->layout = $route['Route']['layout'];
                 }
             }
         }
     }
     $event = $Controller->Event->trigger($Controller->plugin . '.setupThemeEnd', array('theme' => $Controller->theme, 'params' => $Controller->request->params));
     if (isset($event['setupThemeEnd'][$Controller->plugin])) {
         if (is_string($event['setupThemeEnd'][$Controller->plugin])) {
             $Controller->theme = $event['setupThemeEnd'][$Controller->plugin];
         }
     }
     return true;
 }
Example #11
0
 /**
  * Checks if a given url is currently active
  * @param mixed $url The url to check, can be and valid router string or array
  * @return boolean Returns true if the passed url is active
  */
 function isActive($url)
 {
     $currentRoute = Router::currentRoute();
     $url = Router::url($url);
     if ($currentRoute[0] == $url) {
         return true;
     }
     return false;
 }
Example #12
0
 /**
  * Setup the theme for the site
  *
  * Gets the current theme set in db and sets if up
  */
 function setupTheme()
 {
     $event = $this->Controller->Event->trigger($this->Controller->plugin . '.setupThemeStart');
     if (isset($event['setupThemeStart'][$this->Controller->plugin])) {
         if (is_string($event['setupThemeStart'][$this->Controller->plugin])) {
             $this->Controller->theme = $event['setupThemeStart'][$this->Controller->plugin];
             return true;
         } else {
             if ($event['setupThemeStart'][$this->Controller->plugin] === false) {
                 return false;
             }
         }
     }
     $this->Controller->layout = 'front';
     if (isset($this->Controller->params['admin']) && $this->Controller->params['admin']) {
         $this->Controller->layout = 'admin';
     }
     $event = $this->Controller->Event->trigger($this->Controller->plugin . '.setupThemeLayout', array('layout' => $this->Controller->layout, 'params' => $this->Controller->params));
     if (isset($event['setupThemeLayout'][$this->Controller->plugin])) {
         if (is_string($event['setupThemeLayout'][$this->Controller->plugin])) {
             $this->Controller->layout = $event['setupThemeLayout'][$this->Controller->plugin];
         }
     }
     if (!($theme = Cache::read('currentTheme'))) {
         $theme = ClassRegistry::init('Management.Theme')->getCurrentTheme();
     }
     if (!isset($theme['Theme']['name'])) {
         $theme['Theme'] = array();
     } else {
         $event = $this->Controller->Event->trigger($this->Controller->plugin . '.setupThemeSelector', array('theme' => $theme['Theme'], 'params' => $this->Controller->params));
         if (isset($event['setupThemeSelector'][$this->Controller->plugin])) {
             if (is_array($event['setupThemeSelector'][$this->Controller->plugin])) {
                 $theme['Theme'] = $event['setupThemeSelector'][$this->Controller->plugin];
                 if (!isset($theme['Theme']['name'])) {
                     $this->cakeError('eventError', array('message' => 'The theme is invalid.', 'event' => $event));
                 }
             }
         }
     }
     $this->Controller->theme = $theme['Theme']['name'];
     Configure::write('Theme', $theme['Theme']);
     $event = $this->Controller->Event->trigger($this->Controller->plugin . '.setupThemeRoutes', array('params' => $this->Controller->params));
     if (isset($event['setupThemeRoutes'][$this->Controller->plugin]) && !$event['setupThemeRoutes'][$this->Controller->plugin]) {
         return false;
     }
     $routes = Cache::read('routes', 'core');
     if (empty($routes)) {
         $routes = Classregistry::init('Management.Route')->getRoutes();
     }
     $currentRoute = Router::currentRoute();
     if (!empty($routes) && is_object($currentRoute)) {
         foreach ($routes as $route) {
             if ($route['Route']['url'] == $currentRoute->template && !empty($route['Route']['theme'])) {
                 $this->Controller->theme = $route['Route']['theme'];
             }
         }
     }
     $event = $this->Controller->Event->trigger($this->Controller->plugin . '.setupThemeEnd', array('theme' => $this->Controller->theme, 'params' => $this->Controller->params));
     if (isset($event['setupThemeEnd'][$this->Controller->plugin])) {
         if (is_string($event['setupThemeEnd'][$this->Controller->plugin])) {
             $this->Controller->theme = $event['setupThemeEnd'][$this->Controller->plugin];
         }
     }
 }
Example #13
0
 /**
  * Extract from $params custom routes element, defined such as
  * '/:controller/:year/:month/:day', which are not present inside passedArgs
  *
  * @param array $params
  * @access private
  */
 private function __extractCustomRoutesElements($params = array())
 {
     $route = Router::currentRoute();
     if (!$route) {
         return array();
     }
     $customElements = array_intersect_key($params, array_flip($route->keys));
     return $customElements;
 }