The Bug Genie
The Bug Genie
star_faded.png
Please log in to subscribe to updates for this article
The Bug Genie:Development:Response
Last updated at Tue Aug 23 17:37, by zegenie

Introduction  ⇑ top

Manipulating or customizing the response is rarely ever limited to simply displaying variables and/or looping over predefined content. Often you will find yourself wanting to add RSS feeds, javascript or css; setting a custom header title; changing the selected context tab, etc. Luckily, the response object provides functionality for all these common scenarios - and more.

Depending on where you're at, there are several methods of retrieving the current response object.

In templates:
// Set a custom page title
$tbg_response->setTitle('My fancy page');


From the controller / actions:
// Set the response http status code to 401 (unauthorized)
$this->getResponse()->setHttpStatus(401);


From any other location:
// Retrieve the current template
$current_template = TBGContext::getResponse()->getTemplate();


When you have retrieved an instance of the response object, the TBGResponse class provides an abundance of functionality to manipulate the response sent back to the user or browser. We'll use all the different ways of accessing the response object in these examples.

Things you'll do a lot  ⇑ top

Set a custom page title  ⇑ top

To set a custom title, use the setTitle() method. The title will be appended to the default "The Bug Genie ~ " string that always appears.
// Sets the title to "The Bug Genie ~ My fancy page"
$tbg_response->setTitle('My fancy page');
echo $tbg_response->getTitle();


Add a custom header  ⇑ top

You can add a header to the response anywhere in your code. To do this, use the addHeader() function. Because of the way output buffering works with The Bug Genie, never use the php header() function.
// Mark the response as a download with a specified filename
TBGContext::getResponse()->addHeader('Content-Disposition: attachment; filename="issues.csv"');


Add a custom javascript or css file  ⇑ top

You can add custom javascript files using the addJavascript() method. This method takes a second parameter to specify if it should be included in the content server (minifies it and improves caching performance), and a third parameter to specify whether it should go first in the loaded javascript list.
// Add a javascript to the list of javascripts to be included
$tbg_response->addJavascript('js/functions.js');

// Add a javascript to the list of javascripts to be included
// also specify that it shouldn't be ran through the content server
$tbg_response->addJavascript('js/whitespaceaware.js', false);

// Add a javascript file to the top of the javascript list
$tbg_response->addJavascript('js/lib.js', true, true);


You can also add custom css files. This method takes the same parameters with the same functionality as the addJavascript() method.
// Add a stylesheet to the list of stylesheets to be included
$tbg_response->addStylesheet('css/reset.css');

// Add a stylesheet to the list of stylesheets to be included
// also specify that it should not be ran through the content server
$tbg_response->addStylesheet('css/reset.css', false);

// Add a stylesheet to the top of the list of stylesheets to be included
$tbg_response->addStylesheet('css/reset.css', true, true);


Setting / removing a cookie  ⇑ top

Since manipulating cookies is part of the response sent back to the browser / user, this is a natural part of the response method set.
// Send a cookie to the user
TBGContext::getResponse()->setCookie('show_popups', false);

// Delete a cookie
TBGContext::getResponse()->deleteCookie('inactive');


Set the selected page tab  ⇑ top

The Bug Genie uses page tab for the layout. You can specify which tab is suppose to be selected using a response method.
// Select the "wiki" tab
$tbg_response->setPage('wiki');


Things you'll may want to do at some point in time  ⇑ top

Add breadcrumbs to the breadcrumb bar  ⇑ top

You can add items to the breadcrumb bar, as well as specifying what item is selected.
// Add an item called "something" to the breadcrumb bar, no link
$tbg_response->addBreadcrumb('Something');

// Add a link called "this page" to the breadcrumb bar
$tbg_response->addBreadcrumb('this page', make_url('route_to_this_page'));

// Add a link called "this page" with a list of other pages available as a submenu
// If there is a link in the submenu with the same title as the title added as the
// first parameter, it will be marked as selected in the submenu when it is opened
$tbg_response->addBreadcrumb('this page', make_url('route_to_this_page'), array(
  array('url' => make_url('some_page'), 'title' => 'some page'),
  array('url' => make_url('some_other_page'), 'title' => 'some other page'),
  array('url' => make_url('another_page'), 'title' => 'another page'),
  array('url' => make_url('route_to_this_page'), 'title' => 'this page'),
));


Use a custom decoration  ⇑ top

Maybe you want to integrate The Bug Genie more into your company's website, a forum or some other website. Use the custom decoration method to achieve this.
// Use no decoration, only the main template. Excludes js, css, etc
TBGContext::getResponse()->setDecoration(TBGResponse::DECORATE_NONE);

// Use no footer decoration
TBGContext::getResponse()->setDecoration(TBGResponse::DECORATE_HEADER);

// Use a custom header and footer decoration file.
TBGContext::getResponse()->setDecoration(TBGResponse::DECORATE_CUSTOM, array(
  'header' => '/path/to/some/custom/header.html',
  'footer' => '/path/to/some/custom/footer.html'
));



Article attachments

There are no file attached to this article

Article comments (2)