Introduction:
In Barebone, controller methods, also known as actions, they can have different types. Each actiontype dictates the format of the resulting output, influencing how Barebone handles the response. Make sure you read the controller documentation also because actions and controller go hand in hand. Here's a detailed breakdown of these action types with code examples for each:
Action Types and Their Roles:
indexAction()).
function indexAction(array $args = []){
$this->layout->set_layout('bootstrap');
// this would render the view inside of the layout 'bootstrap'
}
This actiontype should be used in combination with the "jquery-php.js" library altough the actiontypeJQEURY is better, but also with a standard xmlhttprequest will work.
The purpose for this actiontype is to interact with ajax calls to the controllers. See it in action in this demo video.
function showpostAJAX(array $args = []){
// this would render nothing
}
// this would render the view as JSON
function showpostJSON(array $args = []){
// assign some variable to the view
$this->view->post = ['title'=>'Blog title', 'body'=>'Hello world'];
// would render
// Content-Type: application/json
{"post":{"title":"Blog title","body":"Hello world"}}
}
// this would render the view as JSON
function showpostXML(array $args = []){
// assign some variable to the view
$this->view->post = ['title'=>'Blog title', 'body'=>'Hello world'];
// would render
// Content-Type: application/xml
<?xml version="1.0"?>
<data><post><title>Blog title</title><body>Hello world</body></post></data>
}
The JQUERY actiontype in Barebone is specifically designed to streamline development of single-page applications (SPAs) utilizing the jQuery JavaScript library. Here's a breakdown of its functionality, incorporating your provided examples:
Purpose:
Watch a video demonstrating this action type.
Key Points:
jquery-php.js: It necessitates the inclusion of the
jquery-php.js library within your layout to bridge the gap between PHP and jQuery.Barebone\jQuery::init(); function before your showpostJQUERY() action and the
Barebone\jQuery::getResponse(); function right after. This ensures proper setup and retrieval of data for manipulation.Example:
View (indexAction):
<div id="response-showpost"></div> <button onclick="$.php('/application/controller/showpost');">Click me!</button>
Controller (indexController):
use Barebone\Controller;
class indexController extends Controller {
function init() {
// Set the layout
$this->layout->set_layout('default');
// Add the jquery-php library
$this->layout->add_javascript('/assets/thirdparty/jquery-php.js');
}
function indexAction(array $args = []) {
// This code would be executed for the "Action" type,
//and the resulting view would be rendered within the layout, including the button.
}
function showpostJQUERY(array $args = []) {
// This code would be executed for the "JQUERY" actiontype triggered by the button click.
// Simulate updating the content of the div
Barebone\jQuery('#response-showpost')->html('Hello world.');
}
}
Explanation:
showpostJQUERY() controller
action.Barebone\jQuery::init(); to prepare the environment for jQuery-like
interactions.showpostJQUERY() action updates the content of the div element with the ID
"response-showpost" using Barebone\jQuery('#response-showpost')->html('Hello world.');, achieving the same effect as you would
with jQuery in JavaScript.Barebone\jQuery::getResponse(); to retrieve the modified DOM content and
send it back to the browser for rendering.Remember:
jquery-php.js library in your layout for this functionality.