Cutelyst  1.11.0
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 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
 
QString actionName () const
 
Applicationapp () const
 
QVariant config (const QString &key, const QVariant &defaultValue=QVariant()) const
 
QVariantMap config () const
 
Controllercontroller () const
 
Controllercontroller (const QString &name) const
 
QString controllerName () const
 
void detach (Action *action=nullptr)
 
bool detached () const
 
Dispatcherdispatcher () const
 
Engineengine () const
 
Q_DECL_DEPRECATED void * engineData ()
 
bool error () const
 Returns true if an error was set.
 
void error (const QString &error)
 Sets an error string and try to stop.
 
QStringList errors () const
 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=QString())
 
QVector< Action * > getActions (const QString &action, const QString &ns=QString())
 
QLocale locale () const
 
QString ns () const
 
template<typename T >
plugin ()
 Returns the registered plugin that casts to the template type T.
 
QVector< Plugin * > plugins ()
 
Requestreq () const
 
Requestrequest () const
 
Responseres () const
 
Responseresponse () const
 
void setLocale (const QLocale &locale)
 
void setStash (const QString &key, const QVariant &value)
 
void setStash (const QString &key, const ParamsMultiMap &map)
 
void setState (bool state)
 Sets the state of the current executed action, setting to false will make the dispatcher skip non processed actions.
 
bool setView (const QString &name)
 
QStack< Component * > stack () const
 
void stash (const QVariantHash &unite)
 
QVariantHash & stash ()
 
QVariant stash (const QString &key) const
 
bool state () const
 
QString translate (const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const
 
QUrl uriFor (const QString &path=QString(), const QStringList &args=QStringList(), const ParamsMultiMap &queryValues=ParamsMultiMap()) const
 
QUrl uriFor (const QString &path, const ParamsMultiMap &queryValues) const
 
QUrl uriFor (Action *action, const QStringList &captures=QStringList(), const QStringList &args=QStringList(), const ParamsMultiMap &queryValues=ParamsMultiMap()) const
 
QUrl uriFor (Action *action, const ParamsMultiMap &queryValues) const
 
QUrl uriForAction (const QString &path, const QStringList &captures=QStringList(), const QStringList &args=QStringList(), const ParamsMultiMap &queryValues=ParamsMultiMap()) const
 
QUrl uriForAction (const QString &path, const ParamsMultiMap &queryValues) const
 
Viewview () const
 
Viewview (const QString &name) const
 

Protected Member Functions

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

Properties

Action action
 
QString actionName
 
QVariantMap config
 
Controller controller
 
QString controllerName
 
QString namespace
 
QString ns
 
Request req
 
Request request
 
bool state
 

Friends

class Action
 
class Application
 
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 50 of file context.h.

Member Function Documentation

Action* Cutelyst::Context::action ( ) const

Returns a pointer to the current action

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

QString Cutelyst::Context::actionName ( ) const

Returns the private name of the current action

Referenced by res(), and translate().

Application * Context::app ( ) const

Returns the application instance. See Cutelyst::Application

Definition at line 105 of file context.cpp.

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

Returns a configuration value for key with an optional default value

Definition at line 404 of file context.cpp.

References config().

QVariantMap Cutelyst::Context::config ( ) const

Returns a configuration mapping for all configuration read

Referenced by config().

Controller* Cutelyst::Context::controller ( ) const

Returns the current controller

Referenced by dispatcher().

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

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

Definition at line 171 of file context.cpp.

QString Cutelyst::Context::controllerName ( ) const

The current controller name

Referenced by dispatcher().

void Context::detach ( Action action = nullptr)

The same as forward(action), but doesn't return to the previous action when processing is finished. When called with no arguments it escapes the processing chain entirely.

Definition at line 319 of file context.cpp.

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

bool Context::detached ( ) const

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

Definition at line 313 of file context.cpp.

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

Dispatcher * Context::dispatcher ( ) const

Returns the dispatcher instance. See Cutelyst::Dispatcher

Definition at line 153 of file context.cpp.

References controller(), and controllerName().

Engine * Context::engine ( ) const

Returns the engine instance. See Cutelyst::Engine

Definition at line 99 of file context.cpp.

void * Context::engineData ( )

Pointer to internal engine data about the current request.

Note
It's only used by Engines subclasses, Application code should not use it.

Definition at line 416 of file context.cpp.

bool Context::execute ( Component code)
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 328 of file context.cpp.

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

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

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

Gets an action in a given namespace.

Definition at line 340 of file context.cpp.

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

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

Definition at line 346 of file context.cpp.

QLocale Context::locale ( ) const

Returns the current locale to be used when processing Views or translating user messages.

If not explicity set by setLocale it will use the QLocale::setDefault(), or QLocale::system() if not set.

Definition at line 392 of file context.cpp.

Referenced by Cutelyst::GrantleeView::render(), and setLocale().

QString Cutelyst::Context::ns ( ) const

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'

Referenced by res().

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

Returns all registered plugins

Definition at line 352 of file context.cpp.

Request* Cutelyst::Context::req ( ) const

Short for request()

Referenced by Context(), and res().

Request* Cutelyst::Context::request ( ) const

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

Referenced by res().

Response * Context::res ( ) const
Response * Context::response ( ) const
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 398 of file context.cpp.

References locale().

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

A convenient method to set a single ParamsMultiMap to the stash

Definition at line 214 of file context.cpp.

bool Context::setView ( 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 189 of file context.cpp.

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

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

Definition at line 220 of file context.cpp.

Referenced by Cutelyst::Dispatcher::dispatchers(), and translate().

void Cutelyst::Context::stash ( const QVariantHash &  unite)
inline

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

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

Definition at line 499 of file context.h.

Referenced by Cutelyst::ViewEmailTemplate::render(), Cutelyst::ClearSilver::render(), Cutelyst::GrantleeView::render(), Cutelyst::ViewJson::render(), Cutelyst::ViewEmail::render(), and Cutelyst::ViewEmailTemplate::setDefaultView().

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

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

A convenient method to retrieve a single value from the stash

Definition at line 202 of file context.cpp.

bool Cutelyst::Context::state ( ) const

Contains the return value of the last executed action.

Referenced by errors(), and setState().

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."));
}

Definition at line 422 of file context.cpp.

References actionName(), Cutelyst::Component::name(), Cutelyst::Component::reverse(), and stack().

Referenced by Cutelyst::Dispatcher::dispatch(), Cutelyst::Memcached::errorString(), and Cutelyst::GrantleeView::render().

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

Referenced by Cutelyst::ActionREST::doExecute(), uriFor(), and uriForAction().

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 502 of file context.h.

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

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

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 505 of file context.h.

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);
};

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

Definition at line 298 of file context.cpp.

References action(), and uriFor().

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 508 of file context.h.

View * Context::view ( ) const

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

Definition at line 177 of file context.cpp.

Referenced by Cutelyst::RenderView::doExecute(), and Cutelyst::ViewEmailTemplate::setDefaultView().

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

Returns the view with name name

Definition at line 183 of file context.cpp.