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.

 

HelloWorldController.php
<?php
namespace myapp\controller\front;

use muuska\controller\AbstractController;

class HelloWorldController extends AbstractController
{
    
}

 

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

 

FrontSubApplication.php
<?php
namespace myapp;

use muuska\project\AbstractSubApplication;

class FrontSubApplication extends AbstractSubApplication
{
    public function createController(\muuska\controller\ControllerInput $input) {
        $result = null;
        if ($input->checkName('hello-world')) {
            $result = new \myapp\controller\front\HelloWorldController($input);
        }
        return $result;
    }
}

 

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:

test controller

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

 

HelloWorldController.php
<?php
namespace myapp\controller\front;

use muuska\controller\AbstractController;

class HelloWorldController extends AbstractController
{
    protected function processDefault()
    {
        
    }
}

To specify the content that will be displayed, add the following instruction:

HelloWorldController.php
$this->result->setContent(App::createHtmlString('Hello world!'));

The complete code for the HelloWorld class will be as follows:

HelloWorldController.php
<?php
namespace myapp\controller\front;

use muuska\controller\AbstractController;
use muuska\util\App;

class HelloWorldController extends AbstractController
{
    protected function processDefault()
    {
        $this->result->setContent(App::createHtmlString('Hello world!'));
    }
}

test content controller

Added a new action in the controller.
We are going to add the “Say goodbye” action to the HelloWorld controller.

 

HelloWorldController.php
<?php
namespace myapp\controller\front;

use muuska\controller\AbstractController;
use muuska\util\App;

class HelloWorldController extends AbstractController
{
    protected function processDefault()
    {
        $this->result->setContent(App::createHtmlString('Hello world!'));
    }
    
    protected function processSayGoodbye()
    {
        $this->result->setContent(App::createHtmlString('Good bye!'));
    }
}

 

Go to your browser and enter the url specifying the action “say-goodbye” (http: localhost/muuska/en/hello-world/say-goodbye).

test content controller

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.

protected function processDefault()
{
    var_dump($this->input->getName());
}

 

var_dump

getFullName

Returns the full name of the controller.

protected function processDefault()
{
    var_dump($this->input->getFullName());
}

 

getFullName vardump

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.

protected function processDefault()
{
    var_dump($this->input->hasQueryParam('my_param'));
}

 

Add the my_param parameter in the URL to test. (http://localhost/muuska/en/hello-world? my_param = my_value).

 

my param

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.

protected function processDefault()
{
    var_dump($this->input->getQueryParam('my_param'));
}

 

getQueryParam

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.

protected function processDefault()
{
    var_dump($this->input->getLang());
}

 

Modify the language code in the URL and validate.

getLang

getLanguageInfo

Returns the instance of the current language. The returned object is of type \muuska\localization\languageInfo.

protected function processDefault()
{
    var_dump($this->input->getLanguageInfo());
}	

 

getLanguageInfo

getLanguages

Returns the list of active languages. It returns an array where each element is an object of type muuska\localization languageInfo.

protected function processDefault()
{
    var_dump($this->input->getLanguages());
}

 

getLanguages

getSubAppName

Returns the name of the current sub-application.

getSubAppName

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.

protected function processDefault()
{
    var_dump($this->result->getTitle());
}

 

getTitle

setTitle

Changes the title of the page.

protected function processDefault()
{
    $this->result->setTitle('Hello world new title');
}

 

setTitle

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.

 

protected function processDefault()
{
    $this->result->setContent(App::createHtmlString('Hello world new content'));
}

 

setContent

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.

protected function processDefault()
{
    $this->result->addError('Hello world error');
}

 

addError

addErrors

Adds an error messages to the page.

protected function processDefault()
{
    $this->result->addErrors(array('Hello world error 1', 'Hello world error2'));
}

 

addErrors

hasErrors

Checks the page for errors.

addSuccess

Adds a success message to the page.

protected function processDefault()
{
    $this->result->addSuccess('Hello world  success');
}

 

addSuccess

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.

protected function processDefault()
{
    $this->result->addAlert(AlertType::WARNING, 'Hello world warning');
}

 

addAlert

addAlerts

Adds several alert messages depending on the type.

protected function processDefault()
{
    $this->result->addAlerts(AlertType::INFO, array('Info 1', 'Info 2'));
}

 

addAlerts

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.

<?php
namespace myapp\controller\front;

use muuska\controller\AbstractController;
use muuska\util\App;

class TestParamController extends AbstractController
{
    protected function initParamResolver()
    {
        $parsers = array(App::controllers()->createDefaultControllerParamParser('name', true));
        $this->paramResolver = App::controllers()->createDefaultControllerParamResolver($this->input, $this->result, $parsers);
    }
    
    protected function processDefault()
    {
        $param = $this->paramResolver->getParam('name');
        var_dump($param);
    }
}

 

Standard parameter

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:

 

parameter name

b. Parameter linked to a model

The affected class is \muuska\controller\param\ModelControllerParamParser.

TestParamController.php
<?php
namespace myapp\controller\front;

use muuska\controller\AbstractController;
use muuska\util\App;
use myapp\model\LibraryDefinition;

class TestParamController extends AbstractController
{
    protected function initParamResolver()
    {
        /*$parsers = array(App::controllers()->createDefaultControllerParamParser('name', true));
        $this->paramResolver = App::controllers()->createDefaultControllerParamResolver($this->input, $this->result, $parsers);*/
        
        $parsers = array(App::controllers()->createModelControllerParamParser(LibraryDefinition::getInstance(), 'id', true));
        $this->paramResolver = App::controllers()->createDefaultControllerParamResolver($this->input, $this->result, $parsers);
    }
    
    protected function processDefault()
    {
        $param = $this->paramResolver->getParam('id');
        $library = $param->getObject();
        var_dump($library);
    }
}

 

Parameter linked

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:

 

var_dump_error

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.