/** * Object ID based routing. URLs like /controller_name/object_id/method_name/ where object_id is numeric. * Call from index() like this: * if ($this->route_object(args...)) { * return; * } * @param string $model Model class to attempt to load * @param string $default Default method to run if URL is /controller_name/object_id/ * @param \jmvc\Model &$obj Reference return of found object * @param function $filter_callback Object will be passed to this function; if the function returns false, a 404 will be triggered * @return bool Whether an object was found or not */ public function route_object($model, $default = false, &$obj = null, $filter_callback = null) { if (!is_numeric($this->args[0])) { return false; } $method = $this->args[1] ?: $default; if (!method_exists($this, $method)) { \jmvc::do404(); } $obj = $model::factory($this->args[0]); if (!$obj) { \jmvc::do404(); } if ($filter_callback && !$filter_callback($obj)) { \jmvc::do404(); } $this->view_override(array('view' => $method)); $this->{$method}($obj); return true; }
/** * Render a view without attempting to call a controller. Similar to render(). * @param mixed $view_name * @param array $args * @return void */ public static function render_static($view_name = null, $args = array()) { $context = self::push_context($view_name, $parent); if (method_exists('jmvc\\Controller', $context['view'])) { \jmvc::do404(); } if (!empty($parent)) { $args['parent'] = $parent; } if ($view_file = self::exists($context)) { ob_start(); include $view_file; $output = ob_get_clean(); } else { throw new \ErrorException('Can\'t find view. View: ' . $context['view'] . ', Controller: ' . $context['controller'] . ', Template: ' . $context['template'] . ', Site: ' . $context['site']); } self::pop_context($view_name); return $output; }
/** * Compile and concatinate LESS and CSS * @return void */ protected static function css() { // for CDN compatibility, args are base64 encoded in the URL parse_str(base64_decode(substr(CURRENT_URL, 5, -1)), $args); if (!empty($args['files'])) { $files = explode(',', $args['files']); } if (!is_array($files)) { exit; } $last_change = 0; foreach ($files as $file) { $file = APP_DIR . '../www' . $file; if (!file_exists($file)) { \jmvc::do404(); } $last_change = max($last_change, filemtime($file)); } // see if we can serve from cache $r = \jmvc::redis(); $key = 'JMVC:css:' . md5(serialize($files) . $last_change); $css_out = $r->get($key); if (!$css_out || $args['nocache']) { foreach ($files as $file) { if (substr($file, -4) == 'less') { $lc = new \jmvc\classes\Lessc(APP_DIR . '../www' . $file); $css = $lc->parse(); } else { $css = file_get_contents(APP_DIR . '../www' . $file); } $out[] = "\n\n\n/*** " . $file . " ***/\n\n" . $css; } $css_out = implode(' ', $out); $r->setex($key, 3600, $css_out); } header('Content-type: text/css'); header('Cache-Control: max-age=31556926, public'); header('Expires: ' . date('r', time() + 31536000)); echo $css_out; exit; }