abstract_controller
abstract class abstract_controller implements controller_interface
| internal |
Base Controller.
Provides common functionality for all controllers:
- Dependency Injection Container access.
- Moodle Page/Auth setup.
- Response helpers (HTML, JSON, Redirects).
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 | |
| protected Request|null | $request | ||
| protected mixed | $course | ||
| protected mixed | $cm | ||
| 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 | ||
| protected array<string,mixed> | $params | ||
| protected array<string,mixed> | $payload |
Methods
Set if login is required and set related options.
Define the requirement of sesskey validation for non-idempotent requests.
Define the capabilities that the user must have.
Set the form for the controller to handle.
Check if the bound form was submitted and validated.
Return submitted form data when valid, otherwise false.
Check if the form was submitted (regardless of validation).
Internal helper to render the form and return HTML.
Set the context for the controller.
Add an item to the page navbar trail.
Helper to set URL from route name.
Resolves the context if null, based on course/cm properties.
Get the Moodle page URL, resolving defaults when needed.
Set the container for dependency injection.
Inject the current HTTP request instance.
Lifecycle Hook: Execute common setup logic.
Pre-handle hook.
Retrieve a service from the container safely.
Return a generic response based on the provided data and status code.
Return a JSON response.
Check if the request expects a JSON response.
Check if the incoming request is an Inertia navigation.
Render a Moodle view (HTML response) or widget.
Render a widget using a Moodle renderer.
Generates an Inertia response for the specified component and props.
Generates an Inertia location response.
Render a renderable widget using a Moodle renderer component.
Render content using Moodle mustache templates.
Display an error page with a specific message and status code.
Generates a URL from the given parameters.
Helper to perform a redirect.
Redirect to a named route with fallback support.
Details
in
interacts_with_auth at line 48
void
set_require_login(mixed $course = null, mixed $cm = null)
Set if login is required and set related options.
in
interacts_with_auth at line 58
void
set_require_sesskey(bool $require = true)
Define the requirement of sesskey validation for non-idempotent requests.
in
interacts_with_auth at line 66
void
set_require_capabilities(array $capabilities)
Define the capabilities that the user must have.
in
interacts_with_auth at line 76
protected void
require_login()
Ensure the user is logged in if required.
in
interacts_with_auth at line 97
protected void
check_capabilities()
Check if the user has the required capabilities.
in
interacts_with_forms at line 43
void
set_form(object|string $form, mixed $formparams = null)
Set the form for the controller to handle.
in
interacts_with_forms at line 65
protected bool
handle_form_submission()
Check if the bound form was submitted and validated.
in
interacts_with_forms at line 73
protected mixed
process_form_submission()
Return submitted form data when valid, otherwise false.
in
interacts_with_forms at line 85
protected bool
process_form_cancel()
Determine if the form has been cancelled.
in
interacts_with_forms at line 97
protected bool
is_form_submitted()
Check if the form was submitted (regardless of validation).
in
interacts_with_forms at line 105
protected string
render_form_html()
Internal helper to render the form and return HTML.
in
interacts_with_page at line 58
void
set_context(context|null $context = null)
Set the context for the controller.
in
interacts_with_page at line 66
context
get_context()
Get the resolved context (defaults to system).
in
interacts_with_page at line 74
void
set_page_url(moodle_url|string $url)
Set the page URL.
in
interacts_with_page at line 82
void
set_page_layout(string $layout)
Set Moodle page layout.
in
interacts_with_page at line 90
void
set_page_title(string $title)
Set page title.
in
interacts_with_page at line 98
void
set_page_heading(string $heading)
Set page heading.
in
interacts_with_page at line 106
void
add_page_navbar(array|string $item)
Add an item to the page navbar trail.
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.
in
interacts_with_page at line 129
protected void
resolve_context()
Resolves the context if null, based on course/cm properties.
in
interacts_with_page at line 147
protected void
setup_moodle_page()
Apply all settings to the global $PAGE object.
in
interacts_with_page at line 179
protected moodle_url
get_page_url()
Get the Moodle page URL, resolving defaults when needed.
at line 86
void
set_container(ContainerInterface $container)
Set the container for dependency injection.
Called automatically by the HttpKernel during resolution.
at line 97
void
set_request(Request $request)
Inject the current HTTP request instance.
at line 110
void
handle()
Lifecycle Hook: Execute common setup logic.
Handle the execution of the controller (login, permissions, page settings).
at line 129
void
pre_handle()
Pre-handle hook.
Can be overridden by subclasses to add logic before the main action.
at line 143
protected mixed
get_service(string $service_name)
Retrieve a service from the container safely.
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.
at line 185
protected JsonResponse
json_response(mixed $data, int $status = Response::HTTP_OK)
Return a JSON response.
at line 195
protected bool
is_json()
Check if the request expects a JSON response.
at line 213
protected bool
is_inertia_request()
Check if the incoming request is an Inertia navigation.
at line 228
protected Response
render(object|string $content = '', string|null $component = null)
Render a Moodle view (HTML response) or widget.
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.
at line 280
protected Response
inertia(string $component, array $props = [])
Generates an Inertia response for the specified component and props.
at line 302
protected Response
inertia_location(string $route, array $params = [])
Generates an Inertia location response.
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.
at line 332
protected Response
render_from_template(string $templatename, array $templatecontext = [])
Render content using Moodle mustache templates.
at line 350
protected Response
error_page(string $message, int $status = Response::HTTP_BAD_REQUEST)
Display an error page with a specific message and status code.
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.
at line 394
protected RedirectResponse
redirect(moodle_url|string $url, int $status = Response::HTTP_FOUND)
Helper to perform a redirect.
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.