Elgg\ActionsService::register PHP Method

register() public method

See also: elgg_register_action
public register ( $action, $filename = "", $access = 'logged_in' )
    public function register($action, $filename = "", $access = 'logged_in')
    {
        // plugins are encouraged to call actions with a trailing / to prevent 301
        // redirects but we store the actions without it
        $action = rtrim($action, '/');
        if (empty($filename)) {
            $path = __DIR__ . '/../../../actions';
            $filename = realpath("{$path}/{$action}.php");
        }
        if (!in_array($access, self::$access_levels)) {
            _elgg_services()->logger->error("Unrecognized value '{$access}' for \$access in " . __METHOD__);
            $access = 'admin';
        }
        $this->actions[$action] = array('file' => $filename, 'access' => $access);
        return true;
    }

Usage Example

Beispiel #1
0
 public function testCanFilterResponseBuilder()
 {
     $this->hooks->registerHandler('response', 'action:output4', function ($hook, $type, $response, $params) {
         $this->assertEquals('response', $hook);
         $this->assertEquals('action:output4', $type);
         $this->assertEquals($response, $params);
         $this->assertInstanceOf(OkResponse::class, $response);
         $response->setContent('good bye');
         $response->setStatusCode(ELGG_HTTP_BAD_REQUEST);
         return $response;
     });
     $this->assertTrue($this->actions->register('output4', "{$this->actionsDir}/output4.php", 'public'));
     $this->request = $this->prepareHttpRequest('action/output4', 'POST', [], 2, true);
     $this->createService();
     set_input('output', ['foo', 'bar']);
     set_input('system_message', 'success');
     set_input('forward_reason', ELGG_HTTP_OK);
     set_input('forward_url', 'index');
     $this->route();
     $response = _elgg_services()->responseFactory->getSentResponse();
     $this->assertInstanceOf(Response::class, $response);
     $this->assertEquals(ELGG_HTTP_BAD_REQUEST, $response->getStatusCode());
     $this->assertContains('application/json', $response->headers->get('Content-Type'));
     //$this->assertContains('charset=utf-8', strtolower($response->headers->get('Content-Type')));
     $output = json_encode(['error' => 'good bye']);
     $this->assertEquals($output, $response->getContent());
 }