BaseBlock

The BaseBlock.php file in the Core directory of the PHPFast framework defines an abstract class BaseBlock to manage blocks in the application. Below is a detailed explanation of the methods in this file.

getName() and setName()

<?php
    // Returns the block name, e.g., "HeaderBlock"
    protected function getName(){
        return ucfirst($this->name);
    }
    protected function setName($value){
        $this->name = $value;
    }
  • The getName method returns the block name with the first letter capitalized.

  • The setName method sets the value of the $name property.

getLabel() and setLabel()

<?php
    protected function getLabel(){
        return $this->label;
    }
    protected function setLabel($value){
        $this->label = $value;
    }
  • The getLabel method returns the value of the $label property.

  • The setLabel method sets the value of the $label property.

setProps() and getProps()

<?php
    public function setProps(array $props) {
        $this->props = array_merge($this->props, $props);
        return $this;
    }
    protected function getProps() {
        return $this->props;
    }
  • The setProps method sets the properties for the block by merging the provided $props array with the current $props array.

  • The getProps method returns the properties of the block.

handleData()

<?php
    // Handle data and return it in the format required by the layout file
    abstract public function handleData();

The abstract method handleData is responsible for processing data and returning it in the format required by the layout file. This method will be implemented in the subclasses that extend BaseBlock.

Example

<?php
namespace App\Blocks\Content;

use System\Core\BaseBlock;
use App\Models\UsersModel;

class ContentBlock extends BaseBlock {
    public function __construct() {
        $this->setLabel('Content Block');
        $this->setName('Content');
        $this->setProps([
            // layout name: layout_1.php
            'layout'            => 'layout_1',         // Required
            'other_properties'  => 'Other properties', // Other Properties 
        ]);
        
        $this->usersModel = new UsersModel();
    }
    
    public function handleData() {
        $props = $this->getProps();
        
        $users   = $this->usersModel->getUsers();  
        return [
            'props'    => $props,
            'users'    => $users,
        ];
    }
}

Find out how to use Block

Last updated

Was this helpful?