cutelyst  3.7.0
A C++ Web Framework built on top of Qt, using the simple approach of Catalyst (Perl) framework.
Public Slots | Public Member Functions | Protected Member Functions | Properties | Friends | List of all members
Cutelyst::Context Class Reference

The Cutelyst Context. More...

#include <Cutelyst/Context>

Inheritance diagram for Cutelyst::Context:
Inheritance graph
[legend]

Public Slots

void finalize ()
 finalize the request right away this is automatically called at the end of the actions chain
 

Public Member Functions

 Context (Application *app)
 Constructs a new DUMMY Context object that is child of Application This currently is experimental to allow non network events (such as database notification) to be able to use our infrastructure.
 
Actionaction () const noexcept
 
QString actionName () const noexcept
 
Applicationapp () const noexcept
 
void attachAsync ()
 attachAsync More...
 
QVariantMap config () const noexcept
 
QVariant config (const QString &key, const QVariant &defaultValue={}) const
 
Controllercontroller () const noexcept
 
Controllercontroller (const QString &name) const
 
QString controllerName () const
 
ViewcustomView () const noexcept
 
void detach (Action *action=nullptr)
 
void detachAsync () noexcept
 
bool detached () const noexcept
 
Dispatcherdispatcher () const noexcept
 
Engineengine () const noexcept
 
bool error () const noexcept
 Returns true if an error was set.
 
void error (const QString &error)
 Sets an error string and try to stop.
 
QStringList errors () const noexcept
 Returns a list of errors that were defined.
 
bool execute (Component *code)
 
bool forward (Component *component)
 
bool forward (const QString &action)
 
ActiongetAction (const QString &action, const QString &ns={}) const
 
QVector< Action * > getActions (const QString &action, const QString &ns={}) const
 
QLocale locale () const noexcept
 
QString ns () const noexcept
 
template<typename T >
plugin ()
 Returns the registered plugin that casts to the template type T.
 
QVector< Plugin * > plugins () const
 
Requestreq () const noexcept
 
Requestrequest () const noexcept
 
Responseres () const noexcept
 
Responseresponse () const noexcept
 
bool setCustomView (const QString &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
 Sets the state of the current executed action, setting to false will make the dispatcher skip non processed actions.
 
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=QStringList(), const QStringList &args=QStringList(), const ParamsMultiMap &queryValues=ParamsMultiMap()) const
 
QUrl uriFor (const QString &path, const ParamsMultiMap &queryValues) const
 
QUrl uriFor (const QString &path=QString(), const QStringList &args=QStringList(), const ParamsMultiMap &queryValues=ParamsMultiMap()) const
 
QUrl uriForAction (const QString &path, const ParamsMultiMap &queryValues) const
 
QUrl uriForAction (const QString &path, const QStringList &captures=QStringList(), const QStringList &args=QStringList(), const ParamsMultiMap &queryValues=ParamsMultiMap()) const
 
Viewview (const QString &name) const
 
Viewview (QStringView name={}) const
 

Protected Member Functions

 Context (ContextPrivate *priv)
 Constructs a new Context object using private implementation.
 

Properties

Actionaction
 
QString actionName
 
QVariantMap config
 
Controllercontroller
 
QString controllerName
 
QString namespace
 
QString ns
 
Requestreq
 
Requestrequest
 
bool state
 

Friends

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

Detailed Description

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

Definition at line 38 of file context.h.

Member Function Documentation

◆ action()

Action * Context::action ( ) const
noexcept

Returns a pointer to the current action

Definition at line 109 of file context.cpp.

◆ actionName()

QString Context::actionName ( ) const
noexcept

Returns the private name of the current action

Definition at line 115 of file context.cpp.

◆ app()

Application * Context::app ( ) const
noexcept

Returns the application instance. See Cutelyst::Application

Definition at line 91 of file context.cpp.

Referenced by Context().

◆ attachAsync()

void Context::attachAsync ( )

The scoped

See also
Async class can make handlying async requests easier.

Reattaches to the remaining actions

Definition at line 355 of file context.cpp.

References execute(), and finalize().

◆ config() [1/2]

QVariantMap Context::config ( ) const
noexcept

Returns a configuration mapping for all configuration read

Definition at line 471 of file context.cpp.

◆ config() [2/2]

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

Returns a configuration value for key with an optional default value

Definition at line 465 of file context.cpp.

◆ controller() [1/2]

Controller * Context::controller ( ) const
noexcept

Returns the current controller

Definition at line 151 of file context.cpp.

◆ controller() [2/2]

Controller * Context::controller ( const QString &  name) const

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

Definition at line 157 of file context.cpp.

◆ controllerName()

QString Cutelyst::Context::controllerName ( ) const

The current controller name

Definition at line 145 of file context.cpp.

◆ customView()

View * Context::customView ( ) const
noexcept

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

Definition at line 163 of file context.cpp.

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

◆ detach()

void Context::detach ( Action action = nullptr)

The same as forward(action)

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

Definition at line 339 of file context.cpp.

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

See also
Async class can make handlying 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 349 of file context.cpp.

◆ detached()

bool Context::detached ( ) const
noexcept

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

Definition at line 333 of file context.cpp.

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

◆ dispatcher()

Dispatcher * Context::dispatcher ( ) const
noexcept

Returns the dispatcher instance. See Cutelyst::Dispatcher

Definition at line 139 of file context.cpp.

◆ engine()

Engine * Context::engine ( ) const
noexcept

Returns the engine instance. See Cutelyst::Engine

Definition at line 85 of file context.cpp.

◆ execute()

bool Context::execute ( Component code)

◆ 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.

forward 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 387 of file context.cpp.

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

◆ forward() [2/2]

bool Context::forward ( const QString &  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.

forward 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 393 of file context.cpp.

◆ getAction()

Action * Context::getAction ( const QString &  action,
const QString &  ns = {} 
) const

Gets an action in a given namespace.

Definition at line 399 of file context.cpp.

◆ getActions()

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

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

Definition at line 405 of file context.cpp.

◆ locale()

QLocale Context::locale ( ) const
noexcept

◆ ns()

QString Context::ns ( ) const
noexcept

Returns the namespace of the current action. i.e. the URI prefix corresponding to the controller of the current action. For example: // a class named FooBar which inherits Controller c->ns(); // returns 'foo/bar'

Definition at line 121 of file context.cpp.

◆ plugins()

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

Returns all registered plugins

Definition at line 411 of file context.cpp.

◆ req()

Request * Context::req ( ) const
noexcept

Short for request()

Definition at line 133 of file context.cpp.

◆ request()

Request * Context::request ( ) const
noexcept

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

Definition at line 127 of file context.cpp.

◆ res()

Response * Context::res ( ) const
noexcept

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

Definition at line 103 of file context.cpp.

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

◆ response()

Response * Context::response ( ) const
noexcept

◆ setCustomView()

bool Context::setCustomView ( const QString &  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 181 of file context.cpp.

◆ setLocale()

void Context::setLocale ( const QLocale &  locale)

Defines the current locale to be used when processing Views 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, and or use the chained dispatcher to first match the locale as in "example.com/pt-br/some_action", and 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.

Definition at line 459 of file context.cpp.

References 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 to the stash

Definition at line 224 of file context.cpp.

◆ setStash() [2/2]

void Context::setStash ( const QString &  key,
const QVariant &  value 
)

◆ stack()

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

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

Definition at line 230 of file context.cpp.

◆ stash() [1/4]

QVariantHash & Context::stash ( )

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 Cutelyst::Plugin::Session for a complete system integrated with Cutelyst).

Definition at line 188 of file context.cpp.

◆ stash() [2/4]

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

A convenient method to retrieve a single value from the stash

Definition at line 194 of file context.cpp.

◆ stash() [3/4]

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

A convenient method to retrieve a single value with a default value from the stash

Definition at line 200 of file context.cpp.

◆ 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 Cutelyst::Plugin::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 546 of file context.cpp.

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::ClearSilver::render(), Cutelyst::CuteleeView::render(), Cutelyst::GrantleeView::render(), Cutelyst::ViewJson::render(), Cutelyst::ViewEmail::render(), Cutelyst::ViewEmailTemplate::render(), Cutelyst::Session::setValue(), Cutelyst::Authentication::user(), Cutelyst::Authentication::userExists(), Cutelyst::Authentication::userInRealm(), Cutelyst::ValidatorIn::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 removed.

Definition at line 212 of file context.cpp.

◆ 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 206 of file context.cpp.

◆ state()

bool Context::state ( ) const
noexcept

Contains the return value of the last executed action.

Definition at line 73 of file context.cpp.

◆ translate()

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

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."));
}
Context(Application *app)
Constructs a new DUMMY Context object that is child of Application This currently is experimental to ...
Definition: context.cpp:30

Definition at line 477 of file context.cpp.

Referenced by Cutelyst::ValidatorEmail::categoryString(), Cutelyst::ValidatorDomain::diagnoseString(), Cutelyst::ValidatorEmail::diagnoseString(), Cutelyst::Dispatcher::dispatch(), Cutelyst::ValidatorPwQuality::errorString(), Cutelyst::Memcached::errorString(), Cutelyst::ValidatorBetween::genericParsingError(), Cutelyst::ValidatorMax::genericParsingError(), Cutelyst::ValidatorMin::genericParsingError(), Cutelyst::ValidatorSize::genericParsingError(), Cutelyst::ValidatorRule::genericParsingError(), Cutelyst::ValidatorAfter::genericParsingError(), Cutelyst::ValidatorBefore::genericParsingError(), Cutelyst::ValidatorBetween::genericValidationDataError(), Cutelyst::ValidatorFileSize::genericValidationDataError(), Cutelyst::ValidatorIn::genericValidationDataError(), Cutelyst::ValidatorMax::genericValidationDataError(), Cutelyst::ValidatorMin::genericValidationDataError(), Cutelyst::ValidatorNotIn::genericValidationDataError(), Cutelyst::ValidatorSize::genericValidationDataError(), Cutelyst::ValidatorRule::genericValidationDataError(), Cutelyst::ValidatorAfter::genericValidationDataError(), Cutelyst::ValidatorBefore::genericValidationDataError(), Cutelyst::ValidatorCharNotAllowed::genericValidationDataError(), Cutelyst::ValidatorRule::genericValidationError(), 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::ValidatorSame::genericValidationError(), Cutelyst::ValidatorSize::genericValidationError(), Cutelyst::ValidatorTime::genericValidationError(), Cutelyst::ValidatorUrl::genericValidationError(), Cutelyst::ValidatorRule::label(), Cutelyst::ValidatorRule::parsingError(), Cutelyst::CuteleeView::render(), Cutelyst::GrantleeView::render(), Cutelyst::ValidatorRule::validationDataError(), and Cutelyst::ValidatorRule::validationError().

◆ uriFor() [1/4]

QUrl Cutelyst::Context::uriFor ( Action action,
const ParamsMultiMap queryValues 
) const
inline

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

Definition at line 540 of file context.h.

◆ uriFor() [2/4]

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

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

If no arguments are provided, 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 282 of file context.cpp.

References Cutelyst::Action::numberOfCaptures(), and uriFor().

◆ uriFor() [3/4]

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

Constructs an absolute QUrl object based on the application root, the provided path, and the additional arguments and query parameters 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 537 of file context.h.

◆ uriFor() [4/4]

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

Constructs an absolute QUrl object based on the application root, the provided path, and the additional arguments and query parameters 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 236 of file context.cpp.

Referenced by uriFor(), and uriForAction().

◆ uriForAction() [1/2]

QUrl Cutelyst::Context::uriForAction ( const QString &  path,
const ParamsMultiMap queryValues 
) const
inline

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

Definition at line 543 of file context.h.

◆ uriForAction() [2/2]

QUrl Context::uriForAction ( const QString &  path,
const QStringList &  captures = QStringList(),
const QStringList &  args = QStringList(),
const ParamsMultiMap queryValues = ParamsMultiMap() 
) const

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->uri_for.

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:90

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

Definition at line 318 of file context.cpp.

References uriFor().

◆ view() [1/2]

View * Context::view ( const QString &  name) const

Returns the view with name name or nullptr if not found

Definition at line 169 of file context.cpp.

◆ view() [2/2]

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

Returns the view with name name or nullptr if not found

Definition at line 175 of file context.cpp.