Barebone\Actions

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:

  1. Action
  2. AJAX
  3. JSON
  4. XML
  5. JQUERY

Action (Default actiontype)

  • This is the default type.
  • Methods ending in "Action()" (e.g., indexAction()).
  • When an action of this actiontype is executed, Barebone renders the corresponding view within the layout defined by your controller.
function indexAction(array $args = []){
    $this->layout->set_layout('bootstrap');
    // this would render the view inside of the layout 'bootstrap'
}    

AJAX

  • This actiontype is used specifically for handling Asynchronous JavaScript and XML (AJAX) requests.
  • Actions of this actiontype return data in a format suitable for JavaScript, typically JSON.
  • This allows for dynamic updates on the client-side (web browser) without full page reloads.

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
}     

JSON

  • This actiontype is dedicated to returning data in JSON format.
  • It's suitable for situations where you need to exchange data between your application and external systems or APIs that expect JSON format.
// 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"}}
}     

XML

  • This actiontype is dedicated to returning data in XML format.
  • It's suitable for situations where you need to exchange data between your application and external systems or APIs that expect XML format.
  • XML elements needs a root element. We use data as the root node.
// 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>
    
}     

JQUERY

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:

  • Facilitates seamless interaction with the DOM (Document Object Model) within your SPA, enabling dynamic updates from the controller using pure PHP.
  • Enhances the development process for SPAs by leveraging familiar jQuery syntax within your controller actions.

Watch a video demonstrating this action type.

Key Points:

  • Integration with jquery-php.js: It necessitates the inclusion of the jquery-php.js library within your layout to bridge the gap between PHP and jQuery.
  • Automatic Execution: Barebone automatically executes the 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.
  • DOM Manipulation: You can perform actions on your existing page elements using jQuery-like syntax directly within your PHP controller code. This eliminates the need for extensive JavaScript code and simplifies DOM updates.

Example:

View (indexAction):

HTML

<div id="response-showpost"></div> <button onclick="$.php('/application/controller/showpost');">Click me!</button>

Controller (indexController):

PHP
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:

  1. The button click in the view triggers a POST request to the showpostJQUERY() controller action.
  2. Barebone executes Barebone\jQuery::init(); to prepare the environment for jQuery-like interactions.
  3. The 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.
  4. Finally, Barebone executes Barebone\jQuery::getResponse(); to retrieve the modified DOM content and send it back to the browser for rendering.

Remember:

  • Choose the JQUERY action actiontype when you require dynamic DOM manipulation within your SPAs using jQuery-like syntax within your controller code.
  • Ensure inclusion of the jquery-php.js library in your layout for this functionality.
  • Barebone\ActionTypes\Action

    Barebone\ActionTypes\Action extends Barebone\ActionTypes\Abstracts\AbstractAction
    • [M] __construct( string $method ) : void
    • [M] __toString() : string
    • [M] run( string $classname, array $params ) : void
    • Barebone\ActionTypes\Abstracts\AbstractAction

      Barebone\ActionTypes\Abstracts\AbstractAction
      • [P] $method : string
      • [M] setMethod( string $method ) : void
      • [M] getMethod() : string
      • [M] canrun( string $classname ) : void
  • Barebone\ActionTypes\AJAX

    Barebone\ActionTypes\AJAX extends Barebone\ActionTypes\Abstracts\AbstractAction
    • [M] __construct( string $method ) : void
    • [M] __toString() : string
    • [M] run( string $classname, array $params ) : void
    • Barebone\ActionTypes\Abstracts\AbstractAction

      Barebone\ActionTypes\Abstracts\AbstractAction
      • [P] $method : string
      • [M] setMethod( string $method ) : void
      • [M] getMethod() : string
      • [M] canrun( string $classname ) : void
  • Barebone\ActionTypes\JQUERY

    Barebone\ActionTypes\JQUERY extends Barebone\ActionTypes\Abstracts\AbstractAction
    • [M] __construct( string $method ) : void
    • [M] __toString() : string
    • [M] run( $classname, $params ) : void
    • Barebone\ActionTypes\Abstracts\AbstractAction

      Barebone\ActionTypes\Abstracts\AbstractAction
      • [P] $method : string
      • [M] setMethod( string $method ) : void
      • [M] getMethod() : string
      • [M] canrun( string $classname ) : void
  • Barebone\ActionTypes\JSON

    Barebone\ActionTypes\JSON extends Barebone\ActionTypes\Abstracts\AbstractAction
    • [M] __construct( string $method ) : void
    • [M] __toString() : string
    • [M] run( string $classname, array $params ) : void
    • Barebone\ActionTypes\Abstracts\AbstractAction

      Barebone\ActionTypes\Abstracts\AbstractAction
      • [P] $method : string
      • [M] setMethod( string $method ) : void
      • [M] getMethod() : string
      • [M] canrun( string $classname ) : void
  • Barebone\ActionTypes\XML

    Barebone\ActionTypes\XML extends Barebone\ActionTypes\Abstracts\AbstractAction
    • [M] __construct( string $method ) : void
    • [M] __toString() : string
    • [M] run( string $classname, array $params ) : void
    • Barebone\ActionTypes\Abstracts\AbstractAction

      Barebone\ActionTypes\Abstracts\AbstractAction
      • [P] $method : string
      • [M] setMethod( string $method ) : void
      • [M] getMethod() : string
      • [M] canrun( string $classname ) : void