You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: middlewares/index.md
+51
Original file line number
Diff line number
Diff line change
@@ -309,6 +309,8 @@ Fano Framework provides several built-in middlewares.
309
309
-`TNoCacheMiddleware`, middleware class which adds `Cache-Control` response header to prevent browser from caching response.
310
310
-`TStaticFilesMiddleware`, middleware class which serves static files. For more information, read [Serving static files](/working-with-response/serve-static-files).
311
311
-`TThrottleMiddleware`, middleware class which [limits rate of request](/utilities/rate-limit) to one or more routes.
312
+
-`TFuncMiddleware`, adapter middleware class which allow function be used as middleware.
313
+
-`TMethodMiddleware`, adapter middleware class which allow class method be used as middleware.
312
314
313
315
### Group several middlewares as one
314
316
@@ -364,6 +366,55 @@ var helloCtrlMiddleware : IMiddleware;
`TFuncMiddleware` and `TMethodMiddleware` are adapter middlewares that allows function and method of class be used as middleware. It means you can wrap a function and attach it to a route and to be executed as part of middleware chain.
372
+
373
+
For example, if you have following code
374
+
```
375
+
unit mymiddleware;
376
+
377
+
interface
378
+
379
+
uses
380
+
fano;
381
+
382
+
function myAjax(
383
+
const request : IRequest;
384
+
const response : IResponse;
385
+
const args : IRouteArgsReader;
386
+
const next : IRequestHandler
387
+
) : IResponse;
388
+
389
+
implementation
390
+
391
+
function myAjax(
392
+
const request : IRequest;
393
+
const response : IResponse;
394
+
const args : IRouteArgsReader;
395
+
const next : IRequestHandler
396
+
) : IResponse;
397
+
begin
398
+
if (not request.isXhr()) then
399
+
begin
400
+
response.headers().setHeader('Status', '403 Not Ajax Request');
401
+
result := response;
402
+
end else
403
+
begin
404
+
result := next.handleRequest(request, response, args);
405
+
end;
406
+
end;
407
+
408
+
end.
409
+
```
410
+
You can use `myAjax()` as middleware as shown below.
0 commit comments