/** * Routing defaults * * app: Admin * class: Admin * method: user() * args: /my/extra/args * domain: meagr.com * subdomain: member. * * @return array */ public static function routes() { return array('home' => array('uri' => '__HOME__', 'pattern' => '\\Meagr\\Controller::GET_index'), '404' => array('uri' => '__404__', 'pattern' => '\\Meagr\\Controller::GET_404'), 'mvc' => array('uri' => '{domain}/{class}/{method}/', 'pattern' => '\\{modules}\\{controllers}\\{class}::{method}'), 'hmvc' => array('uri' => '{domain}/{class}/{method}/', 'pattern' => '\\{modules}\\{class}\\{controllers}\\{class}::{method}'), 'subcontroller' => array('uri' => '{domain}/{class}/{method}/', 'pattern' => '\\{modules}\\{class}\\{controllers}\\{subclass}::{submethod}'), 'subdomain' => array('uri' => '{subdomain}.{domain}/{class}/{method}/', 'pattern' => '\\{modules}\\{subdomain}\\{class}\\{controllers}\\{class}::{method}', 'filter' => function ($object, $args = null) { // instantiate our router singleton $router = Router::init(); //check if there is a {class} value set if ($object->routeMapKeyExists('{class}') === false or $router->getRouteMap('{class}') == '/') { //get the subdomain thats been set in the url and the config $subdomain = $router->getRouteMap('{subdomain}'); //if not, set it to the subdomain $router->setRouteMap('{class}', $subdomain); //lets not double up $router->setRouteMap('{subdomain}', ''); //make the pattern namespace compatible $object = Router::namespaceRoutePattern($object); //update the pattern $object = $router->translatePattern($object); //set the class back to empty $router->setRouteMap('{class}', ''); $object->uri_mapped = rtrim($object->uri_mapped, '/'); } //return our object return $object; }), 'pages' => array('uri' => '{domain}/{args}', 'pattern' => '\\{modules}\\{controllers}\\Page::{args}', 'filter' => function ($object, $args = null) { //if we have no args, just return as we dont need to do anything if (!is_array($args) or empty($args)) { return $object; } // instantiate our router singleton $router = Router::init(); //if we have arguments, get the first $page = $args[0]; //create the string $args_string = implode('/', $args); //unset the first unset($args[0]); //reorder the remaining sort($args); //set the router route map arg to be our page $router->setRouteMap('{args}', $page); //translate the pattern $object = $router->translatePattern($object); //namespace the pattern $object = Router::namespaceRoutePattern($object); //trim the uri $object->uri_mapped = rtrim($object->uri_mapped, '/'); //put the args back on the url for matching $router->setRouteMap('{args}', $args_string); //...and translate $object = $router->translateUri($object); //set the new pattern, minus the argument string, instead use the first argument $object->setMappedPattern(str_replace($args_string, $page, $object->getMappedPattern())); $object->setMappedUri($object->uri_mapped); //return return $object; })); }
/** * set the body of content to be sent to the browser, * buffer all output and store in $this->body * * @return object */ function body() { //if we already have content if (!empty($this->body) or $this->cache_exists) { //return self and move on return $this; } //get our route object $route = $this->router->route; //properly capitalise our namespace $route = Router::namespaceRoutePattern($route); //get our arguments $args = $this->router->arguments ?: array(); //get our class and method list($class, $method) = explode('::', $route->getMappedPattern()); \Meagr\Debug::init('log')->add(array('message' => 'Route: ' . $class . ' ' . $method, 'class' => __METHOD__, 'status' => 'success', 'backtrace' => Debug::backtrace())); //start our buffering ob_start(); //our before function call_user_func_array(array($class, '__before'), $args); //call our function and class call_user_func_array(array($class, $method), $args); //our after function call_user_func_array(array($class, '__after'), $args); //assign the buffer to our body variable $this->body = ob_get_contents(); //finish buffering and clean the output ob_end_clean(); //allow for chaining return $this; }