Dev:Working with URLs

From Habari Project

Jump to: navigation, search


Often you'll want to respond to requests to a specific URL, or customize how Habari behaves when an existing URL is requested.

The simplest way for a plugin to respond to a specific URL.

/**
 * Action to run when this plugin is initialized, every page load when the plugin is active.
 *
 * Add template and create rewrite rule
 */
public function action_init()
{
  // Template is called 'blog', and is loaded from the plugin's director.
  $this->add_template('blog', dirname(__FILE__) . '/blog.php');
  // Add a rewrite rule called blog, with display_blog action.
  $this->add_rule('"blog"', 'display_blog');
}
 
/**
 * Handle display_blog action
 *
 * Rewrite rule above causes this action to run.
 */
public function action_plugin_act_display_blog( $handler )
{
  $theme = $handler->theme;
 
  // Add variables to the theme. These can be accessed as $post, for example
  // See http://wiki.habariproject.org/en/Dev:Retrieving_Posts for examples of how to retrieve posts.
  $theme->posts = Posts::get(array('status' => Post::status('published')));
 
  // If there is a template called 'blog' in the current theme, that will be used instead.
  $theme->display('blog');
}
Personal tools