Habari\URL::get PHP Method

get() public static method

URL::get( 'display_entries_by_date', array( 'year' => '2000', 'month' => '05', 'day' => '01', ) );
public static get ( mixed $rule_names = '', mixed $args = [], boolean $useall = true, boolean $noamp = false, boolean $prepend_site = true ) : string
$rule_names mixed string name of the rule or array of rules which would build the URL
$args mixed (optional) array or object of placeholder replacement values
$useall boolean If true (default), then all passed parameters that are not part of the built URL are tacked onto the URL as querystring
$noamp boolean
$prepend_site boolean If true (default), a full URL is returned, if false, only the path part of the URL is returned
return string
    public static function get($rule_names = '', $args = array(), $useall = true, $noamp = false, $prepend_site = true)
    {
        $f_args = self::extract_args($args);
        $f_args = Plugins::filter('url_args', $f_args, $args, $rule_names);
        $url = URL::instance();
        if ($rule_names == '') {
            // Retrieve current matched RewriteRule
            $selectedrule = $url->get_matched_rule();
            // Retrieve arguments name the RewriteRule can use to build a URL.
            $rr_named_args = $selectedrule->named_args;
            $rr_args = array_merge($rr_named_args['required'], $rr_named_args['optional']);
            // For each argument, check if the handler_vars array has that argument and if it does, use it.
            foreach ($rr_args as $rr_arg) {
                if (!isset($f_args[$rr_arg])) {
                    $rr_arg_value = Controller::get_var($rr_arg);
                    if ($rr_arg_value != '') {
                        $f_args[$rr_arg] = $rr_arg_value;
                    }
                }
            }
        } else {
            $url->load_rules();
            $selectedrule = null;
            if (!is_array($rule_names)) {
                $rule_names = array($rule_names);
            }
            foreach ($rule_names as $rule_name) {
                if ($rules = $url->rules->by_name($rule_name)) {
                    $rating = null;
                    foreach ($rules as $rule) {
                        $newrating = $rule->arg_match($f_args);
                        // Is the rating perfect?
                        if ($rating == 0) {
                            $selectedrule = $rule;
                            break;
                        }
                        if (empty($rating) || $newrating < $rating) {
                            $rating = $newrating;
                            $selectedrule = $rule;
                        }
                    }
                    if (isset($selectedrule)) {
                        break;
                    }
                }
            }
        }
        if ($selectedrule instanceof RewriteRule) {
            $return_url = $selectedrule->build($f_args, $useall, $noamp);
            if ($prepend_site) {
                return Site::get_url('site', true) . $return_url;
            } else {
                return $return_url;
            }
        } else {
            $error = new \Exception();
            $error_trace = $error->getTrace();
            // Since URL::out() calls this function, the index 0 is URL::get() which is not the proper failing call.
            if (isset($error_trace[1]['class']) && isset($error_trace[1]['function']) && $error_trace[1]['class'] == 'URL' && $error_trace[1]['function'] == 'out') {
                $error_args = $error_trace[1];
            } else {
                $error_args = $error_trace[0];
            }
            EventLog::log(_t('Could not find a rule matching the following names: %s. File: %s (line %s)', array(implode(', ', $rule_names), $error_args['file'], $error_args['line'])), 'notice', 'rewriterules', 'habari');
        }
    }

Usage Example

Example #1
0
 /**
  * Accepts an Atom entry for insertion as a new post.
  */
 public function post_collection()
 {
     if ($this->is_auth(true)) {
         $bxml = file_get_contents('php://input');
     }
     $xml = new SimpleXMLElement($bxml);
     $post = new Post();
     Plugins::act('atom_post_collection', $xml, $post, $this->handler_vars);
     if ((string) $xml->title != '') {
         $post->title = $xml->title;
     }
     if ((string) $xml->id != '') {
         $post->guid = $xml->id;
     }
     if ((string) $xml->content != '') {
         $post->content = (string) $xml->content;
     }
     if ((string) $xml->pubdate != '') {
         $post->pubdate = (string) $xml->pubdate;
     }
     // Save categories as tags
     $atom_ns = $xml->children('http://www.w3.org/2005/Atom');
     $categories = $atom_ns->category;
     if (!empty($categories)) {
         $terms = array();
         foreach ($categories as $category) {
             $category_attrs = $category->attributes();
             $terms[] = (string) $category_attrs['term'];
         }
         $post->tags = $terms;
     }
     if (isset($_SERVER['HTTP_SLUG'])) {
         $post->slug = $_SERVER['HTTP_SLUG'];
     }
     // Check if it's a draft (using XPath because Namespaces are easier than with SimpleXML)
     $xml->registerXPathNamespace('app', 'http://www.w3.org/2007/app');
     $draft = $xml->xpath('//app:control/app:draft');
     if (is_array($draft) && (string) $draft[0] == 'yes') {
         $post->status = Post::status('draft');
     } else {
         $post->status = Post::status('published');
     }
     $post->user_id = $this->user->id;
     $post->insert();
     header('HTTP/1.1 201 Created', true, 201);
     header('Status: 201 Created');
     header('Location: ' . URL::get('atom_entry', array('slug' => $post->slug)));
     $this->get_entry($post->slug);
 }