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
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
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()

  • 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()

  • 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()

  • Generate standardized JSON responses for successful operations.

  • $data: Data to be returned.

  • $message: Success message.

  • outputs: Data in JSON format.

error()

  • Generate standardized JSON responses for errors

  • $message: Error message.

  • $errors: Array of errors.

  • $statusCode: HTTP status code.

  • outputs: Data in JSON format.

get_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()

  • 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

Last updated

Was this helpful?