Cutelyst
2.13.0
|
The Cutelyst Context. More...
#include <Cutelyst/Context>
Public Slots | |
void | next (bool force=false) |
This method is deprecated and no longer works, creating local event loops leads to crashes. More... | |
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. | |
Action * | action () const |
QString | actionName () const |
Application * | app () const |
void | attachAsync () |
attachAsync More... | |
QVariantMap | config () const |
QVariant | config (const QString &key, const QVariant &defaultValue=QVariant()) const |
Controller * | controller () const |
Controller * | controller (const QString &name) const |
QString | controllerName () const |
View * | customView () const |
void | detach (Action *action=nullptr) |
void | detachAsync () |
bool | detached () const |
Dispatcher * | dispatcher () const |
Engine * | engine () const |
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) |
Action * | getAction (const QString &action, const QString &ns=QString()) const |
QVector< Action * > | getActions (const QString &action, const QString &ns=QString()) const |
QLocale | locale () const |
QString | ns () const |
template<typename T > | |
T | plugin () |
Returns the registered plugin that casts to the template type T . | |
QVector< Plugin * > | plugins () const |
Request * | req () const |
Request * | request () const |
Response * | res () const |
Response * | response () const |
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) |
Sets the state of the current executed action, setting to false will make the dispatcher skip non processed actions. | |
QStack< Component * > | stack () const |
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 |
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 |
View * | view (const QString &name=QString()) const |
Q_DECL_DEPRECATED bool | wait (uint count=1) |
This method is deprecated and no longer works, creating local event loops leads to crashes. More... | |
Protected Member Functions | |
Context (ContextPrivate *priv) | |
Constructs a new Context object using private implementation. | |
void | finalize () |
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 | Controller |
class | DispatchType |
class | Engine |
class | Plugin |
Action* Cutelyst::Context::action | ( | ) | const |
Returns a pointer to the current action
QString Cutelyst::Context::actionName | ( | ) | const |
Returns the private name of the current action
Application * Context::app | ( | ) | const |
Returns the application instance. See Cutelyst::Application
Definition at line 104 of file context.cpp.
Referenced by Context().
void Context::attachAsync | ( | ) |
Reattaches to the remaining actions
Definition at line 363 of file context.cpp.
References execute().
Referenced by Cutelyst::Controller::_DISPATCH().
QVariantMap Cutelyst::Context::config | ( | ) | const |
Returns a configuration mapping for all configuration read
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 461 of file context.cpp.
Controller* Cutelyst::Context::controller | ( | ) | const |
Returns the current controller
Controller * Context::controller | ( | const QString & | name | ) | const |
Returns the controller by name, or nullptr if the controller is not found
Definition at line 170 of file context.cpp.
QString Cutelyst::Context::controllerName | ( | ) | const |
The current controller name
View * Context::customView | ( | ) | const |
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 176 of file context.cpp.
Referenced by Cutelyst::RenderView::doExecute().
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 346 of file context.cpp.
Referenced by Cutelyst::RoleACL::aroundExecute(), Cutelyst::Action::doExecute(), Cutelyst::LangSelect::fromPath(), and Cutelyst::LangSelect::fromUrlQuery().
void Context::detachAsync | ( | ) |
Detaches the processing chain telling the Engine that the request is not finished yet.
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 356 of file context.cpp.
bool Context::detached | ( | ) | const |
Returns true if the last executed Action requested that the processing be escaped.
Definition at line 340 of file context.cpp.
Referenced by Cutelyst::Action::doExecute().
Dispatcher * Context::dispatcher | ( | ) | const |
Returns the dispatcher instance. See Cutelyst::Dispatcher
Definition at line 152 of file context.cpp.
Engine * Context::engine | ( | ) | const |
Returns the engine instance. See Cutelyst::Engine
Definition at line 98 of file context.cpp.
bool Context::execute | ( | Component * | code | ) |
Execute an action. Errors are available via error().
Definition at line 415 of file context.cpp.
References error(), Cutelyst::Component::execute(), Cutelyst::Component::name(), Cutelyst::Component::reverse(), and setState().
Referenced by attachAsync(), Cutelyst::Action::dispatch(), and Cutelyst::Dispatcher::forward().
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 385 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 391 of file context.cpp.
Action * Context::getAction | ( | const QString & | action, |
const QString & | ns = QString() |
||
) | const |
Gets an action in a given namespace.
Definition at line 397 of file context.cpp.
QVector< Action * > Context::getActions | ( | const QString & | action, |
const QString & | ns = QString() |
||
) | const |
Gets all actions of a given name in a namespace and all parent namespaces.
Definition at line 403 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 449 of file context.cpp.
Referenced by Cutelyst::LangSelect::fromPath(), Cutelyst::ValidatorIn::genericValidationError(), Cutelyst::ValidatorInteger::genericValidationError(), Cutelyst::ValidatorSize::genericValidationError(), Cutelyst::ValidatorMax::genericValidationError(), Cutelyst::ValidatorMin::genericValidationError(), Cutelyst::ValidatorBetween::genericValidationError(), Cutelyst::ValidatorBefore::genericValidationError(), Cutelyst::ValidatorAfter::genericValidationError(), Cutelyst::ValidatorFileSize::inputPattern(), Cutelyst::CuteleeView::render(), Cutelyst::GrantleeView::render(), setLocale(), Cutelyst::ValidatorMin::validate(), Cutelyst::ValidatorMax::validate(), Cutelyst::ValidatorSize::validate(), Cutelyst::ValidatorBetween::validate(), and Cutelyst::ValidatorFileSize::validate().
|
slot |
Decreases the local event loop counter created by wait() eventually quitting it's execution if 0 is reached.
If you set force to true it will quit the loop immediately regardless of it's counter.
Definition at line 526 of file context.cpp.
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'
QVector< Cutelyst::Plugin * > Context::plugins | ( | ) | const |
Returns all registered plugins
Definition at line 409 of file context.cpp.
Request* Cutelyst::Context::req | ( | ) | const |
Short for request()
Request* Cutelyst::Context::request | ( | ) | const |
Response * Context::res | ( | ) | const |
Returns the current Cutelyst::Response object, see there for details.
Definition at line 116 of file context.cpp.
Referenced by Cutelyst::RenderView::doExecute(), Cutelyst::LangSelect::fromPath(), Cutelyst::CuteleeView::render(), and Cutelyst::GrantleeView::render().
Response * Context::response | ( | ) | const |
Returns the current Cutelyst::Response object, see there for details.
Definition at line 110 of file context.cpp.
Referenced by Cutelyst::EngineRequest::finalizeBody(), Cutelyst::EngineRequest::finalizeCookies(), Cutelyst::EngineRequest::finalizeError(), Cutelyst::EngineRequest::finalizeHeaders(), and Cutelyst::ViewJson::render().
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 188 of file context.cpp.
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 455 of file context.cpp.
References locale().
Referenced by Cutelyst::LangSelect::fromPath().
void Context::setStash | ( | const QString & | key, |
const ParamsMultiMap & | map | ||
) |
A convenient method to set a single ParamsMultiMap to the stash
Definition at line 231 of file context.cpp.
void Context::setStash | ( | const QString & | key, |
const QVariant & | value | ||
) |
A convenient method to set a single value to the stash
Definition at line 225 of file context.cpp.
Referenced by Cutelyst::SessionStoreFile::deleteSessionData(), Cutelyst::MemcachedSessionStore::deleteSessionData(), Cutelyst::Session::deleteValue(), Cutelyst::Session::deleteValues(), Cutelyst::CSRFProtection::getToken(), Cutelyst::ValidatorFileSize::inputPattern(), Cutelyst::StatusMessage::load(), Cutelyst::ViewEmailTemplate::render(), Cutelyst::GrantleeView::render(), Cutelyst::CuteleeView::render(), Cutelyst::Session::setValue(), Cutelyst::SessionStoreFile::storeSessionData(), Cutelyst::MemcachedSessionStore::storeSessionData(), and Cutelyst::Validator::validate().
QStack< Component * > Context::stack | ( | ) | const |
Returns the internal execution stack (actions that are currently executing).
Definition at line 237 of file context.cpp.
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 195 of file context.cpp.
Referenced by stash().
QVariant Context::stash | ( | const QString & | key | ) | const |
A convenient method to retrieve a single value from the stash
Definition at line 201 of file context.cpp.
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 207 of file context.cpp.
|
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).
Definition at line 558 of file context.h.
References stash().
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::ViewEmailTemplate::render(), Cutelyst::ClearSilver::render(), Cutelyst::ViewJson::render(), Cutelyst::GrantleeView::render(), Cutelyst::CuteleeView::render(), Cutelyst::ViewEmail::render(), Cutelyst::Session::setValue(), Cutelyst::Authentication::user(), Cutelyst::Authentication::userExists(), Cutelyst::Authentication::userInRealm(), Cutelyst::ValidatorIn::validate(), Cutelyst::ValidatorRequiredIfStash::validate(), Cutelyst::ValidatorRequiredUnlessStash::validate(), Cutelyst::ValidatorPwQuality::validate(), and Cutelyst::Session::value().
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 219 of file context.cpp.
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 213 of file context.cpp.
bool Cutelyst::Context::state | ( | ) | const |
Contains the return value of the last executed action.
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().
Definition at line 473 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::ValidatorMax::genericParsingError(), Cutelyst::ValidatorSize::genericParsingError(), Cutelyst::ValidatorMin::genericParsingError(), Cutelyst::ValidatorBetween::genericParsingError(), Cutelyst::ValidatorBefore::genericParsingError(), Cutelyst::ValidatorAfter::genericParsingError(), Cutelyst::ValidatorRule::genericParsingError(), Cutelyst::ValidatorIn::genericValidationDataError(), Cutelyst::ValidatorNotIn::genericValidationDataError(), Cutelyst::ValidatorCharNotAllowed::genericValidationDataError(), Cutelyst::ValidatorSize::genericValidationDataError(), Cutelyst::ValidatorMax::genericValidationDataError(), Cutelyst::ValidatorMin::genericValidationDataError(), Cutelyst::ValidatorBetween::genericValidationDataError(), Cutelyst::ValidatorAfter::genericValidationDataError(), Cutelyst::ValidatorBefore::genericValidationDataError(), Cutelyst::ValidatorFileSize::genericValidationDataError(), Cutelyst::ValidatorRule::genericValidationDataError(), Cutelyst::ValidatorPresent::genericValidationError(), Cutelyst::ValidatorFilled::genericValidationError(), Cutelyst::ValidatorRequired::genericValidationError(), Cutelyst::ValidatorBoolean::genericValidationError(), Cutelyst::ValidatorConfirmed::genericValidationError(), Cutelyst::ValidatorJson::genericValidationError(), Cutelyst::ValidatorDifferent::genericValidationError(), Cutelyst::ValidatorRequiredUnless::genericValidationError(), Cutelyst::ValidatorRequiredWithAll::genericValidationError(), Cutelyst::ValidatorRequiredWithout::genericValidationError(), Cutelyst::ValidatorRequiredWithoutAll::genericValidationError(), Cutelyst::ValidatorRequiredIf::genericValidationError(), Cutelyst::ValidatorRequiredWith::genericValidationError(), Cutelyst::ValidatorRequiredIfStash::genericValidationError(), Cutelyst::ValidatorRegularExpression::genericValidationError(), Cutelyst::ValidatorIn::genericValidationError(), Cutelyst::ValidatorInteger::genericValidationError(), Cutelyst::ValidatorNotIn::genericValidationError(), Cutelyst::ValidatorAccepted::genericValidationError(), Cutelyst::ValidatorRequiredUnlessStash::genericValidationError(), Cutelyst::ValidatorTime::genericValidationError(), Cutelyst::ValidatorNumeric::genericValidationError(), Cutelyst::ValidatorDate::genericValidationError(), Cutelyst::ValidatorDateTime::genericValidationError(), Cutelyst::ValidatorSame::genericValidationError(), Cutelyst::ValidatorMax::genericValidationError(), Cutelyst::ValidatorMin::genericValidationError(), Cutelyst::ValidatorSize::genericValidationError(), Cutelyst::ValidatorUrl::genericValidationError(), Cutelyst::ValidatorBetween::genericValidationError(), Cutelyst::ValidatorCharNotAllowed::genericValidationError(), Cutelyst::ValidatorDigitsBetween::genericValidationError(), Cutelyst::ValidatorDigits::genericValidationError(), Cutelyst::ValidatorAlphaNum::genericValidationError(), Cutelyst::ValidatorAlphaDash::genericValidationError(), Cutelyst::ValidatorAlpha::genericValidationError(), Cutelyst::ValidatorIp::genericValidationError(), Cutelyst::ValidatorAfter::genericValidationError(), Cutelyst::ValidatorBefore::genericValidationError(), Cutelyst::ValidatorFileSize::genericValidationError(), Cutelyst::ValidatorRule::genericValidationError(), Cutelyst::ValidatorRule::label(), Cutelyst::ValidatorRule::parsingError(), Cutelyst::GrantleeView::render(), Cutelyst::CuteleeView::render(), Cutelyst::ValidatorRule::validationDataError(), and Cutelyst::ValidatorRule::validationError().
|
inline |
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 289 of file context.cpp.
References Cutelyst::Action::numberOfCaptures(), and uriFor().
|
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 561 of file context.h.
References uriFor().
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 243 of file context.cpp.
Referenced by uriFor(), and uriForAction().
|
inline |
A convenience method for the uriForAction() without the arguments parameter
Definition at line 567 of file context.h.
References uriForAction().
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:
You can use: c->uriForAction('/users/lst'); and it will create the URI /users/the-list.
Definition at line 325 of file context.cpp.
References uriFor().
Referenced by uriForAction().
View * Context::view | ( | const QString & | name = QString() | ) | const |
Returns the view with name name or nullptr if not found
Definition at line 182 of file context.cpp.
bool Context::wait | ( | uint | count = 1 | ) |
This creates a local event loop that requires next() to be called count
times.
If wait() was already called and didn't return it will increase the counter of the unfinished wait().
Returns true when the event loop finishes and false if the call only increased the event loop counter.
Definition at line 479 of file context.cpp.