cutelyst 4.8.0
A C++ Web Framework built on top of Qt, using the simple approach of Catalyst (Perl) framework.
Cutelyst::Context Class Reference

The Cutelyst Context. More...

#include <Cutelyst/Context>

Inheritance diagram for Cutelyst::Context:

Public Slots

void finalize ()

Public Member Functions

 Context (Application *app)
virtual ~Context ()
Action * action () const noexcept
QString actionName () const noexcept
Application * app () const noexcept
void appendError (const QString &error)
void attachAsync ()
QVariantMap config () const noexcept
QVariant config (const QString &key, const QVariant &defaultValue={}) const
Controller * controller () const noexcept
Controller * controller (QStringView name) const
QString controllerName () const noexcept
ViewcustomView () const noexcept
void detach (Action *action=nullptr)
void detachAsync () noexcept
bool detached () const noexcept
Dispatcherdispatcher () const noexcept
Engine * engine () const noexcept
bool error () const noexcept
QStringList errors () const noexcept
bool execute (Component *code)
bool forward (Component *component)
bool forward (QStringView action)
Action * getAction (QStringView action, QStringView ns={}) const
QVector< Action * > getActions (QStringView action, QStringView ns={}) const
QLocale locale () const noexcept
QString ns () const noexcept
template<typename T>
plugin ()
QVector< Plugin * > plugins () const
QString qtTrId (const char *id, int n=-1) const
Requestreq () const noexcept
Requestrequest () const noexcept
Responseres () const noexcept
Responseresponse () const noexcept
bool setCustomView (QStringView name)
void setLocale (const QLocale &locale)
void setStash (const QString &key, const ParamsMultiMap &map)
void setStash (const QString &key, const QVariant &value)
void setState (bool state) noexcept
QStack< Component * > stack () const noexcept
QVariantHash & stash ()
QVariant stash (const QString &key) const
QVariant stash (const QString &key, const QVariant &defaultValue) const
void stash (const QVariantHash &unite)
bool stashRemove (const QString &key)
QVariant stashTake (const QString &key)
bool state () const noexcept
QString translate (const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const
QUrl uriFor (Action *action, const ParamsMultiMap &queryValues) const
QUrl uriFor (Action *action, const QStringList &captures={}, const QStringList &args={}, const ParamsMultiMap &queryValues={}) const
QUrl uriFor (const QString &path, const ParamsMultiMap &queryValues) const
QUrl uriFor (const QString &path={}, const QStringList &args={}, const ParamsMultiMap &queryValues={}) const
QUrl uriForAction (QStringView path, const ParamsMultiMap &queryValues) const
QUrl uriForAction (QStringView path, const QStringList &captures={}, const QStringList &args={}, const ParamsMultiMap &queryValues={}) const
Viewview (QStringView name={}) const
Public Member Functions inherited from QObject
 QObject (QObject *parent)
QBindable< QStringbindableObjectName ()
bool blockSignals (bool block)
const QObjectListchildren () const const
QMetaObject::Connection connect (const QObject *sender, const char *signal, const char *method, Qt::ConnectionType type) const const
void deleteLater ()
void destroyed (QObject *obj)
bool disconnect (const char *signal, const QObject *receiver, const char *method) const const
bool disconnect (const QObject *receiver, const char *method) const const
void dumpObjectInfo () const const
void dumpObjectTree () const const
QList< QByteArraydynamicPropertyNames () const const
virtual bool event (QEvent *e)
virtual bool eventFilter (QObject *watched, QEvent *event)
findChild (QAnyStringView name, Qt::FindChildOptions options) const const
findChild (Qt::FindChildOptions options) const const
QList< T > findChildren (const QRegularExpression &re, Qt::FindChildOptions options) const const
QList< T > findChildren (QAnyStringView name, Qt::FindChildOptions options) const const
QList< T > findChildren (Qt::FindChildOptions options) const const
bool inherits (const char *className) const const
void installEventFilter (QObject *filterObj)
bool isQmlExposed () const const
bool isQuickItemType () const const
bool isWidgetType () const const
bool isWindowType () const const
void killTimer (int id)
void killTimer (Qt::TimerId id)
virtual const QMetaObjectmetaObject () const const
bool moveToThread (QThread *targetThread)
QString objectName () const const
void objectNameChanged (const QString &objectName)
QObjectparent () const const
QVariant property (const char *name) const const
 Q_CLASSINFO (Name, Value)
 Q_EMIT Q_EMIT
 Q_ENUM (...)
 Q_ENUM_NS (...)
 Q_ENUMS (...)
 Q_FLAG (...)
 Q_FLAG_NS (...)
 Q_FLAGS (...)
 Q_GADGET Q_GADGET
 Q_GADGET_EXPORT (EXPORT_MACRO)
 Q_INTERFACES (...)
 Q_INVOKABLE Q_INVOKABLE
 Q_MOC_INCLUDE Q_MOC_INCLUDE
 Q_NAMESPACE Q_NAMESPACE
 Q_NAMESPACE_EXPORT (EXPORT_MACRO)
 Q_OBJECT Q_OBJECT
 Q_PROPERTY (...)
 Q_REVISION Q_REVISION
 Q_SET_OBJECT_NAME (Object)
 Q_SIGNAL Q_SIGNAL
 Q_SIGNALS Q_SIGNALS
 Q_SLOT Q_SLOT
 Q_SLOTS Q_SLOTS
qobject_cast (const QObject *object)
qobject_cast (QObject *object)
 QT_NO_CONTEXTLESS_CONNECT QT_NO_CONTEXTLESS_CONNECT
 QT_NO_NARROWING_CONVERSIONS_IN_CONNECT QT_NO_NARROWING_CONVERSIONS_IN_CONNECT
void removeEventFilter (QObject *obj)
void setObjectName (const QString &name)
void setObjectName (QAnyStringView name)
void setParent (QObject *parent)
bool setProperty (const char *name, const QVariant &value)
bool setProperty (const char *name, QVariant &&value)
bool signalsBlocked () const const
int startTimer (int interval, Qt::TimerType timerType)
int startTimer (std::chrono::nanoseconds interval, Qt::TimerType timerType)
QThreadthread () const const

Protected Member Functions

 Context (ContextPrivate *priv)
Protected Member Functions inherited from QObject
virtual void childEvent (QChildEvent *event)
virtual void connectNotify (const QMetaMethod &signal)
virtual void customEvent (QEvent *event)
virtual void disconnectNotify (const QMetaMethod &signal)
bool isSignalConnected (const QMetaMethod &signal) const const
int receivers (const char *signal) const const
QObjectsender () const const
int senderSignalIndex () const const
virtual void timerEvent (QTimerEvent *event)

Properties

Action * action
QString actionName
QVariantMap config
Controller * controller
QString controllerName
QString namespace
QString ns
Requestreq
Requestrequest
bool state
Properties inherited from QObject
 objectName

Friends

class Action
class ActionChain
class Application
class Async
class Controller
class DispatchType
class Engine
class Plugin

Additional Inherited Members

Public Types inherited from QObject
enum  TimerId
Static Public Member Functions inherited from QObject
QMetaObject::Connection connect (const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QMetaObject::Connection connect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method, Qt::ConnectionType type)
QMetaObject::Connection connect (const QObject *sender, PointerToMemberFunction signal, const QObject *context, Functor functor, Qt::ConnectionType type)
QMetaObject::Connection connect (const QObject *sender, PointerToMemberFunction signal, const QObject *receiver, PointerToMemberFunction method, Qt::ConnectionType type)
QMetaObject::Connection connect (const QObject *sender, PointerToMemberFunction signal, Functor functor)
bool disconnect (const QMetaObject::Connection &connection)
bool disconnect (const QObject *sender, const char *signal, const QObject *receiver, const char *method)
bool disconnect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method)
bool disconnect (const QObject *sender, PointerToMemberFunction signal, const QObject *receiver, PointerToMemberFunction method)
QString tr (const char *sourceText, const char *disambiguation, int n)
Public Attributes inherited from QObject
 Invalid
typedef QObjectList

Detailed Description

This is the context class that glues Request and Response plus some helper methods.

Logging category
cutelyst.core
Logging with Cutelyst

Definition at line 41 of file context.h.

Constructor & Destructor Documentation

◆ Context() [1/2]

Context::Context ( Application * app)

◆ ~Context()

Context::~Context ( )
virtual

Destroys the Context object. This will also delete the associated Request and Response.

Definition at line 44 of file context.cpp.

◆ Context() [2/2]

Context::Context ( ContextPrivate * priv)
protected

Constructs a new Context object using private implementation.

Definition at line 25 of file context.cpp.

Member Function Documentation

◆ action()

Action * Context::action ( ) const
nodiscardnoexcept

Returns a pointer to the current action.

Definition at line 110 of file context.cpp.

References Context().

◆ actionName()

QString Context::actionName ( ) const
nodiscardnoexcept

Returns the private name of the current action.

Definition at line 116 of file context.cpp.

References Context().

◆ app()

Application * Context::app ( ) const
nodiscardnoexcept

Returns the application instance. See Cutelyst::Application.

Definition at line 92 of file context.cpp.

References Context().

Referenced by Context().

◆ appendError()

void Context::appendError ( const QString & error)

Sets an error string and tries to stop.

See also
error() errors()

Definition at line 57 of file context.cpp.

References Context(), and error().

Referenced by Cutelyst::Dispatcher::dispatch(), execute(), Cutelyst::CuteleeView::render(), and Cutelyst::ViewEmail::render().

◆ attachAsync()

void Context::attachAsync ( )

Reattaches to the processing chain in order to process the remaining of the action chaing. Call this after you called detachAsync().

The scoped ASync class can make handlying async requests easier.

Definition at line 356 of file context.cpp.

References Context(), action, execute(), finalize(), and QObject::Q_EMIT().

◆ config() [1/2]

QVariantMap Context::config ( ) const
nodiscardnoexcept

Returns a configuration mapping for all configuration entries read from the Cutelyst configuration section.

See also
Configure your application

Definition at line 479 of file context.cpp.

References Context().

◆ config() [2/2]

QVariant Context::config ( const QString & key,
const QVariant & defaultValue = {} ) const
nodiscard

Returns a configuration value for key. If key is not found, defaultValue will be returned.

This calls Application::config(), so it will only return entries from the Cutelyst configuration section.

See also
Configure your application

Definition at line 473 of file context.cpp.

References Context().

◆ controller() [1/2]

Controller * Context::controller ( ) const
nodiscardnoexcept

Returns the current controller.

Definition at line 152 of file context.cpp.

References Context().

◆ controller() [2/2]

Controller * Context::controller ( QStringView name) const
nodiscard

Returns the controller by name or nullptr if the controller is not found.

Definition at line 158 of file context.cpp.

References Context().

◆ controllerName()

QString Cutelyst::Context::controllerName ( ) const
nodiscardnoexcept

Returns the current controller name.

Definition at line 146 of file context.cpp.

References Context().

◆ customView()

View * Context::customView ( ) const
nodiscardnoexcept

Returns the View set to be used for rendering this request, if one is set by setCustomView() or nullptr if none was set.

Definition at line 164 of file context.cpp.

References Context().

Referenced by Cutelyst::RenderView::doExecute().

◆ detach()

void Context::detach ( Action * action = nullptr)

The same as forward().

When called with no arguments it escapes the processing chain entirely.

See also
finalize()

Definition at line 340 of file context.cpp.

References Context(), and action.

Referenced by Cutelyst::RoleACL::aroundExecute(), Cutelyst::Action::doExecute(), Cutelyst::LangSelect::fromPath(), and Cutelyst::LangSelect::fromUrlQuery().

◆ detachAsync()

void Context::detachAsync ( )
noexcept

Detaches the processing chain telling the Engine that the request is not finished yet.

The scoped ASync class can make handling async requests easier.

It’s often useful to call async API’s. While convenient, the use of QEventLoop will only work for the first request or lead to a crash due stacking of calls.

This method, tells the Engine that this request is not finished yet, making it return to the event loop to process other requests or the task that was created prior to calling this.

Once done, call attachAsync() in order to process the remaining of the action chain.

Definition at line 350 of file context.cpp.

References Context().

◆ detached()

bool Context::detached ( ) const
noexcept

Returns true if the last executed Action requested that the processing be escaped.

Definition at line 334 of file context.cpp.

References Context().

Referenced by Cutelyst::Action::doExecute().

◆ dispatcher()

Dispatcher * Context::dispatcher ( ) const
nodiscardnoexcept

Returns the dispatcher instance. See Cutelyst::Dispatcher.

Definition at line 140 of file context.cpp.

References Context().

Referenced by Context().

◆ engine()

Engine * Context::engine ( ) const
nodiscardnoexcept

Returns the engine instance. See Cutelyst::Engine.

Definition at line 86 of file context.cpp.

References Context().

Referenced by Context().

◆ error()

bool Context::error ( ) const
nodiscardnoexcept

Returns true if an error was set.

See also
appendError() errors()

Definition at line 51 of file context.cpp.

References Context().

Referenced by appendError().

◆ errors()

QStringList Context::errors ( ) const
nodiscardnoexcept

Returns a list of errors that were defined.

See also
error() appendError()

Definition at line 68 of file context.cpp.

References Context().

◆ execute()

bool Context::execute ( Component * code)

◆ finalize

void Context::finalize ( )
slot

Finalize the request right away. This is automatically called at the end of the actions chain.

Definition at line 494 of file context.cpp.

References Context(), QString::fromLatin1(), QByteArray::number(), QString::number(), QString::size(), and QString::truncate().

Referenced by attachAsync().

◆ forward() [1/2]

bool Context::forward ( Component * component)

This is one way of calling another action (method) in the same or a different controller. You can also use directly call another method to the same or a different controller.

The main difference is that 'forward' uses some of the Cutelyst request cycle overhead, including debugging, which may be useful to you. On the other hand, there are some complications to using 'forward', restrictions on values returned from 'forward', and it may not handle errors as you prefer. Whether you use 'forward' or not is up to you; it is not considered superior to the other ways to call a method.

This calls Component::execute().

Keep in mind that the End() method used is that of the caller action. So a c->detach() inside a forwarded action would run the End() method from the original action requested.

Definition at line 394 of file context.cpp.

References Context(), and action.

Referenced by Cutelyst::RenderView::doExecute().

◆ forward() [2/2]

bool Context::forward ( QStringView action)

This is one way of calling another action (method) in the same or a different controller. You can also use directly call another method to the same or a different controller.

The main difference is that 'forward' uses some of the Cutelyst request cycle overhead, including debugging, which may be useful to you. On the other hand, there are some complications to using 'forward', restrictions on values returned from 'forward', and it may not handle errors as you prefer. Whether you use 'forward' or not is up to you; it is not considered superior to the other ways to call a method.

This calls another action, by its private name.

Keep in mind that the End() method used is that of the caller action. So a c->detach() inside a forwarded action would run the End() method from the original action requested.

Definition at line 400 of file context.cpp.

References Context(), and action.

◆ getAction()

Action * Context::getAction ( QStringView action,
QStringView ns = {} ) const
nodiscard

Gets an action in a given namespace ns.

Definition at line 406 of file context.cpp.

References Context(), action, and ns.

◆ getActions()

QVector< Action * > Context::getActions ( QStringView action,
QStringView ns = {} ) const
nodiscard

Gets all actions of a given action name in a namespace ns and all parent namespaces.

Definition at line 412 of file context.cpp.

References Context(), action, and ns.

◆ locale()

◆ ns()

QString Context::ns ( ) const
nodiscardnoexcept

Returns the namespace of the current action. I.e. the URI prefix corresponding to the controller of the current action.

For example: on a class named FooBar which inherits Controller this will return 'foo/bar'.

Definition at line 122 of file context.cpp.

References Context().

◆ plugin()

template<typename T>
T Cutelyst::Context::plugin ( )
inline

Returns the registered plugin that casts to the template type T.

Definition at line 490 of file context.h.

References plugin(), plugins(), and QObject::qobject_cast().

Referenced by plugin().

◆ plugins()

QVector< Cutelyst::Plugin * > Context::plugins ( ) const
nodiscard

Returns all registered plugins.

Definition at line 418 of file context.cpp.

References Context().

Referenced by Context(), and plugin().

◆ qtTrId()

QString Cutelyst::Context::qtTrId ( const char * id,
int n = -1 ) const
inlinenodiscard

Finds and returns a translated string.

Returns a translated string identified by id. If no matching string is found, the id itself is returned. This can be used similar to Qt’s global qtTrId() function.

If n >= 0, all occurences of %n in the resulting string are replaced with a decimal representation of n. In addition, appending n’s value, the translation may vary.

Meta data and comments can be passed as documented for QObject::tr(). In addition, it is possible to supply a source string template like that:

//% <C string>

or

\begincomment% <C string> \endcomment

Example:

void MyController::index(Context *c)
{
//% "%n fooish bar(s) found.\n"
//% "Do you want to continue?"
c->res()->setBody(c->qtTrId("my-app-translation-id", n));
}
Context(Application *app)
Definition context.cpp:30
Response * res() const noexcept
Definition context.cpp:104
QString qtTrId(const char *id, int n=-1) const
Definition context.h:656
void setBody(QIODevice *body)
Definition response.cpp:104

Creating QM files suitable for use with this function requires passing the -idbased option to the lrelease tool.

See also
Translate your application
Since
Cutelyst 3.9.0

Definition at line 656 of file context.h.

References qtTrId(), and translate().

Referenced by Cutelyst::ValidatorEmail::categoryString(), Cutelyst::ValidatorDomain::diagnoseString(), Cutelyst::ValidatorEmail::diagnoseString(), Cutelyst::Dispatcher::dispatch(), Cutelyst::Memcached::errorString(), Cutelyst::ValidatorPwQuality::errorString(), Cutelyst::ValidatorAfter::genericParsingError(), Cutelyst::ValidatorBefore::genericParsingError(), Cutelyst::ValidatorBetween::genericParsingError(), Cutelyst::ValidatorMax::genericParsingError(), Cutelyst::ValidatorMin::genericParsingError(), Cutelyst::ValidatorRule::genericParsingError(), Cutelyst::ValidatorSize::genericParsingError(), Cutelyst::ValidatorAfter::genericValidationDataError(), Cutelyst::ValidatorBefore::genericValidationDataError(), Cutelyst::ValidatorBetween::genericValidationDataError(), Cutelyst::ValidatorCharNotAllowed::genericValidationDataError(), Cutelyst::ValidatorFileSize::genericValidationDataError(), Cutelyst::ValidatorIn::genericValidationDataError(), Cutelyst::ValidatorMax::genericValidationDataError(), Cutelyst::ValidatorMin::genericValidationDataError(), Cutelyst::ValidatorNotIn::genericValidationDataError(), Cutelyst::ValidatorRule::genericValidationDataError(), Cutelyst::ValidatorSize::genericValidationDataError(), Cutelyst::ValidatorAccepted::genericValidationError(), Cutelyst::ValidatorAfter::genericValidationError(), Cutelyst::ValidatorAlpha::genericValidationError(), Cutelyst::ValidatorAlphaDash::genericValidationError(), Cutelyst::ValidatorAlphaNum::genericValidationError(), Cutelyst::ValidatorBefore::genericValidationError(), Cutelyst::ValidatorBetween::genericValidationError(), Cutelyst::ValidatorBoolean::genericValidationError(), Cutelyst::ValidatorCharNotAllowed::genericValidationError(), Cutelyst::ValidatorConfirmed::genericValidationError(), Cutelyst::ValidatorDate::genericValidationError(), Cutelyst::ValidatorDateTime::genericValidationError(), Cutelyst::ValidatorDifferent::genericValidationError(), Cutelyst::ValidatorDigits::genericValidationError(), Cutelyst::ValidatorDigitsBetween::genericValidationError(), Cutelyst::ValidatorFileSize::genericValidationError(), Cutelyst::ValidatorFilled::genericValidationError(), Cutelyst::ValidatorIn::genericValidationError(), Cutelyst::ValidatorInteger::genericValidationError(), Cutelyst::ValidatorIp::genericValidationError(), Cutelyst::ValidatorJson::genericValidationError(), Cutelyst::ValidatorMax::genericValidationError(), Cutelyst::ValidatorMin::genericValidationError(), Cutelyst::ValidatorNotIn::genericValidationError(), Cutelyst::ValidatorNumeric::genericValidationError(), Cutelyst::ValidatorPresent::genericValidationError(), Cutelyst::ValidatorRegularExpression::genericValidationError(), Cutelyst::ValidatorRequired::genericValidationError(), Cutelyst::ValidatorRequiredIf::genericValidationError(), Cutelyst::ValidatorRequiredIfStash::genericValidationError(), Cutelyst::ValidatorRequiredUnless::genericValidationError(), Cutelyst::ValidatorRequiredUnlessStash::genericValidationError(), Cutelyst::ValidatorRequiredWith::genericValidationError(), Cutelyst::ValidatorRequiredWithAll::genericValidationError(), Cutelyst::ValidatorRequiredWithout::genericValidationError(), Cutelyst::ValidatorRequiredWithoutAll::genericValidationError(), Cutelyst::ValidatorRule::genericValidationError(), Cutelyst::ValidatorSame::genericValidationError(), Cutelyst::ValidatorSize::genericValidationError(), Cutelyst::ValidatorTime::genericValidationError(), Cutelyst::ValidatorUrl::genericValidationError(), Cutelyst::ValidatorRule::label(), Cutelyst::ValidatorRule::parsingError(), qtTrId(), Cutelyst::CuteleeView::render(), Cutelyst::ValidatorRule::validationDataError(), and Cutelyst::ValidatorRule::validationError().

◆ req()

Request * Context::req ( ) const
nodiscardnoexcept

Short for request().

Definition at line 134 of file context.cpp.

References Context().

◆ request()

Request * Context::request ( ) const
nodiscardnoexcept

Returns the current Request object containing information about the client request.

Definition at line 128 of file context.cpp.

References Context().

◆ res()

Response * Context::res ( ) const
nodiscardnoexcept

Returns the current Cutelyst::Response object, see there for details.

Definition at line 104 of file context.cpp.

References Context().

Referenced by Cutelyst::RenderView::doExecute(), Cutelyst::LangSelect::fromPath(), and Cutelyst::CuteleeView::render().

◆ response()

Response * Context::response ( ) const
nodiscardnoexcept

Returns the current Cutelyst::Response object, see there for details.

Definition at line 98 of file context.cpp.

References Context().

Referenced by Cutelyst::UA::forwardAsync(), Cutelyst::UA::forwardRequestResponse(), and Cutelyst::ViewJson::render().

◆ setCustomView()

bool Context::setCustomView ( QStringView name)

Defines the View to be used to render the request, it must be previously be registered by Cutelyst::Application.

Action classes like RenderView will use this value to overwrite their settings.

Returns true if a view with the given name was found.

Definition at line 176 of file context.cpp.

References Context().

◆ setLocale()

void Context::setLocale ( const QLocale & locale)

Defines the current locale to be used when processing a View or translating user messages.

Setting a locale on a web application can be done in many ways, so it’s up to the developer to decide which one to use.

For example it’s possible to try to guess the user locale with the request header Accept-Language, or use the chained dispatcher to first match the locale as in "example.com/pt-br/some_action", or store the locale into a cookie or session.

Be sure to set it as soon as possible so that all content can be properly localized.

See also
LangSelect
Translate your application

Definition at line 467 of file context.cpp.

References Context(), and locale().

Referenced by Cutelyst::LangSelect::fromPath().

◆ setStash() [1/2]

void Context::setStash ( const QString & key,
const ParamsMultiMap & map )

A convenient method to set a single ParamsMultiMap map to the stash on key.

Definition at line 219 of file context.cpp.

References Context(), and QVariant::fromValue().

◆ setStash() [2/2]

◆ setState()

void Context::setState ( bool state)
noexcept

Sets the state of the current executed action. Setting to false will make the dispatcher skip non processed actions.

Definition at line 80 of file context.cpp.

References Context(), and state.

Referenced by Cutelyst::Action::doExecute(), and execute().

◆ stack()

QStack< Component * > Context::stack ( ) const
nodiscardnoexcept

Returns the internal execution stack (actions that are currently executing).

Definition at line 225 of file context.cpp.

References Context().

◆ stash() [1/4]

QVariantHash & Context::stash ( )
nodiscard

Returns a QVariantHash reference to the stash, which may be used to store data and pass it between components during a request.

The stash is automatically sent to the view. The stash is cleared at the end of a request; it cannot be used for persistent storage (for this you must use a session; see Session for a complete system integrated with Cutelyst).

Definition at line 183 of file context.cpp.

References Context().

◆ stash() [2/4]

QVariant Context::stash ( const QString & key) const
nodiscard

A convenient method to retrieve a single value from the stash by key.

Definition at line 189 of file context.cpp.

References Context().

◆ stash() [3/4]

QVariant Context::stash ( const QString & key,
const QVariant & defaultValue ) const
nodiscard

A convenient method to retrieve a single value with the stash by key. If key is not found, defaultValue will be returned.

Definition at line 195 of file context.cpp.

References Context().

◆ stash() [4/4]

void Context::stash ( const QVariantHash & unite)

You can set hash keys by passing arguments, that will be united with the stash, which may be used to store data and pass it between components during a request.

The stash is automatically sent to the View. The stash is cleared at the end of a request; it cannot be used for persistent storage (for this you must use a session; see Session for a complete system integrated with Cutelyst).

If a given key is present it will be replaced.

c->stash({
{"foo", 10},
{"bar", QStringLiteral("my stash value")}
});

Definition at line 563 of file context.cpp.

References Context().

Referenced by Cutelyst::CSRFProtection::checkPassed(), Cutelyst::ValidatorRule::defaultValue(), Cutelyst::Session::deleteReason(), Cutelyst::Session::deleteValue(), Cutelyst::Session::deleteValues(), Cutelyst::Session::expires(), Cutelyst::CSRFProtection::getToken(), Cutelyst::Session::id(), Cutelyst::CuteleeView::render(), Cutelyst::ViewEmail::render(), Cutelyst::ViewEmailTemplate::render(), Cutelyst::ViewJson::render(), Cutelyst::Session::setValue(), Cutelyst::Authentication::user(), Cutelyst::Authentication::userExists(), Cutelyst::Authentication::userInRealm(), Cutelyst::ValidatorIn::validate(), Cutelyst::ValidatorNotIn::validate(), Cutelyst::ValidatorPwQuality::validate(), Cutelyst::ValidatorRequiredIfStash::validate(), Cutelyst::ValidatorRequiredUnlessStash::validate(), and Cutelyst::Session::value().

◆ stashRemove()

bool Context::stashRemove ( const QString & key)

Removes the item that has the key from the stash. Returns true if any item was removed.

Definition at line 207 of file context.cpp.

References Context().

◆ stashTake()

QVariant Context::stashTake ( const QString & key)

Removes the item with the key from the stash and returns the value associated with it. If the item does not exist in the stash, the function simply returns a default-constructed value. If you don’t use the return value, stashRemove() is more efficient.

Definition at line 201 of file context.cpp.

References Context().

◆ state()

bool Context::state ( ) const
nodiscardnoexcept

Contains the return value of the last executed action.

Definition at line 74 of file context.cpp.

References Context().

◆ translate()

QString Context::translate ( const char * context,
const char * sourceText,
const char * disambiguation = nullptr,
int n = -1 ) const
nodiscard

Translates the sourceText for the given context into the language defined by locale().

See Application::addTranslator() for information about installation of translators. Internally this function will use QTranslator::translate().

void MyController::index(Context *c)
{
c->res()->setBody(c->translate("MyController", "You are on the index page."));
}
QString translate(const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const
Definition context.cpp:485
See also
Translate your application

Definition at line 485 of file context.cpp.

References Context().

Referenced by Cutelyst::ValidatorAfter::genericParsingError(), Cutelyst::ValidatorBefore::genericParsingError(), Cutelyst::ValidatorDate::genericValidationError(), Cutelyst::ValidatorDateTime::genericValidationError(), Cutelyst::ValidatorDifferent::genericValidationError(), Cutelyst::ValidatorSame::genericValidationError(), Cutelyst::ValidatorTime::genericValidationError(), Cutelyst::ValidatorRule::label(), Cutelyst::ValidatorRule::parsingError(), qtTrId(), Cutelyst::ValidatorRule::validationDataError(), and Cutelyst::ValidatorRule::validationError().

◆ uriFor() [1/4]

QUrl Cutelyst::Context::uriFor ( Action * action,
const ParamsMultiMap & queryValues ) const
inlinenodiscard

Constructs an absolute QUrl object based on the application root, the provided action and the query parameters queryValues provided. When used as a string, provides a textual URI.

Definition at line 646 of file context.h.

References action, and uriFor().

◆ uriFor() [2/4]

QUrl Context::uriFor ( Action * action,
const QStringList & captures = {},
const QStringList & args = {},
const ParamsMultiMap & queryValues = {} ) const
nodiscard

Constructs an absolute QUrl object based on the application root, the provided action, captures, arguments args and query parameters queryValues. When used as a string, provides a textual URI.

If action is a nullptr, the URI for the current action is returned. To return the current action and also provide args, use c->uriFor(c->action(), args).

Definition at line 279 of file context.cpp.

References Context(), action, QList::append(), QString::isEmpty(), Cutelyst::Action::numberOfCaptures(), QList::size(), QList::takeFirst(), and uriFor().

◆ uriFor() [3/4]

QUrl Cutelyst::Context::uriFor ( const QString & path,
const ParamsMultiMap & queryValues ) const
inlinenodiscard

Constructs an absolute QUrl object based on the application root, the provided path and the query parameters queryValues provided. When used as a string, provides a textual URI.

The first argument is taken as a public URI path relative c->ns() (if it doesn’t begin with a forward slash) or relative to the application root (if it does). It is then merged with c->request()->base() and any queryValues are appended as "?foo=bar" parameters.

Definition at line 641 of file context.h.

References Context(), and uriFor().

◆ uriFor() [4/4]

QUrl Context::uriFor ( const QString & path = {},
const QStringList & args = {},
const ParamsMultiMap & queryValues = {} ) const
nodiscard

Constructs an absolute QUrl object based on the application root, the provided path, and the additional arguments args and query parameters queryValues provided. When used as a string, provides a textual URI.

The first argument is taken as a public URI path relative c->ns() (if it doesn’t begin with a forward slash) or relative to the application root (if it does). It is then merged with c->request()->base() any args are appended as additional path components; and any queryValues are appended as "?foo=bar" parameters.

Definition at line 231 of file context.cpp.

References Context(), QUrlQuery::addQueryItem(), QString::compare(), QMultiMap::constBegin(), QMultiMap::constEnd(), QUrl::DecodedMode, QList::isEmpty(), QMultiMap::isEmpty(), QString::isEmpty(), QStringList::join(), QString::prepend(), QUrl::setPath(), QUrl::setQuery(), QMultiMap::size(), and QString::startsWith().

Referenced by uriFor(), uriFor(), uriFor(), and uriForAction().

◆ uriForAction() [1/2]

QUrl Cutelyst::Context::uriForAction ( QStringView path,
const ParamsMultiMap & queryValues ) const
inlinenodiscard

A convenience method for the uriForAction() without the arguments parameter.

Definition at line 651 of file context.h.

References uriForAction().

◆ uriForAction() [2/2]

QUrl Context::uriForAction ( QStringView path,
const QStringList & captures = {},
const QStringList & args = {},
const ParamsMultiMap & queryValues = {} ) const
nodiscard

A private path to the Cutelyst action you want to create a URI for.

This is a shortcut for calling c->dispatcher()->getActionByPath(path) and passing the resulting action and the remaining arguments to c->uriFor().

Note that although the path looks like a URI that dispatches to the wanted action, it is not a URI, but an internal path to that action.

For example, if the action looks like:

class Users : public Cutelyst::Controller
{
public:
C_ATTR(lst, :Path(the-list))
void lst(Context *c);
};
Cutelyst Controller base class.
Definition controller.h:56
#define C_ATTR(X, Y)
Definition controller.h:41

You can use: c->uriForAction('/users/lst'); and it will create the URI /users/the-list.

Definition at line 316 of file context.cpp.

References Context(), action, and uriFor().

Referenced by uriForAction().

◆ view()

View * Context::view ( QStringView name = {}) const
nodiscard

Returns the View with name or nullptr if not found.

Definition at line 170 of file context.cpp.

References Context().

◆ Action

friend class Action
friend

Definition at line 628 of file context.h.

◆ ActionChain

friend class ActionChain
friend

Definition at line 629 of file context.h.

◆ Application

friend class Application
friend

Definition at line 627 of file context.h.

◆ Async

friend class Async
friend

Definition at line 634 of file context.h.

◆ Controller

friend class Controller
friend

Definition at line 633 of file context.h.

◆ DispatchType

friend class DispatchType
friend

Definition at line 630 of file context.h.

◆ Engine

friend class Engine
friend

Definition at line 632 of file context.h.

◆ Plugin

friend class Plugin
friend

Definition at line 631 of file context.h.

Property Documentation

◆ action

◆ actionName

QString Cutelyst::Context::actionName
read

Private name of the current action

Definition at line 51 of file context.h.

Referenced by Cutelyst::ValidatorRule::debugString(), and Cutelyst::ValidatorRule::defaultValue().

◆ config

QVariantMap Cutelyst::Context::config
read

Mapping for all configuration entries read from the Cutelyst configuration section.

See also
Configure your application

Definition at line 86 of file context.h.

◆ controller

Controller * Cutelyst::Context::controller
read

The current controller.

Definition at line 75 of file context.h.

◆ controllerName

QString Cutelyst::Context::controllerName
read

The current controller name.

Definition at line 79 of file context.h.

Referenced by Cutelyst::ValidatorRule::debugString(), and Cutelyst::ValidatorRule::defaultValue().

◆ namespace

QString Cutelyst::Context::namespace
read

The namespace of the current action.

See also
ns()

Definition at line 61 of file context.h.

◆ ns

QString Cutelyst::Context::ns
read

The namespace of the current action.

See also
ns()

Definition at line 56 of file context.h.

Referenced by getAction(), and getActions().

◆ req

Request * Cutelyst::Context::req
read

◆ request

◆ state

bool Cutelyst::Context::state
read

Holds the return value of the last executed action.

Definition at line 90 of file context.h.

Referenced by setState().