public function before() { $directory = $this->request->directory(); $controller = $this->request->controller(); $action = $this->request->action(); $format = $this->_detect_response_format(); $mime = File::mime_by_ext($format) ?: 'text/html'; $this->response->view(['basedir' => $controller, 'directory' => $directory, 'action' => $action, 'format' => $format]); $this->response->view->set(['basedir' => $controller, 'directory' => $directory, 'controller' => $controller, 'action' => $action, 'format' => $format, 'request' => $this->request, 'method' => $this->request->method(), 'secure' => $this->request->secure(), 'route' => $this->request->route(), 'route_name' => Route::name($this->request->route()), 'params' => $this->request->param(), 'query' => $this->request->query()]); $this->format = $format; if ($this->_layout === NULL) { $layout = strtolower($controller); if ($this->response->view->viewfile_exists($layout, 'layouts')) { $this->_layout = $layout; } else { if ($this->response->view->viewfile_exists($this->_default_layout, 'layouts')) { $this->_layout = $this->_default_layout; } } } $this->response->headers('Content-Type', $mime . '; charset=' . Kohana::$charset); if ($this->_layout) { View::layout($this->_layout); } $this->_before_action && $this->_call_method_for_action($this->_before_action); }
/** * Get an array of Route_Tester objects from the config settings * * @param $tests A URL to test, or an array of URLs to test. * @returns array An array of Route_Tester objects */ public static function create_tests($tests) { if (is_string($tests)) { $tests = array($tests); } $array = array(); // Get the url and optional expected_params from the config foreach ($tests as $key => $value) { $current = new Route_Tester(); if (is_array($value)) { $url = $key; $current->expected_params = $value; } else { $url = $value; } $current->url = trim($url, '/'); // Test each route, and save the route and params if it matches foreach (Route::all() as $route) { if ($current->params = $route->matches(Request::factory($current->url))) { $current->route = Route::name($route); $current->params = array_merge(array('route' => $current->route), $current->params); break; } } $array[] = $current; } return $array; }
/** * Route::name() should fetch the name of a passed route * If route is not found then it should return FALSE * * @TODO: This test needs to segregate the Route::$_routes singleton * @test * @covers Route::name */ public function test_name_returns_routes_name_or_false_if_dnx() { $route = Route::set('flamingo_people', 'flamingo/dance'); $this->assertSame('flamingo_people', Route::name($route)); $route = new Route('dance/dance'); $this->assertFalse(Route::name($route)); }
public function error($message) { $this->response->status(404); $this->template->title = "Userguide - Error"; $this->template->content = View::factory('userguide/error', array('message' => $message)); // Don't show disqus on error pages $this->template->show_comments = FALSE; // If we are in a module and that module has a menu, show that if ($module = $this->request->param('module') and $menu = $this->file($module . '/menu') and Kohana::$config->load('userguide.modules.' . $module . '.enabled')) { // Namespace the markdown parser Kodoc_Markdown::$base_url = URL::site($this->guide->uri()) . '/' . $module . '/'; Kodoc_Markdown::$image_url = URL::site($this->media->uri()) . '/' . $module . '/'; $this->template->menu = Kodoc_Markdown::markdown($this->_get_all_menu_markdown()); $this->template->breadcrumb = array($this->guide->uri() => 'User Guide', $this->guide->uri(array('module' => $module)) => Kohana::$config->load('userguide.modules.' . $module . '.name'), 'Error'); } else { if (Route::name($this->request->route()) == 'docs/api') { $this->template->menu = Kodoc::menu(); // Bind the breadcrumb $this->template->breadcrumb = array($this->guide->uri(array('page' => NULL)) => 'User Guide', $this->request->route()->uri() => 'API Browser', 'Error'); } else { $this->template->menu = View::factory('userguide/menu', array('modules' => $this->_modules())); $this->template->breadcrumb = array($this->request->route()->uri() => 'User Guide', 'Error'); } } }
/** * Confirm restore password * @param $user_id * @param $user_email * @param $expires * @return bool */ public static function confirm_restore($user_id, $user_email, $expires = Date::DAY) { $config = Kohana::$config->load('app'); $url_confirmed = HTML::anchor(Kohana::$server_name . Route::url(Route::name(Request::$current->route()), ['action' => 'confirmed_restore', 'token' => Model_User_Confirm::token($user_id, Model_User_Confirm::TYPE_RESTORE, $expires)]), 'confirm the password change'); $msg = "<p>Please <strong>{$url_confirmed}</strong> to change your password.</p>"; // Send new password return Mail::sendmail($user_email, $config['site_email'], $config['site_subject'] . ' restore password ' . $config['sitename'], $msg); }
/** * * @param array $routes * @return Routes */ public function fromArray($routes) { $this->clearRoutes(); foreach ($routes as $name => $routeData) { $routeData['name'] = $name; $route = new Route(); $route->fromArray($routeData); if ($route->isValid()) { $this->routes[$route->name()] = $route; } } }
/** * Route should set name and be cached by Router */ public function testRouteSetsNameAndIsCached() { $router = new RouterMock(); $route = new Route('/foo/bar', function () { }); $route->setRouter($router); $route->name('foo'); $cacheKeys = array_keys($router->cache); $cacheValues = array_values($router->cache); $this->assertEquals($cacheKeys[0], 'foo'); $this->assertSame($cacheValues[0], $route); }
protected function _changed_uri($params) { if (is_string($params)) { // assume its an action name $params = array('action' => $params); } $current_params = $this->request->param(); $current_params['controller'] = strtolower($this->request->controller()); $current_params['directory'] = strtolower($this->request->directory()); $current_params['action'] = strtolower($this->request->action()); $params = $params + $current_params; return Route::url(Route::name(Request::current()->route()), $params, TRUE); }
protected function _get_page_links($count, $offset, $limit) { $links = array(); $route = Route::name($this->request->route()); $query = $this->request->query(); $internal = array('kohana_uri' => null, 'oauth_consumer_key' => null, 'oauth_nonce' => null, 'oauth_signature_method' => null, 'oauth_timestamp' => null, 'oauth_token' => null, 'oauth_version' => null, 'oauth_signature' => null); $query = array_diff_key($query, $internal); $query['limit'] = $limit; if ($count > ($query['offset'] = $offset + $limit)) { $links[] = Controller_Resources::to_href('next', $route, $this->request->param(), $query); } if (0 <= ($query['offset'] = $offset - $limit)) { $links[] = Controller_Resources::to_href('previous', $route, $this->request->param(), $query); } return $links; }
protected function init() { $request = $this->request->current(); $this->config = Kohana::$config->load($this->config)->as_array(); $this->acl_init(); $this->site_init(); $helper_acl = new Helper_ACL($this->acl); $a2_config = Kohana::$config->load('admin/a2/base')->as_array(); $helper_acl->inject($a2_config); if (Route::name($request->route()) == 'modules') { $this->module_page_id = (int) $request->query('page'); $this->module_config = empty($this->module_config) ? Helper_Module::code_by_controller($request->controller()) : $this->module_config; $_pages = $this->get_module_pages($this->module_config); if ($_pages->count() > 0) { if ($this->module_page_id == 0) { $this->module_page_id = $_pages->rewind()->current()->id; } foreach ($_pages as $_item) { $_link = URL::base() . Page_Route::dynamic_base_uri($_item->id); $this->module_pages[$_item->id] = $_item->title . " [ {$_link} ]"; } } $this->module_config = Helper_Module::load_config($this->module_config); if (!Kohana::$is_cli) { $config = Arr::get($this->module_config, 'a2'); $helper_acl->inject($config); } if (!$this->acl->is_allowed($this->user, $request->controller() . '_controller', 'access')) { throw new HTTP_Exception_404(); } } $injectors = array(); foreach ($this->injectors as $_key => $_array) { $params = Arr::get($_array, 1); if (class_exists($_array[0])) { $object = new $_array[0]($request, $this->user, $this->acl, $params); $injectors[$_key] = $object; } } $this->injectors = $injectors; unset($injectors); $this->is_cancel = $request->post('cancel') == 'cancel'; $this->back_url = $request->query('back_url'); $this->post_check_empty_files(); $this->post_check_deleted_fields(); }
/** * Collect all data * @static * @return void */ private static function collectData() { if (!Request::$current || Route::name(Request::$current->route()) == self::$_data_collect_current_route) { return; } self::$DATA_APP_TIME = self::getAppTime(); self::$DATA_APP_MEMORY = self::getAppMemory(); self::$DATA_SQL = self::getSql(); self::$DATA_CACHE = self::getCache(); self::$DATA_POST = self::getPost(); self::$DATA_GET = self::getGet(); self::$DATA_FILES = self::getFiles(); self::$DATA_COOKIE = self::getCookie(); self::$DATA_SESSION = self::getSession(); self::$DATA_SERVER = self::getServer(); self::$DATA_ROUTES = self::getRoutes(); self::$DATA_INC_FILES = self::getIncFiles(); self::$DATA_CUSTOM = self::getCustom(); self::$_data_collect_current_route = Route::name(Request::$current->route()); }
/** * Initial data loading */ public static function init() { if (in_array(Route::name(Request::instance()->route), self::$ignored_routes)) { return FALSE; } // don't call init() twice! if (self::$_loaded) { return FALSE; } self::$_session = Session::instance(); // load session data self::$_olddata = self::$_session->get(self::$_session_var, FALSE); // clear session data self::$_session->delete(self::$_session_var); if (self::$_olddata == FALSE) { // create empty array - there is no data self::$_olddata = array(); } self::$_data = self::$_olddata; self::$_loaded = TRUE; }
/** * Setup adapter * * @param Jelly_Builder $source * @param array $config pagination config */ public function __construct(Jelly_Builder $source, array $config = null) { $this->_target = new Pagination(); $this->_source = $source; //merge config $this->_config += $this->_target->config_group(); //merge config if ($config) { $this->_config += $config; } $order_how = isset($this->_config['current_order']) ? Request::current()->query($this->_config['current_order']['key']) : 'DESC'; $this->_direct = $order_how == 'DESC' ? 'ASC' : 'DESC'; $this->_limit = isset($this->_config['current_limit']) ? Request::current()->query($this->_config['current_limit']['key']) ? Request::current()->query($this->_config['current_limit']['key']) : 10 : 10; $this->_sort_column = Request::current()->query($this->_config['current_sort']['key']); $this->_target->setup(array('total_items' => $source->select()->count(), 'items_per_page' => $this->_limit)); if (!is_null($this->_sort_column)) { $this->_source->order_by($this->_sort_column, $order_how); } // Get the current route name $current_route = Route::name(Request::initial()->route()); //Current route $this->_route = Route::get($current_route); }
/** * @return string HTML anchor */ public function render($include_classes = false) { $title = $this->_render_icon() . $this->_config['title']; if ($this->last == true) { return $title; } else { $is_current = Route::name(Request::$initial->route()) == $this->_config['route']; // Apply URL::site if (isset($this->_config['route'])) { $route_params = array(); if (isset($this->_config['route_param']) && count($this->_config['route_param'] > 0)) { foreach ($this->_config['route_param'] as $param) { if ($this->_element->route_params[$this->_config['route']] == null) { if ($is_current) { $route_params[$param] = Request::$initial->param($param); } else { throw new Kohana_Exception('Route parameters aren\'t set for ":route"', array(':route' => $this->_config['route'])); } } else { $route_params[$param] = $this->_element->route_params[$this->_config['route']][$param]; } } } $this->_config['url'] = Route::url($this->_config['route'], $route_params, true); } else { if (!'http://' == substr($this->_config['url'], 0, 7) and !'https://' == substr($this->_config['url'], 0, 8)) { $this->_config['url'] = URL::site($this->_cornfig['url']); } } $attr = ['title' => $this->_config['tooltip']]; if ($include_classes == true) { $attr['class'] = implode(' ', $this->_config['classes']); } return HTML::anchor($this->_config['url'], $title, $attr, NULL, FALSE); } }
#results.fail { background: #911; } </style> <h1>Route Dump</h1> <?php if (count(Route::all()) > 0) { ?> <?php foreach (Route::all() as $route) { ?> <h3><?php echo Route::name($route); ?> </h3> <?php $array = (array) $route; foreach ($array as $key => $value) { $new_key = substr($key, strrpos($key, "") + 1); $array[$new_key] = $value; unset($array[$key]); } ?> <table> <tr> <th>Route uri</th> <td><code><?php echo html::chars($array['_uri']);
/** * Return an array to traverse to possibly get sibling items based on a given route * * @param Route $route * @return array|bool */ public function get_tree_index(Route $route) { $name = Route::name($route); if (isset($this->routes[$name])) { return explode('.', $this->routes[$name]); } return false; }
*/ //Route::set('smarty', '(smarty/(<action>)/(<id>))', array( // 'controller' => '(smartydemo)', //// 'action' => '.+', //// 'id' => '.+', //)) // ->defaults(array( // 'controller' => 'smartydemo', // 'action' => 'index', // )); Route::set('directory', '(<controller>(/<action>(/<id>)))', array('id' => '.+'))->defaults(array('controller' => 'welcome', 'action' => 'index')); //Route::set('directory', '(<controller>(/<action>(/<id>)))') // ->defaults(array( // 'controller' => 'test', // 'action' => 'test123', // )); //Route::set('default', '(<controller>(/<action>(/<id>)))') // ->defaults(array( // 'controller' => 'welcome', // 'action' => 'index', // )); if (Kohana::$environment !== Kohana::PRODUCTION && isset($_GET["debugroute"]) && "true" == $_GET["debugroute"]) { foreach (Route::all() as $route) { /* @var $route Route */ $requesturi = Request::factory(Request::detect_uri()); if ($route->matches($requesturi)) { die("MATCH with " . Route::name($route) . " route"); } } die("NOT route matches <br>"); }
/** * Get the name of a route * @return string */ public function routename() { return Route::name(Request::$current->route()); }
public function doTOC($text) { // Only add the toc do userguide pages, not api since they already have one if (self::$show_toc and Route::name(Request::current()->route()) == "docs/guide") { $toc = View::factory('userguide/page-toc')->set('array', self::$_toc)->render(); if (($offset = strpos($text, '<p>')) !== FALSE) { // Insert the page TOC just before the first <p>, which every // Markdown page should (will?) have. $text = substr_replace($text, $toc, $offset, 0); } } return $text; }
public function error($message) { $this->request->status = 404; $this->template->title = "Userguide - Error"; $this->template->content = View::factory('userguide/error',array('message' => $message)); // Don't show disqus on error pages $this->template->hide_disqus = TRUE; // If we are in a module and that module has a menu, show that if ($module = $this->request->param('module') AND $menu = $this->file($module.'/menu') AND Kohana::config('userguide.modules.'.$module.'.enabled')) { // Namespace the markdown parser Kodoc_Markdown::$base_url = URL::site($this->guide->uri()).'/'.$module.'/'; Kodoc_Markdown::$image_url = URL::site($this->media->uri()).'/'.$module.'/'; $this->template->menu = Markdown($this->_get_all_menu_markdown()); $this->template->breadcrumb = array( $this->guide->uri() => 'User Guide', $this->guide->uri(array('module' => $module)) => Kohana::config('userguide.modules.'.$module.'.name'), 'Error' ); } // If we are in the api browser, show the menu and show the api browser in the breadcrumbs else if (Route::name($this->request->route) == 'docs/api') { $this->template->menu = Kodoc::menu(); // Bind the breadcrumb $this->template->breadcrumb = array( $this->guide->uri(array('page' => NULL)) => 'User Guide', $this->request->route->uri() => 'API Browser', 'Error' ); } // Otherwise, show the userguide module menu on the side else { $this->template->menu = View::factory('userguide/menu',array('modules' => $this->_modules())); $this->template->breadcrumb = array($this->request->route->uri() => 'User Guide','Error'); } }
/** * Display a list of published articles, * filtered by date (year or month), * with pagination * * Set request param `date` to either YYYY/MM or YYYY */ public function action_archive() { Kohana::$log->add(Kohana::DEBUG, 'Executing Controller_Blog::action_archive'); Kohana::$log->add(Kohana::DEBUG, 'Route is '.Route::name($this->request->route)); $this->template->content = View::factory('blog/front/list') ->set('legend', __('Published Articles')) ->bind('articles', $articles) ->bind('pagination', $pagination); $date = $this->request->param('date'); $search = Sprig::factory('blog_search'); $articles = $search->search_by_date($date); $pagination = $search->pagination; }
<?php $request = Request::current() ? Request::current() : Request::initial(); ?> <table class="route-info"> <tr> <th>Route name</th> <th>directory</th> <th>controller</th> <th>action</th> <th>params</th> <th>query string</th> </tr> <tr> <td><?php echo Route::name($request->route()); ?> </td> <td><?php echo $request->directory(); ?> </td> <td><?php echo $request->controller(); ?> </td> <td><?php echo $request->action(); ?> </td> <td class="params">
/** * Preparing feed * * @uses Arr::get * @uses Config::load * @uses Config_Group::get * @uses URL::site * @uses Cache:get * @uses Feed::generator * @uses Request::current * @uses Request::routes */ public function before() { // Get route name (rss|atom) for creating object (Gleez_Rss|Gleez_Atom) $this->_feed_type = Route::name(Request::current()->route()); // Start at which page? $this->_page = (int) $this->request->param('p', 1); // How Many Items Should We Retrieve? // Configurable page size between 1 and 200, default 30 $this->_limit = max(1, min(200, (int) $this->request->param('l', $this->_page_size))); // For example: Term ID or Rag ID $this->_id = (int) $this->request->param('id', 0); // Offset $this->_offset = $this->_page == 1 ? $this->_page : ($this->_page - 1) * $this->_limit; // Getting settings $this->_config = Config::load('site'); // Getting site URL $this->_site_url = $this->_config->get('site_url', URL::site(NULL, TRUE)); // Getting TTL $this->_ttl = $this->_config->get('feed_ttl', Date::HOUR * 60); // Initiate cache $this->_cache = Cache::instance('feeds'); $this->_cache_key = "feed-{$this->request->controller()}-{$this->request->action()}-{$this->_limit}-{$this->_page}-{$this->_id}"; // Fills the array elements $this->_items = $this->_cache->get($this->_cache_key, array()); // Create feed object $this->_feed = Feed::instance($this->_feed_type); // Preparing header for XML document $this->_info = $this->_feed->getInfo(); parent::before(); $this->response->headers('Content-Type', 'text/xml'); if (Kohana::$environment === Kohana::DEVELOPMENT) { Log::debug('Executing Controller: :controller, action: :action', array(':controller' => $this->request->controller(), ':action' => $this->request->action())); } }
<?php $currentRoute = Route::name(Request::current()->route()); ?> <div class="sidebar-nav nav-collapse collapse navbar-collapse"> <ul class="nav main-menu"> <?php $i = 0; foreach ($menu as $name => $route) { ?> <?php if (is_array($route)) { ?> <li class="item<?php echo $i; ?> "> <?php $nameParts = Helpers_Menu::getNameParts($name); ?> <a class="dropmenu" href="javascript:void(0);"><?php echo $nameParts['icon']; ?> <span class="hidden-sm text"> <?php echo $nameParts['name']; ?> </span> <span class="chevron <?php if (in_array($currentRoute, $route)) { ?> opened<?php } else {
public function testName() { $this->assertNull($this->route->name()); $this->assertSame('foo', $this->route->name('foo')); $this->assertSame('foo', $this->route->name()); }
/** * Test whether a URL is the home page * * @param string $route_name The home route name [Optional] * @param array $route_params The home route parameters [Optional] * @return boolean * * @uses Request::initial * @uses Route::name */ public static function is_homepage($route_name = NULL, $route_params = NULL) { // Process the current URL $request = Request::initial(); $name = Route::name($request->route()); $params = $request->param(); $params['action'] = $request->action(); $params['controller'] = $request->controller(); $current = self::canonical($name, $params); // Process the home URL if (empty($route_name)) { $route_name = 'default'; } if (empty($route_params)) { $route_params = array('controller' => 'home'); } $home = self::canonical($route_name, $route_params); return $current === $home; }