Controller
Analyzes the user’s request and performs the appropriate processing, then returns the response.
1. Creation of a controller
To create a controller, go to the “myapp/controller/front” folder, then add your file for example “HelloWorldController.php”.
The name of the class must be the same as the name of the file; this class must inherit from the “muuska\controller\ AbstractController” class.
[pastacode lang=”php” manual=”%3C%3Fphp%0Anamespace%20myapp%5Ccontroller%5Cfront%3B%0A%0Ause%20muuska%5Ccontroller%5CAbstractController%3B%0A%0Aclass%20HelloWorldController%20extends%20AbstractController%0A%7B%0A%20%20%20%20%0A%7D%0A” message=”HelloWorldController.php” highlight=”” provider=”manual”/]
Once the class is created, we must provide the means to create an instance of this class. To do this, open the “FrontSubApplication” class (\myapp\FrontSubApplication “and redefine the” createController “method
[pastacode lang=”php” manual=”%3C%3Fphp%0Anamespace%20myapp%3B%0A%0Ause%20muuska%5Cproject%5CAbstractSubApplication%3B%0A%0Aclass%20FrontSubApplication%20extends%20AbstractSubApplication%0A%7B%0A%20%20%20%20public%20function%20createController(%5Cmuuska%5Ccontroller%5CControllerInput%20%24input)%20%7B%0A%20%20%20%20%20%20%20%20%24result%20%3D%20null%3B%0A%20%20%20%20%20%20%20%20if%20(%24input-%3EcheckName(‘hello-world’))%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%24result%20%3D%20new%20%5Cmyapp%5Ccontroller%5Cfront%5CHelloWorldController(%24input)%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20return%20%24result%3B%0A%20%20%20%20%7D%0A%7D%0A” message=”FrontSubApplication.php” highlight=”” provider=”manual”/]
Your controller is finally ready, you just have to enter the URL to access it.
2. Url of a controller
The URL allowing access to a controller must be in the form {baseUrl}/{subAppUrlPath}{lang}/{controllerName}/{action}
– {baseUrl}: variable representing the base url. Example http: localhost/muuska or https: www.domain.com. If the sub-application to which the controller is linked has a value in its configuration for this “host” field, it is this value that will be used.
– {subAppUrlPath}: corresponds to the “url_path” field found in the configuration of the current sub-application followed by “/”. In case the value of “url_path” is empty, you should not add the “/”.
– {lang}: The language code. Example “fr” or “en”. If the sub-application to which the controller is linked to defines in its configuration the field “lang_in_url” with the value “false”, you must not specify the language code in the url.
– {controllerName}: The name of the controller. This value must be lowercase so if the name of the controller is composed of several words (For example “HelloWorld”), you will have to use a hyphen (-) to separate the words (The name of the controller will be “hello-world “). In case you don’t specify it, the name of the controller will match “home”.
– {action}: The name of the action. This value must be in lowercase so if the name of the action is composed of several words (For example “UpdatePassword”), you will have to use a hyphen (-) to separate the words (The name of the controller will be “update -password ”). In case you do not specify it, the action name will be “default”.
Example URL:
http:localhost/muuska/en where {baseUrl} = http: localhost/muuska/, {subAppUrlPath} is empty, {lang} = en, {controllerName} is empty (But the considered value will be “home”), {action} is empty (But the considered value will be “default”).
http: localhost/muuska/en/hello-world where {baseUrl} = http: localhost/muuska/, {subAppUrlPath} is empty, {lang} = en, {controllerName} = hello-world, {action} is empty (But the value considered will be “default”).
http: localhost/muuska/en/hello-world goodbye where {baseUrl} = http: localhost/muuska/, {subAppUrlPath} is empty, {lang} = en, {controllerName} = hello-world, {action} = goodbye .
http: localhost/muuska/admin/en/hello-world where {baseUrl} = http: localhost/muuska/, {subAppUrlPath} = admin, {lang} = en, {controllerName} = hello-world, {action} is empty (But the value considered will be “default”).
http: localhost/muuska/admin/hello-world where {baseUrl} = http: localhost/muuska/, {subAppUrlPath} = admin, {lang} is not specified because in the configuration of the current sub-application, the lang_in_url field has the value “false”, {controllerName} = hello-world, {action} is empty (But the value considered will be “default”).
https: www.domain.com/en/hello-world/goodbye where {baseUrl} = https: www.domain.com, {subAppUrlPath} is empty, {lang} = en, {controllerName} = hello-world, {action } = goodbye.
https: www.api.domain.com/en/hello-world/goodbye where {baseUrl} = https: www.api.domain.com because in the configuration of the current sub-application the “host” field has the value www. api.domain.com, {subAppUrlPath} is empty, {lang} = en, {controllerName} = hello-world, {action} = goodbye.
https: www.domain.com/en/hello-world,
To access the controller you just created, enter the controller url (http: localhost / muuska / en / hello-world) in your browser; You will have a page similar to the following:
The page is still empty.
3. The notion of action
The controller performs processing according to the action specified in the request.
• If the name of the action in the request consists of several words, you must use a hyphen (-) to separate the words. Example “update-password” (http: localhost/muuska/en/hello-world/update-password)
• If no action is specified, the name of the action will have the value “default”
• The method to be executed for processing an action in a controller must be named as follows: process {actionName} where actionName Represents the name of the action in the nomenclature “Camel case” (For example “UpdatePassword” with the first letter of the action in uppercase.
Example: processUpdatePassword, processAdd, processDefault.
For our HelloWorld controller, we will use the processDefault method
[pastacode lang=”php” manual=”%3C%3Fphp%0Anamespace%20myapp%5Ccontroller%5Cfront%3B%0A%0Ause%20muuska%5Ccontroller%5CAbstractController%3B%0A%0Aclass%20HelloWorldController%20extends%20AbstractController%0A%7B%0A%20%20%20%20protected%20function%20processDefault()%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%0A%20%20%20%20%7D%0A%7D%0A%0A” message=”HelloWorldController.php” highlight=”” provider=”manual”/]
To specify the content that will be displayed, add the following instruction:
[pastacode lang=”php” manual=”%24this-%3Eresult-%3EsetContent(App%3A%3AcreateHtmlString(‘Hello%20world!’))%3B” message=”HelloWorldController.php” highlight=”” provider=”manual”/]
The complete code for the HelloWorld class will be as follows:
[pastacode lang=”php” manual=”%3C%3Fphp%0Anamespace%20myapp%5Ccontroller%5Cfront%3B%0A%0Ause%20muuska%5Ccontroller%5CAbstractController%3B%0Ause%20muuska%5Cutil%5CApp%3B%0A%0Aclass%20HelloWorldController%20extends%20AbstractController%0A%7B%0A%20%20%20%20protected%20function%20processDefault()%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%24this-%3Eresult-%3EsetContent(App%3A%3AcreateHtmlString(‘Hello%20world!’))%3B%0A%20%20%20%20%7D%0A%7D%0A” message=”HelloWorldController.php” highlight=”” provider=”manual”/]
Added a new action in the controller.
We are going to add the “Say goodbye” action to the HelloWorld controller.
[pastacode lang=”php” manual=”%3C%3Fphp%0Anamespace%20myapp%5Ccontroller%5Cfront%3B%0A%0Ause%20muuska%5Ccontroller%5CAbstractController%3B%0Ause%20muuska%5Cutil%5CApp%3B%0A%0Aclass%20HelloWorldController%20extends%20AbstractController%0A%7B%0A%20%20%20%20protected%20function%20processDefault()%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%24this-%3Eresult-%3EsetContent(App%3A%3AcreateHtmlString(‘Hello%20world!’))%3B%0A%20%20%20%20%7D%0A%20%20%20%20%0A%20%20%20%20protected%20function%20processSayGoodbye()%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%24this-%3Eresult-%3EsetContent(App%3A%3AcreateHtmlString(‘Good%20bye!’))%3B%0A%20%20%20%20%7D%0A%7D%0A” message=”HelloWorldController.php” highlight=”” provider=”manual”/]
Go to your browser and enter the url specifying the action “say-goodbye” (http: localhost/muuska/en/hello-world/say-goodbye).
4. Controller Input
Is an object of type \muuska\controller\ControllerInput provided to the controller at the time of its instantiation. This class contains the resources that the controller needs to perform its processing.
To access this object from your controller, write $ this-> input.
Here is the list of available methods:
getName
Returns the name of the current controller.
[pastacode lang=”php” manual=”protected%20function%20processDefault()%0A%7B%0A%20%20%20%20var_dump(%24this-%3Einput-%3EgetName())%3B%0A%7D%0A” message=”” highlight=”” provider=”manual”/]
getFullName
Returns the full name of the controller.
[pastacode lang=”php” manual=”protected%20function%20processDefault()%0A%7B%0A%20%20%20%20var_dump(%24this-%3Einput-%3EgetFullName())%3B%0A%7D%0A” message=”” highlight=”” provider=”manual”/]
checkName
Checks if the name of the controller is equal to the name you pass in parameter.
checkFullName
Checks if the full name of the controller is equal to the name you pass in parameter.
getAction
Returns the current action.
getRequestType
Returns the type of query.
isAjaxRequest
Check if this is an AJAX request or not. AJAX requests must have an “ajax” parameter in the URL the with the value 1. Example http://localhost/muuska/en/hello-world? Ajax = 1.
hasQueryParam
Checks if a parameter is present in the URL.
[pastacode lang=”php” manual=”protected%20function%20processDefault()%0A%7B%0A%20%20%20%20var_dump(%24this-%3Einput-%3EhasQueryParam(‘my_param’))%3B%0A%7D%0A” message=”” highlight=”” provider=”manual”/]
Add the my_param parameter in the URL to test. (http://localhost/muuska/en/hello-world? my_param = my_value).
getQueryParam
Returns the value of a parameter found in the URL. It takes as a parameter the name of the parameter and the value to return in case the parameter does not exist.
[pastacode lang=”php” manual=”protected%20function%20processDefault()%0A%7B%0A%20%20%20%20var_dump(%24this-%3Einput-%3EgetQueryParam(‘my_param’))%3B%0A%7D%0A” message=”” highlight=”” provider=”manual”/]
hasPostParam
Checks if a parameter is present in the POST.
getPostParam
Returns the value of a parameter found in POST. It takes as a parameter the name of the parameter and the value to return in case the
parameter does not exist.
hasParam
Checks if a parameter is present in the URL or in the POST.
getParam
Returns the value of a parameter found in the URL or in the POST. It takes as a parameter the name of the parameter and the value to return in case the parameter does not exist.
getLang
Returns the code of the current language.
[pastacode lang=”php” manual=”protected%20function%20processDefault()%0A%7B%0A%20%20%20%20var_dump(%24this-%3Einput-%3EgetLang())%3B%0A%7D%0A” message=”” highlight=”” provider=”manual”/]
Modify the language code in the URL and validate.
getLanguageInfo
Returns the instance of the current language. The returned object is of type \muuska\localization\languageInfo.
[pastacode lang=”php” manual=”protected%20function%20processDefault()%0A%7B%0A%20%20%20%20var_dump(%24this-%3Einput-%3EgetLanguageInfo())%3B%0A%7D%09%0A” message=”” highlight=”” provider=”manual”/]
getLanguages
Returns the list of active languages. It returns an array where each element is an object of type muuska\localization languageInfo.
[pastacode lang=”php” manual=”protected%20function%20processDefault()%0A%7B%0A%20%20%20%20var_dump(%24this-%3Einput-%3EgetLanguages())%3B%0A%7D%0A” message=”” highlight=”” provider=”manual”/]
getSubAppName
Returns the name of the current sub-application.
getProject
Returns the instance of the current project. The returned object is of type \muuska\project\Project.
getSubProject
Returns the instance of the current subproject. The returned object is of type \muuska\ project\SubProject.
getSubApplication
Returns the instance of the current sub application. The returned object is of type \muuska\project\SubApplication.
hastheme
Checks if the current sub-application has a theme.
getTheme
Returns the instance of the theme used by the current sub application. It returns “null” if the sub application has no theme. The returned object is of type \muuska\util\theme\Theme.
getDAO
Returns the DAO instance according to the definition of the model provided as a parameter. The returned object is of type \muuska\dao\DAO.
getDaoFactory
Returns the data access manager. The returned object is of type \muuska\dao\DAOFactory.
createSelectionConfig
Creates an instance of the data selection configurator. The returned object is of type \muuska\dao\util\SelectionConfig.
createSaveConfig
Create an instance of the data logging configurator. The returned object is of type \muuska\dao\util\SaveConfig.
createDeleteConfig
Creates an instance of the data deletion configurator. The returned object is of type \muuska\dao\util\DeleteConfig.
getRequest
Returns the instance of the user’s request. The returned object is of type \muuska\http\Request.
getResponse
Returns the instance of the user’s response flow. The returned object is of type \muuska\http\Response.
getVisitorInfoRecorder
Returns the object used to record the information of the current visitor (Cookie or Session). The returned object is of type \muuska\http\VisitorInfoRecorder.
getCurrentUser
Returns the manager instance of the current user. The returned object is of type \muuska\security\CurrentUser.
5. Controller result
Is an object of type \muuska\controller\DefaultControllerResult. This object contains the result of performing an action.
To access this object from your controller, write $ this-> result.
Here is the list of available methods:
getTitle
Returns the title of the page.
[pastacode lang=”php” manual=”protected%20function%20processDefault()%0A%7B%0A%20%20%20%20var_dump(%24this-%3Eresult-%3EgetTitle())%3B%0A%7D%0A” message=”” highlight=”” provider=”manual”/]
setTitle
Changes the title of the page.
[pastacode lang=”php” manual=”protected%20function%20processDefault()%0A%7B%0A%20%20%20%20%24this-%3Eresult-%3EsetTitle(‘Hello%20world%20new%20title’)%3B%0A%7D%0A” message=”” highlight=”” provider=”manual”/]
setContent
Modifies the main content of the page. It takes as parameter an object of type \muuska\html\HtmlContent which represents an HTML component. You will see the list of available components in the \muuska\html\namespace.
[pastacode lang=”php” manual=”protected%20function%20processDefault()%0A%7B%0A%20%20%20%20%24this-%3Eresult-%3EsetContent(App%3A%3AcreateHtmlString(‘Hello%20world%20new%20content’))%3B%0A%7D%0A” message=”” highlight=”” provider=”manual”/]
hasContent
Checks if the page has main content.
getContent
Returns the main content of the page. The returned object is of type \muuska\html\HtmlContent. If the main content does not exist it returns “null”
addError
Adds an error message to the page.
[pastacode lang=”php” manual=”protected%20function%20processDefault()%0A%7B%0A%20%20%20%20%24this-%3Eresult-%3EaddError(‘Hello%20world%20error’)%3B%0A%7D%0A” message=”” highlight=”” provider=”manual”/]
addErrors
Adds an error messages to the page.
[pastacode lang=”php” manual=”protected%20function%20processDefault()%0A%7B%0A%20%20%20%20%24this-%3Eresult-%3EaddErrors(array(‘Hello%20world%20error%201’%2C%20’Hello%20world%20error2’))%3B%0A%7D%0A” message=”” highlight=”” provider=”manual”/]
hasErrors
Checks the page for errors.
addSuccess
Adds a success message to the page.
[pastacode lang=”php” manual=”protected%20function%20processDefault()%0A%7B%0A%20%20%20%20%24this-%3Eresult-%3EaddSuccess(‘Hello%20world%20%20success’)%3B%0A%7D%0A” message=”” highlight=”” provider=”manual”/]
addAlert
Adds an alert to the page. It takes the type of alert as a parameter (the types of alerts available are in the \ muuska \ html \ constants \ AlertType class) and the alert message.
[pastacode lang=”php” manual=”protected%20function%20processDefault()%0A%7B%0A%20%20%20%20%24this-%3Eresult-%3EaddAlert(AlertType%3A%3AWARNING%2C%20’Hello%20world%20warning’)%3B%0A%7D%0A” message=”” highlight=”” provider=”manual”/]
addAlerts
Adds several alert messages depending on the type.
[pastacode lang=”php” manual=”protected%20function%20processDefault()%0A%7B%0A%20%20%20%20%24this-%3Eresult-%3EaddAlerts(AlertType%3A%3AINFO%2C%20array(‘Info%201’%2C%20’Info%202’))%3B%0A%7D%0A” message=”” highlight=”” provider=”manual”/]
getAssetSetter
Returns the asset manager (css, js, etc…). The returned object is of type \muuska\asset\AssetSetter.
setRedirection
Fill in a redirect. It takes as parameter an object of type \muuska\http\redirection\Redirection.
hasRedirection
Checks if a redirect has been completed.
6. Controller param
Is an object of type \muuska\controller\param\ControllerParamResolver. This object allows you to define the basic parameters of your controller and easily retrieve their value. Thus, you will no longer have to check yourself if a mandatory parameter (By the identifier of an object at the time of Edit) is present.
To access this object from your controller, write $ this-> paramResolver.
To configure your parameter handler, you need to override the “initParamResolver” method in your controller.
To define a parameter, the \muuska\controller\param\ControllerParamParser interface enumerates the list of expected methods.
There are three types of parameter:
a. Standard parameter
The affected class is \muuska\controller\param\DefaultControllerParamParser.
[pastacode lang=”php” manual=”%3C%3Fphp%0Anamespace%20myapp%5Ccontroller%5Cfront%3B%0A%0Ause%20muuska%5Ccontroller%5CAbstractController%3B%0Ause%20muuska%5Cutil%5CApp%3B%0A%0Aclass%20TestParamController%20extends%20AbstractController%0A%7B%0A%20%20%20%20protected%20function%20initParamResolver()%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%24parsers%20%3D%20array(App%3A%3Acontrollers()-%3EcreateDefaultControllerParamParser(‘name’%2C%20true))%3B%0A%20%20%20%20%20%20%20%20%24this-%3EparamResolver%20%3D%20App%3A%3Acontrollers()-%3EcreateDefaultControllerParamResolver(%24this-%3Einput%2C%20%24this-%3Eresult%2C%20%24parsers)%3B%0A%20%20%20%20%7D%0A%20%20%20%20%0A%20%20%20%20protected%20function%20processDefault()%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%24param%20%3D%20%24this-%3EparamResolver-%3EgetParam(‘name’)%3B%0A%20%20%20%20%20%20%20%20var_dump(%24param)%3B%0A%20%20%20%20%7D%0A%7D%0A” message=”” highlight=”” provider=”manual”/]
The “processDefault” method does not run because the “name” parameter has not been entered in the URL.
If you add the “name” parameter in the url you will see the content of the “param” variable:
b. Parameter linked to a model
The affected class is \muuska\controller\param\ModelControllerParamParser.
[pastacode lang=”php” manual=”%3C%3Fphp%0Anamespace%20myapp%5Ccontroller%5Cfront%3B%0A%0Ause%20muuska%5Ccontroller%5CAbstractController%3B%0Ause%20muuska%5Cutil%5CApp%3B%0Ause%20myapp%5Cmodel%5CLibraryDefinition%3B%0A%0Aclass%20TestParamController%20extends%20AbstractController%0A%7B%0A%20%20%20%20protected%20function%20initParamResolver()%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%2F*%24parsers%20%3D%20array(App%3A%3Acontrollers()-%3EcreateDefaultControllerParamParser(‘name’%2C%20true))%3B%0A%20%20%20%20%20%20%20%20%24this-%3EparamResolver%20%3D%20App%3A%3Acontrollers()-%3EcreateDefaultControllerParamResolver(%24this-%3Einput%2C%20%24this-%3Eresult%2C%20%24parsers)%3B*%2F%0A%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%24parsers%20%3D%20array(App%3A%3Acontrollers()-%3EcreateModelControllerParamParser(LibraryDefinition%3A%3AgetInstance()%2C%20’id’%2C%20true))%3B%0A%20%20%20%20%20%20%20%20%24this-%3EparamResolver%20%3D%20App%3A%3Acontrollers()-%3EcreateDefaultControllerParamResolver(%24this-%3Einput%2C%20%24this-%3Eresult%2C%20%24parsers)%3B%0A%20%20%20%20%7D%0A%20%20%20%20%0A%20%20%20%20protected%20function%20processDefault()%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%24param%20%3D%20%24this-%3EparamResolver-%3EgetParam(‘id’)%3B%0A%20%20%20%20%20%20%20%20%24library%20%3D%20%24param-%3EgetObject()%3B%0A%20%20%20%20%20%20%20%20var_dump(%24library)%3B%0A%20%20%20%20%7D%0A%7D%0A” message=”TestParamController.php” highlight=”” provider=”manual”/]
The error displayed is due to the fact that there is no library registered with the identifier “10”.
If you enter a valid username, you will see the following display:
7. Controller URL creator
Is an object of type \muuska\url\ControllerUrlCreator. This object is used to create the URLs.
To access this object from your controller, write $ this-> urlCreator.