Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 43 additions & 1 deletion docs/plugin_services/session.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,46 @@ Session

Please read the :xref:`dev docs contributing guidelines` and :xref:`Contributing to Mautic’s documentation` to get started.

.. vale on
.. vale on

In Mautic 7, the session is part of the request. Obtain it from the current ``Request`` rather than from a ``session`` service. Inject ``Symfony\Component\HttpFoundation\RequestStack`` and call ``getSession()`` on the current request:

.. code-block:: php

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ported the session operations (all(), get() with a default, has(), set(), remove(), clear()) from the legacy _plugin_services_session.md guide. Updated the access pattern to RequestStack::getSession() for Mautic 7, since the legacy $this->get('session') service getter (and Symfony 2.8 Session class) no longer applies.

Source: https://github.com/mautic/developer-documentation/blob/main/source/includes/_plugin_services_session.md


<?php

use Symfony\Component\HttpFoundation\RequestStack;

final class ExampleService
{
public function __construct(private RequestStack $requestStack)
{
}

public function handleSession(): void
{
$session = $this->requestStack->getSession();

// Get all session parameters
$all = $session->all();

// Get a specific parameter, using 'mars' as the default
$world = $session->get('helloworld.world', 'mars');

// Check whether a parameter exists
if ($session->has('helloworld.world')) {
// Do something.
}

// Set a session parameter
$session->set('helloworld.world', 'mars');

// Remove a session parameter
$session->remove('helloworld.world');

// Clear the whole session
$session->clear();
}
}

Within a controller action, you can also type-hint ``Symfony\Component\HttpFoundation\Request`` as an argument and call ``$request->getSession()`` directly.
Loading