# 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`)

```php
<?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()`

```php
<?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()`

```php
<?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()`

```php
<?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 code
* `outputs`: Data in JSON format.

## `success()`

```php
<?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()`

```php
<?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()`

```php
<?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 message
* `return`: Array

## `get_error()`

```php
<?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 message
* `return`: Array

## Example

```php
<?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' => 'php_fast.doe@gmail.com'
        ];

        // Return success response in JSON format
        $this->success($data, 'Data retrieved successfully');
    }
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.cmsfullform.com/documents/core/basecontroller.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
