Skip to main content

Filter

On this page

Filters are controllers that are executed before the main controller. They allow you to monitor and influence web applications.

Registering Filters​

Filters do not differ in their structure from main controllers, but several filters can be executed per request. So you could also register several filters for the same context path.

[routing]

filters[] = "/example > atusch\filter\ExampleFilter"
filters[] = "/example > atusch\filter\SomeOtherFilter"

Just like main controllers, filters usually extend n2n\web\http\controller\ControllerAdapter.

class ExampleFilter extends ControllerAdapter {

public function doFoo(array $params) {
if ($params[0] == 'baz' && '127.0.0.1' != $this->getRequest()->getRemoteIp()) {
throw new ForbiddenException();
}
}
}

The filter above ensures that http://[url-to-n2n-public]/example/foo/baz/... can only be accessed from the local machine.

Controlling Plan​

n2n creates an n2n\web\http\controller\ControllingPlan for every request, which you can access via ControllerAdapter::getControllingPlan(). The controlling plan contains the filters and the main controller that are to be executed for the current request. The controlling plan can be modified at runtime, which can be particularly useful in filters. Via ControllingPlan::abort() you can, for example, abort the execution of the controlling plan.

For example, if you want to protect an area http://[url-to-n2n-public]/secure/... with access control, you could register a filter for it:

[routing]

filters[] = "/secure/** > atusch\filter\AuthorizationFilter"

The following example shows you how you could implement such a filter:

class AuthorizationFilter extends ControllerAdapter {

public function index(LoginContext $loginContext) {
if ($loginContext->hasUser()) {
return;
}

$this->forward('view\login.html', array('loginContext' => $loginContext));
$this->getControllingPlan()->abort();
}
}

This filter checks whether the user is logged in. If this is not the case, it outputs a login view and aborts the execution of the controlling plan. The type LoginContext used in the example is fictional and would have to be created by yourself.

Interceptors​

Filters are registered for context paths in the app.ini. If you want to protect individual controllers or controller methods instead, interceptors are usually the better choice. An interceptor is executed right before the controller or controller method it is attached to and can abort its execution.

Implementing an Interceptor​

The easiest way is to extend n2n\web\http\controller\impl\InterceptorAdapter and implement a method check(). check() is a magic method, so you can have any lookupable objects passed to it. Call abort() to prevent the intercepted controller or controller method from being executed.

<?php
namespace atusch\controller;

use n2n\web\http\controller\impl\InterceptorAdapter;
use atusch\model\LoginContext;

class SecurityInterceptor extends InterceptorAdapter {

public function check(LoginContext $loginContext) {
if ($loginContext->hasUser()) {
return;
}

$this->abort();
$this->forward('view\login.html', array('loginContext' => $loginContext));
}
}

InterceptorAdapter provides the same controlling methods as ControllerAdapter (e.g. forward(), redirect(), dispatch() or getRequest()), so an interceptor can also produce the response itself, as in the example above, where a login view is output instead of the protected content.

Assigning Interceptors​

Interceptors are assigned with the attribute n2n\web\http\attribute\Intercept, which expects the lookup ids (usually the class names) of one or more interceptors. It can be used on the controller class as well as on individual controller methods.

<?php
namespace atusch\controller;

use n2n\web\http\controller\ControllerAdapter;
use n2n\web\http\attribute\Intercept;

#[Intercept(SecurityInterceptor::class)]
class AdminController extends ControllerAdapter {

public function index() {
// only reachable for logged in users
}

#[Intercept(AdminRoleInterceptor::class, AuditInterceptor::class)]
public function doDelete($id) {
// only reachable for logged in users that pass AdminRoleInterceptor and AuditInterceptor
}
}
  • Class: The interceptors are executed before any method of the controller is invoked, including prepare(). If one of them aborts, no method of the controller is executed.
  • Method: The interceptors are executed right before the respective method is invoked. This also works for prepare() and notFound(). If one of them aborts, the method is skipped.

If several interceptors are specified, they are executed in the given order. The execution stops at the first interceptor that aborts.

Invoking Interceptors manually​

Within a controller you can also invoke interceptors yourself via ControllerAdapter::intercept(). It accepts lookup ids as well as Interceptor objects and returns false if one of the interceptors aborted.

public function doEdit($id) {
if (!$this->intercept(SecurityInterceptor::class)) {
return;
}

// code
}