Blocks
To manage and organize the interface of a complex website, PHPFast Framework will use the method of breaking the interface into independent Blocks such as head
, header
, content
, footer
, etc. This approach makes it easier to manage and reuse these interface components when needed.
With this approach, the separation of interface and logic becomes clear, enhancing reusability, maintainability, and scalability in the future.
Block Structure
ROOT
├── application/ # Application-specific files
│ ├── Blocks/ # UI blocks (footer, header, content)
│ │ ├── Footer/ # Footer block
│ │ ├── Head/ # Head block (meta, CSS, JS)
│ │ ├── Header/ # Header block (nav, logo)
│ │ └── Content/ # Content block
│ │ ├── Views/ # Content layouts
│ │ │ ├── layout_1.php # Layout 1
│ │ │ ├── layout_2.php # Layout 2
│ │ │ └── layout_3.php # Layout 3
│ │ └── ContentBlock.php # Content block logic
│ └── Views/ # Views for frontend and backend
│ ├── Backend/ # Backend views
│ └── Frontend/ # Frontend views (assets, components)
│ ├── Assets/ # Frontend assets (CSS, JS)
│ │ ├── css/ # CSS files
│ │ └── js/ # JS files
│ ├── Component/ # Reusable components (e.g., menu, search)
│ │ ├── menu.php # Menu component
│ │ └── search.php # Search component
│ └── home_index.php # Homepage view
└── system/ # Core framework files
├── Core/ # Core classes for block handling
│ └── BaseBlock.php # Base class for blocks
└── Libraries/ # Helper libraries (rendering, etc.)
└── Render.php # Class for rendering views
Create Structure
To be able to create a folder automatically, you can learn about it through
BlockCommand
.Or you can create the folder structure in the manual way.
Call Blocks
Example in home_index.php
<?php
namespace System\Libraries;
?>
<?php Render::block('head'); ?>
<body>
<div class="container-full">
<?php Render::block('content'); ?>
</div>
<?php Render::block('footer'); ?>
</body>
block()
method in Render.php
: The block()
function receives a block name as a parameter, dynamically constructs the corresponding class name and its path, then processes the data based on predefined properties and delivers it to the specified layout.
Process
In ContentBlock.php
Extends from BaseBlock
to use the pre-defined methods.
<?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
]);
$this->usersModel = new UsersModel();
}
public function handleData() {
$users = $this->usersModel->getUsers();
return $users;
}
}
In the ContentBlock
class, the methods setLabel()
, setName()
, and setProps()
are mandatory when initializing a block.
setLabel()
: Defines a user-friendly label for the block.setName()
: Sets the internal name used to identify the block in the system.setProps()
: Specifies essential properties such as the required layout file.
These methods ensure that every block is properly defined, named, and configured, making it easier to manage and integrate into different layouts.
The handle()
method is responsible for processing data and returning the result to the layout defined in setProps()
. It serves as the core logic handler for the block, ensuring that the necessary data is prepared before rendering.
If a layout is specified in setProps()
, the method processes data accordingly and returns it to the defined layout. However, if no layout is set, the system will automatically use the default file default.php
to return the data. This ensures flexibility, allowing blocks to adapt dynamically while maintaining a fallback mechanism for handling data efficiently
Display data
In layout_1.php
:
<?php foreach ($users as $user): ?>
<span>ID: <?= $user['id'] ?> </span>
<span>Username: <?= $user['username'] ?> </span>
<span>Email: <?= $user['email'] ?> </span>
<?php endforeach; ?>
home_index.php
will get the HTML from layout_1.php
Last updated
Was this helpful?