abstract class api_controller extends abstract_api_controller

Public SDK API Controller.

Extend this class to expose JSON-first endpoints that leverage the framework routing and middleware without touching Moodle globals directly.

Traits

Trait handling authentication and authorization logic for controllers.

Trait handling Moodle Form interactions within controllers.

Trait handling Moodle $PAGE, Context, Output and Layouts.

Properties

protected bool $require_login from  interacts_with_auth
protected bool $require_sesskey from  interacts_with_auth
protected bool $required_login from  interacts_with_auth
protected array $capabilities from  interacts_with_auth
Request $request from  interacts_with_auth
mixed $course from  interacts_with_page
mixed $cm from  interacts_with_page
protected object|string|null $form from  interacts_with_forms
protected mixed $formparams from  interacts_with_forms
protected null|context $context from  interacts_with_page
protected string $page_layout from  interacts_with_page
protected string $page_title from  interacts_with_page
protected string $page_heading from  interacts_with_page
protected moodle_url|string|null $page_url from  interacts_with_page
protected array $page_navbar from  interacts_with_page
protected string $admin_section from  interacts_with_page
protected ContainerInterface $container from  abstract_controller
protected array<string,mixed> $params from  abstract_controller
protected array<string,mixed> $payload from  abstract_controller

Methods

void
set_require_login(mixed $course = null, mixed $cm = null)

Set if login is required and set related options.

void
set_require_sesskey(bool $require = true)

Define the requirement of sesskey validation for non-idempotent requests.

void
set_require_capabilities(array $capabilities)

Define the capabilities that the user must have.

void
require_login()

Ensure the user is logged in if required.

void
check_capabilities()

Check if the user has the required capabilities.

void
set_form(object|string $form, mixed $formparams = null)

Set the form for the controller to handle.

bool
handle_form_submission()

Check if the bound form was submitted and validated.

mixed
process_form_submission()

Return submitted form data when valid, otherwise false.

bool
process_form_cancel()

Determine if the form has been cancelled.

bool
is_form_submitted()

Check if the form was submitted (regardless of validation).

string
render_form_html()

Internal helper to render the form and return HTML.

void
set_context(context|null $context = null)

Set the context for the controller.

context
get_context()

Get the resolved context (defaults to system).

void
set_page_url(moodle_url|string $url)

Set the page URL.

void
set_page_layout(string $layout)

Set Moodle page layout.

void
set_page_title(string $title)

Set page title.

void
set_page_heading(string $heading)

Set page heading.

void
add_page_navbar(array|string $item)

Add an item to the page navbar trail.

void
set_url_from_route(string $route, array $parameters = [], int $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH)

Helper to set URL from route name.

void
resolve_context()

Resolves the context if null, based on course/cm properties.

void
setup_moodle_page()

Apply all settings to the global $PAGE object.

moodle_url
get_page_url()

Get the Moodle page URL, resolving defaults when needed.

void
set_container(ContainerInterface $container)

Set the container for dependency injection.

void
set_request(Request $request)

Inject the current HTTP request instance.

void
handle()

Lifecycle Hook: Execute common setup logic.

void
pre_handle()

Pre-handle hook.

mixed
get_service(string $service_name)

Retrieve a service from the container safely.

Response
response(mixed $data, int $status = Response::HTTP_OK)

Return a generic response based on the provided data and status code.

JsonResponse
json_response(mixed $data, int $status = Response::HTTP_OK)

Return a JSON response with 'success' wrapper.

bool
is_json()

Check if the request expects a JSON response.

bool
is_inertia_request()

Check if the incoming request is an Inertia navigation.

Response
render(object|string $content = '', string|null $component = null)

Render a Moodle view (HTML response) or widget.

Response
render_from_widget(string $vue_component, array $props = [], string $component = middag_helper::COMPONENT_NAME)

Render a widget using a Moodle renderer.

Response
inertia(string $component, array $props = [])

Generates an Inertia response for the specified component and props.

Response
inertia_location(string $route, array $params = [])

Generates an Inertia location response.

Response
render_from_renderer(renderable $widget, string $component = middag_helper::COMPONENT_NAME)

Render a renderable widget using a Moodle renderer component.

Response
render_from_template(string $templatename, array $templatecontext = [])

Render content using Moodle mustache templates.

Response
error_page(string $message, int $status = Response::HTTP_BAD_REQUEST)

Override error page to return JSON.

moodle_url
url_generator(string $route, array $parameters = [], int $reference_type = UrlGeneratorInterface::ABSOLUTE_PATH)

Generates a URL from the given parameters.

RedirectResponse
redirect(moodle_url|string $url, int $status = Response::HTTP_FOUND)

Helper to perform a redirect.

RedirectResponse
redirect_to_route(string $route, array $parameters = [], int $status = Response::HTTP_FOUND)

Redirect to a named route with fallback support.

JsonResponse
error_response(string $message, int $status = Response::HTTP_BAD_REQUEST, mixed $debug = null)

Send a standardized JSON error response.

JsonResponse
created(mixed $data = [])

Helper for 201 Created.

Response
no_content()

Helper for 204 No Content.

JsonResponse
not_found(string $message = 'Resource not found')

Helper for 404 Not Found.

JsonResponse
forbidden(string $message = 'Access denied')

Helper for 403 Forbidden.

Details

void set_require_login(mixed $course = null, mixed $cm = null)

Set if login is required and set related options.

Parameters

mixed $course
mixed $cm

Return Value

void

void set_require_sesskey(bool $require = true)

Define the requirement of sesskey validation for non-idempotent requests.

Parameters

bool $require

Return Value

void

void set_require_capabilities(array $capabilities)

Define the capabilities that the user must have.

Parameters

array $capabilities

Return Value

void

protected void require_login()

Ensure the user is logged in if required.

Return Value

void

Exceptions

moodle_exception

protected void check_capabilities()

Check if the user has the required capabilities.

Return Value

void

Exceptions

moodle_exception

void set_form(object|string $form, mixed $formparams = null)

Set the form for the controller to handle.

Parameters

object|string $form

Class string or instance

mixed $formparams

Parameters for the form constructor

Return Value

void

Exceptions

coding_exception
moodle_exception

protected bool handle_form_submission()

Check if the bound form was submitted and validated.

Return Value

bool

protected mixed process_form_submission()

Return submitted form data when valid, otherwise false.

Return Value

mixed

protected bool process_form_cancel()

Determine if the form has been cancelled.

Return Value

bool

protected bool is_form_submitted()

Check if the form was submitted (regardless of validation).

Return Value

bool

in interacts_with_forms at line 105
protected string render_form_html()

Internal helper to render the form and return HTML.

Return Value

string

void set_context(context|null $context = null)

Set the context for the controller.

Parameters

context|null $context

Return Value

void

context get_context()

Get the resolved context (defaults to system).

Return Value

context

void set_page_url(moodle_url|string $url)

Set the page URL.

Parameters

moodle_url|string $url

Return Value

void

void set_page_layout(string $layout)

Set Moodle page layout.

Parameters

string $layout

Return Value

void

void set_page_title(string $title)

Set page title.

Parameters

string $title

Return Value

void

void set_page_heading(string $heading)

Set page heading.

Parameters

string $heading

Return Value

void

in interacts_with_page at line 106
void add_page_navbar(array|string $item)

Add an item to the page navbar trail.

Parameters

array|string $item

Return Value

void

in interacts_with_page at line 114
void set_url_from_route(string $route, array $parameters = [], int $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH)

Helper to set URL from route name.

Parameters

string $route
array $parameters
int $referenceType

Return Value

void

in interacts_with_page at line 129
protected void resolve_context()

Resolves the context if null, based on course/cm properties.

Return Value

void

in interacts_with_page at line 147
protected void setup_moodle_page()

Apply all settings to the global $PAGE object.

Return Value

void

Exceptions

moodle_exception

in interacts_with_page at line 179
protected moodle_url get_page_url()

Get the Moodle page URL, resolving defaults when needed.

Return Value

moodle_url

Exceptions

moodle_exception

void set_container(ContainerInterface $container)

Set the container for dependency injection.

Called automatically by the HttpKernel during resolution.

Parameters

ContainerInterface $container

Return Value

void

void set_request(Request $request)

Inject the current HTTP request instance.

Parameters

Request $request

Return Value

void

in abstract_controller at line 110
void handle()

Lifecycle Hook: Execute common setup logic.

Handle the execution of the controller (login, permissions, page settings).

Return Value

void

Exceptions

moodle_exception

in abstract_controller at line 129
void pre_handle()

Pre-handle hook.

Can be overridden by subclasses to add logic before the main action.

Return Value

void

Exceptions

moodle_exception

in abstract_controller at line 143
protected mixed get_service(string $service_name)

Retrieve a service from the container safely.

Parameters

string $service_name

Return Value

mixed

the service instance or null if not found

in abstract_controller at line 168
protected Response response(mixed $data, int $status = Response::HTTP_OK)

Return a generic response based on the provided data and status code.

Parameters

mixed $data

Data to return

int $status

HTTP status code (default is 200)

Return Value

Response

protected JsonResponse json_response(mixed $data, int $status = Response::HTTP_OK)

Return a JSON response with 'success' wrapper.

Parameters

mixed $data

Data to return

int $status

HTTP status code (default is 200)

Return Value

JsonResponse

protected bool is_json()

Check if the request expects a JSON response.

Always true for API controllers to force JSON logic.

Return Value

bool

in abstract_controller at line 213
protected bool is_inertia_request()

Check if the incoming request is an Inertia navigation.

Return Value

bool

in abstract_controller at line 228
protected Response render(object|string $content = '', string|null $component = null)

Render a Moodle view (HTML response) or widget.

Parameters

object|string $content

the content or widget to render

string|null $component

optional component name for rendering widgets

Return Value

Response

Exceptions

moodle_exception

in abstract_controller at line 264
protected Response render_from_widget(string $vue_component, array $props = [], string $component = middag_helper::COMPONENT_NAME)

Render a widget using a Moodle renderer.

Parameters

string $vue_component
array $props
string $component

The component to use with the renderer

Return Value

Response

Exceptions

moodle_exception

in abstract_controller at line 280
protected Response inertia(string $component, array $props = [])

Generates an Inertia response for the specified component and props.

Parameters

string $component

the name of the Inertia component to render

array $props

an associative array of properties to pass to the component

Return Value

Response

the generated response, either JSON for SPA requests or wrapped in Moodle layout for initial visits

Exceptions

moodle_exception

in abstract_controller at line 302
protected Response inertia_location(string $route, array $params = [])

Generates an Inertia location response.

Parameters

string $route

the name of the route to redirect or navigate to

array $params

an optional array of parameters to include with the route

Return Value

Response

the generated Inertia location response

in abstract_controller at line 317
protected Response render_from_renderer(renderable $widget, string $component = middag_helper::COMPONENT_NAME)

Render a renderable widget using a Moodle renderer component.

Parameters

renderable $widget

renderable instance to output

string $component

renderer component name (defaults to plugin component)

Return Value

Response

Exceptions

moodle_exception

in abstract_controller at line 332
protected Response render_from_template(string $templatename, array $templatecontext = [])

Render content using Moodle mustache templates.

Parameters

string $templatename

The template view. e.g. 'local_middag/dashboard'

array $templatecontext

The context for the template

Return Value

Response

Exceptions

moodle_exception

protected Response error_page(string $message, int $status = Response::HTTP_BAD_REQUEST)

Override error page to return JSON.

Parameters

string $message

Error message

int $status

HTTP status code (default is 400)

Return Value

Response

in abstract_controller at line 381
protected moodle_url url_generator(string $route, array $parameters = [], int $reference_type = UrlGeneratorInterface::ABSOLUTE_PATH)

Generates a URL from the given parameters.

Parameters

string $route

route name registered in the router

array $parameters

parameters to inject into the route placeholders

int $reference_type

URL reference type (absolute path or absolute URL)

Return Value

moodle_url

See also

UrlGeneratorInterface

in abstract_controller at line 394
protected RedirectResponse redirect(moodle_url|string $url, int $status = Response::HTTP_FOUND)

Helper to perform a redirect.

Parameters

moodle_url|string $url
int $status

Return Value

RedirectResponse

in abstract_controller at line 412
protected RedirectResponse redirect_to_route(string $route, array $parameters = [], int $status = Response::HTTP_FOUND)

Redirect to a named route with fallback support.

Parameters

string $route
array $parameters
int $status

Return Value

RedirectResponse

protected JsonResponse error_response(string $message, int $status = Response::HTTP_BAD_REQUEST, mixed $debug = null)

Send a standardized JSON error response.

Parameters

string $message

User-friendly error message

int $status

HTTP Status Code

mixed $debug

Optional debug data (only shown in developer mode)

Return Value

JsonResponse

protected JsonResponse created(mixed $data = [])

Helper for 201 Created.

Parameters

mixed $data

Return Value

JsonResponse

protected Response no_content()

Helper for 204 No Content.

Return Value

Response

protected JsonResponse not_found(string $message = 'Resource not found')

Helper for 404 Not Found.

Parameters

string $message

Return Value

JsonResponse

protected JsonResponse forbidden(string $message = 'Access denied')

Helper for 403 Forbidden.

Parameters

string $message

Return Value

JsonResponse