Jet\MVC_Controller_Router

Having explained the basic meaning and principle of a microrouter, it's time to take a detailed look at the Jet\MVC_Controller_Router class and its Jet\MVC_Controller_Router_Interface.

Overview of methods

Method Meaning of
public __construct(
MVC_Controller $controller
)
It is necessary to pass to the constructor an instance of the controller that the microrouter will serve. The binding is therefore mutual. The controller is to hold the microrouter instance and the microrouter holds the controller instance.
public addAction(
string $controller_action_name,
string $module_action_name = ''
): MVC_Controller_Router_Action
It creates the action and then returns its instance.
So the action is created, but not set - at least the resolver must be defined.

Let's look at the parameters, because there you can already find the way how to verify permissions.
  • controller_action_name Controller action - this was explained in previous chapter.
  • module_action_name The name of the ACL action of the application module. You can learn more in the chapters on modules, but I'll give at least a brief explanation here: Application modules can define their own set of actions/operations. Permissions can then be set using user roles and checking if the logged in user has the necessary permissions and can perform the action. This is again a separate topic to authentication and authorization. Importantly, the microrouter action can be mapped directly to the application module action, and the microrouter can immediately resolve whether the action is accessible to the current user.

    The parameter is not mandatory. If it is not specified, then no permission check is performed.
public getActions(
): MVC_Controller_Router_Action[]
Returns a list of all defined microrouter actions.
public getAction(
string $controller_action_name
): MVC_Controller_Router_Action
Returns the defined action. The action must exist. The method does not check if it is defined. This is the intent, so that when a non-existent action is requested, the application would rather "crash" and not get into an undefined state. The definition of actions and their usage must be consistent.
public action(
string $action_name
): MVC_Controller_Router_Action
This is an alias of the getAction method for shorter writing.
public setDefaultAction(
string $controller_action_name,
string $module_action_name = ''
): MVC_Controller_Router_Action
The microrouter allows you to define a default action. That is, an action that is valid and applied when none of the defined actions have been recognized as relevant.
public getDefaultAction(
): MVC_Controller_Router_Action
Returns an instance of the default action.
public getController(
): MVC_Controller
Returns the controller instance to which the microrouter belongs.
public resolve(
): bool|string
This method performs an evaluation. The method returns either the bool value false or the name of the action to be performed (i.e. string). Let's talk about the evaluation algorithm:
  • If a relevant action is found and the action does not have a module ACL action defined, or if the ACL action is defined but the permission check enables the action, then the action name is returned.
  • If a relevant action is found but the permission check fails, the controller method handleNotAuthorized() is called and false is returned.
  • If no relevant action is found and no default action is defined, false is returned.
  • If no relevant action is found but a default action is defined, then the name of the default action is returned. But this is only assuming that the default action does not have a module ACL action defined, or the action has passed the permissions check. If not, then false is returned and if it does not pass the permission check the controller's handleNotAuthorized() method is called at the same time.

And for completeness, please take a look at the MVC_Controller_Router_Action class.

Previous chapter
Microrouter
Next chapter
Jet\MVC_Controller_Router_Action