BaseController
The BaseController
class serves as the foundational controller in the framework, providing common functionalities and utilities that can be inherited by other controllers. This class typically includes methods for handling requests, rendering views, and managing session data. By centralizing these common tasks, the BaseController
helps to reduce code duplication and streamline the development process.
Constructor (__construct
)
__construct
)<?php
public function __construct() {
// Common initializations for all controllers
// Example: load helpers, libraries, check session, etc.
}
Initializes common settings for all controllers, such as loading helpers, libraries, and checking sessions.
data()
data()
<?php
public function data($key, $value = null) {}
Sets or gets data.
If two parameters are passed, it sets the data.
If one parameter is passed, it gets the data.
$key
: Name of the data$value
: Value of the data (if any)return
: Returns the data if only one parameter is passed.
render()
render()
<?php
protected function render($layout, $view, $isreturn = false) {}
Renders a specified layout and view.
$layout
: Name of the layout.$view
: Name of the view.$isreturn
Whether to return the rendered content or echo it.
json()
json()
<?php
protected function json($data = [], $statusCode = 200) {}
Formats data as a JSON response, making it suitable for APIs and AJAX requests. It allows setting a response payload and an HTTP status code, ensuring proper API communication.
$data
: Data to be returned.$statusCode
: HTTP status codeoutputs
: Data in JSON format.
success()
success()
<?php
protected function success($data = [], $message = 'Success') {}
Generate standardized JSON responses for successful operations.
$data
: Data to be returned.$message
: Success message.outputs
: Data in JSON format.
error()
error()
<?php
protected function error($message = 'An error occurred', $errors = [], $statusCode = 400) {}
Generate standardized JSON responses for errors
$message
: Error message.$errors
: Array of errors.$statusCode
: HTTP status code.outputs
: Data in JSON format.
get_success()
get_success()
<?php
protected function get_success($data = [], $message = 'Success') {}
Same purpose as
success()
, but instead of returning JSON, it returns responses as arrays.$data
: Data to be returned.$message
: Success messagereturn
: Array
get_error()
get_error()
<?php
protected function get_error($message = 'An error occurred', $errors = [], $statusCode = 400) {}
Same purpose as
error()
, but instead of returning JSON, it returns responses as arrays.$data
: Data to be returned.$message
: Success messagereturn
: Array
Example
<?php
namespace App\Controllers;
use System\Core\BaseController;
class HomeController extends BaseController {
public function index() {
// Set some data to be passed to the view
$this->data('title', 'Home Page');
$this->data('content', 'Welcome to PHPFast Framework!');
// Render the view
$this->render('backend', 'backend/home/index');
}
public function getData() {
// Example data
$data = [
'name' => 'PHPFast Framework',
'email' => '[email protected]'
];
// Return success response in JSON format
$this->success($data, 'Data retrieved successfully');
}
}
Last updated
Was this helpful?