Skip to content

Latest commit

 

History

22 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Api Interceptor

BETA. 1.0.* - these are drafts

ApiPlatform does not use controllers. Although the symphony framework is only built on the concept of controller. An interceptor is an attempt to replace controller functions. This is something that is required when you do not have a controller, but only a resource object.

Description

The interceptor method runs at the specified level when the specified request method is called. You can pass some attributes. The Intercept method receives events object. The second parameter is the attributes for the interceptor.

At the moment, you can intercept a specific resource using the Interceptor or any request using the RequestHandler.

Annotations

/**
* @Interceptor("App\Interceptor\ResourceInterceptor")
**/
class ResourceEntity
{
    //
}

class ResourceInterceptor implements InterceptorInterface
{
    /**
     * @Intercept("read", method="PUT")
     */
    public function update()
    {
        //
    }
}

The interceptor class must be declared as a public service. You can set the tag "interceptor" for the interceptor service. You can simply implement the InterceptorInterface interface. Then the tag will be automatically installed and the service will be public.

Interceptor Annotation

Indicates the interceptor class for this resource.

Intercept Annotation

Specified for an interceptor class method. Indicates at which point to intercept execution. And run this method. Сase is not important.

// Parameters
$level;
$method;
$attributes;

Levels

Event Listeners Api-Platform default. Сase is not important

Constant Event Priority
PRE_READ kernel.request 5
POST_READ kernel.request 3
PRE_DESERIALIZE kernel.request 3
POST_DESERIALIZE kernel.request 1
PRE_VALIDATE kernel.view 65
POST_VALIDATE kernel.view 63
PRE_WRITE kernel.view 33
POST_WRITE kernel.view 31
PRE_SERIALIZE kernel.view 17
POST_SERIALIZE kernel.view 15
PRE_RESPOND kernel.view 9
POST_RESPOND kernel.response 0

Initial Shortcut concept

  • Init(REQUEST) - first stage of request
  • Read(POST_DESERIALIZE) - request data reading moment
  • Write(PRE_SERIALIZE) - record data response result
  • Audit(TERMINATE) - after sending a response

Additional(with 0 priority)

  • REQUEST kernel.request
  • VIEW kernel.view
  • RESPONSE kernel.response
  • TERMINATE kernel.terminate

Methods

GET, POST, PUT, PATCH, DELETE

Attributes

Not necessary. Something to pass to the interceptor method.


RequestHandler

Create a request interceptor class. Apply the RequestHandlerInterface interface. You can use the request.handler tag. Set the interceptor annotation to the selected method.

use FreedomSex\ApiInterceptorBundle\Contract\RequestHandlerInterface;

class SomeHandler implements RequestHandlerInterface
{ 
    /**
     * @Intercept("PRE_SERIALIZE", method="GET")
     */
    public function someMethod($event)
    {
        //
    } 
}

About

Api-platform interceptors concept

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages