Welcome to Trishul API Questions and Answers. Help and learn how to use it efficiently.
0 like 0 dislike
13 views
in Middlewares by (570 points)

Hello Developers, all of us know the value of middlewares in any framework.

Some people call it interceptors, in spring boot it is called filterchain. In frameworks like laravel, TrishulAPI it is called middleware.

Work of Middleware:

To intercept the incoming request and outgoing response.

Use Case:

AuthMiddleware: To authenticate the request before it reaches to any controller.
LogMiddleware: To log the incoming request and outgoing response.
How to create middleware in TrishulApi?

1 Answer

0 like 0 dislike
by (570 points)
 
Best answer

Once again I would like to welcome you all.

To create middlwares in trishulapi, you must follow the below steps:

  • It is good practice that you keep all your middlewares in a separate folder/package.
  • create a folder middlewares
  • create a class LogMiddleware
  • implement MiddlewareInterface from TrishulApi\Core\Middleware\MiddlewareInterface
  • it provides two methods for implementation handle_request(TrishulApi\Core\Http\Request $request):TrishulApi\Core\Http\Request and handle_response(TrishulApi\Core\Http\Response $response):TrishulApi\Core\Http\Response
  • in handle_request() function you will get the object of Request class so you can add any logic for logging.
  • in handle_response() function, you will get the object of Response class so you can add any login for logging.
Note: LoggerFactory class is provided by TrishulApi to help logging in the application.
Welcome to TrishulAPI Q&A, where you can ask questions and receive answers from other members of the community.
...