class \mpf\WebApp

class \mpf\WebApp

Default class used for websites created with MPF Framework.

This should be instantiated in the index.php file, for more details about that you can also read the description for \mpf\base\App.

One extra config option that can be sent as a param to \mpf\base\App::run() is "accessMap":


App::run(array(
'accessMap' => new AccessMap(['map' => include(APP_ROOT . 'config' . DIRECTORY_SEPARATOR . 'accessmap.php')]),
'startTime' => microtime(true),
'autoload' => $autoload
));

Default used AccessMap is \mpf\web\AccessMap class that requires a 'map' config option that has the full path to accessmap.php file. More details about the accessmap.php file can be found on the \mpf\web\AccessMap class description page.

When the ::run() method is executed it will call ::start() method from this class and that runs the entire website.

You can use ::get() method to access the instantiated object of this class. That offers access to two new components: - request() - Access to the implementation of \mpf\interfaces\HtmlRequestInterface, default is : \mpf\web\request\HTML. - user() - Access to the implementation of \mpf\interfaces\ActiveUserInterface, default is : \app\components\ActiveUser. An class that can be modified by the developer that also extends, by default, \mpf\web\ActiveUser class.

Usage examples:


use \mpf\WebApp;
if (WebApp::get()->hasAccessTo('user', 'profile')){
    echo "<a href='" . WebApp::get()->request()->createURL('user', 'profile') . "'>Welcome " . WebApp::get()->user()->name . "!</a>";
}
// this will display a link to user profile if it has access to it.

Attributes
public array $controllerAliases = []

List of aliases for selected controllers. For example:


[
     'dev' => '\mpf\web\dev\Controller'
]
When a url requires an alias then it will use the selected class instead.

public string $requestClass = "\mpf\web\request\HTML"

Name of the class used to handle HTML requests.

Must implement \mpf\interfaces\HtmlRequestInterface in order to be accepted!

public string $activeUserClass = "\app\components\ActiveUser"

Class name for ActiveUser object.

Must implement \mpf\interfaces\ActiveUserInterface in order to be accepted!

public string $controllersNamespace = "controllers"

Name of the controllers namespace. By default it's "controllers". Same name must be used by the folders from both app folder and module folder.

public string $modulesNamespace = "modules"

Name of the modules namespace. Same name must be used by folder that contains all modules that don't have a specific namespace specified.

public string[] $pageNotFound = ["special","notFound"]

List of Controller & Action replacement page in case that the requeste page was not found.

public string[] $pageAccessDenied = ["special","accessDenied"]

List of Controller & Action where user is redirected if it tries to access a page without the required rights;

public string[] $pageLogin = ["user","login"]

List of Controller & Action where user is redirected when it tries to access a restricted page but it's not logged in

public \mpf\web\AccessMap $accessMap

Object to be used as access map.

Must implement \mpf\interfaces\AccessMapInterface to be accepted!

public bool $logMissingPages = false

Create a log entry when someone accesses an unknown page.

private \mpf\web\Controller $_controller

Link to active controller

protected array $currentControllerAliasConfig = []

If ::getControllerClassFromNameAndModule() matches an alias then this will record the config for it;

Methods

protected mixed start ()

This method will load the controller based on the Request and execute it;

protected string calculateControllerClassFromRights ()

Get controller class by checking if user has rights to access current controller or not.

private \mpf\web\Controller loadController (string $class)

  • $class

    -no description found-

Instantiate controller and check if class is correct;It will also check for current alias config and it will sent it to constructor

public \mpf\web\Controller getController ()

Return an instance of active controller

public string getControllerClassFromNameAndModule (string $controller, string $module)

  • $controller

    name of the controller

  • $module

    name of the module

Returns full namespace and classname for selected controller. Controller name is modified with ucfirst() method. Also 'app' it's added as a vendor name in namespace and $this->controllersNamespace as subnamespace. In case of modules, if there are no aliases for selected module then modulesNamespace it's added and then module name plus controllersNamespace, in case an alias it's found, then that alias it's used instead of 'app', modulesNamespace and module name .

Examples: Controller: home Module : - Result : \app\controllers\Home

Controller: home Module: admin Result: \app\modules\admin\controllers\Home

Controller: home Module: chat Alias for chat: outsidevendor\chatModule Result: \outsidevender\chatModule\controllers\Home

protected string getPageNotFound ()

Return controller class for in case that page was not found!

protected string getPageAccessDenied ()

Return class name to access denied controller

protected string getPageLogin ()

Return class name to login controller

public \mpf\interfaces\HtmlRequestInterface request ()

-no description found-

public \app\components\ActiveUser user ()

Get activeUser class;

public \mpf\WebApp useAccessMap (\mpf\interfaces\AccessMapInterface $mapObject)

  • $mapObject

    -no description found-

Set a new object to be used as access map. Object must implement \mpf\interfaces\AccessMapInterface

public bool hasAccessTo (string $controller, string $action, string $module = null)

  • $controller

    name of the controller to be checked

  • $action

    name of the action to be checked

  • $module

    name of the module to be checked. If it's base website use '/' as value. If null is sent then it will use active module

Checks access to a specific controller and action.