Skip to content
  • Sponsor
  • Notifications You must be signed in to change notification settings
  • Fork 4

Commit 5fe0b01

Browse files
committedJun 13, 2021
add info about use method and function as request handler
1 parent a1aeea2 commit 5fe0b01

File tree

2 files changed

+53
-8
lines changed

2 files changed

+53
-8
lines changed
 

‎working-with-controllers/index.md

+46-2
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ uses
123123
type
124124
125125
TUserController = class(TInterfacedObject, IRequestHandler)
126-
private
126+
public
127127
function createUser(
128128
const request : IRequest;
129129
const response : IResponse;
@@ -147,7 +147,7 @@ type
147147
const response : IResponse;
148148
const args : IRouteArgsReader
149149
) : IResponse;
150-
public
150+
151151
function handleRequest(
152152
const request : IRequest;
153153
const response : IResponse;
@@ -213,6 +213,50 @@ implementation
213213
end;
214214
end.
215215
```
216+
You register route as follows
217+
218+
```
219+
router.any('/users', TUserController.create());
220+
```
221+
222+
## Use class method or function as request handler
223+
Fano Framework provides `TMethodRequestHandler` and `TFuncRequestHandler` adapter class which implements `IRequestHandler` interface to allow use of method or function as request handler. For example, using `TUserController` class above,
224+
225+
```
226+
var
227+
userCtrl : TUserController;
228+
229+
router.get(
230+
'/users/show/{id}',
231+
TMethodRequestHandler.create(@userCtrl.showUser)
232+
);
233+
router.post(
234+
'/users/create',
235+
TMethodRequestHandler.create(@userCtrl.createUser)
236+
);
237+
router.put(
238+
'/users/update',
239+
TMethodRequestHandler.create(@userCtrl.updateUser)
240+
);
241+
```
242+
You can also use ordinary function,
243+
244+
```
245+
function hello(
246+
const request : IRequest;
247+
const response : IResponse;
248+
const args : IRouteArgsReader
249+
) : IResponse;
250+
begin
251+
//handle view user
252+
result := response;
253+
end;
254+
255+
router.get(
256+
'/users/show/{id}',
257+
TFuncRequestHandler.create(@hello)
258+
);
259+
```
216260

217261
## Explore more
218262

‎working-with-router/index.md

+7-6
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ description: Tutorial on how to work with router in Fano Framework
77

88
## Router and route
99

10-
In Fano Framework, a route is an association rule between URL path pattern, HTTP method and code that handles it. Router manages one or more routes and match request URL path, extract data in it and select code that handles it. Router is any class implements `IRouter` interface.
10+
In Fano Framework, a route is an association rule between URL path pattern, HTTP method and code that handles it. Router manages one or more routes and match request URL path, extract data in it and select code that handles it (request handler instance, i.e., [controller](/working-with-controllers)). Router is any class implements `IRouter` interface.
1111

1212
### Creating route for GET method
1313

@@ -98,7 +98,7 @@ If client opens `http://example.com/not/exists` through browser, our application
9898

9999
## <a name="route-builder"></a>Build application routes with route builder
100100

101-
To build application routes, you need to create class that implements `IRouteBuilder` interface.
101+
To build application routes, you need to create class that implements `IRouteBuilder` interface.
102102
```
103103
IRouteBuilder = interface
104104
['{46016416-B249-4258-B76A-7F5B55E8348D}']
@@ -305,9 +305,9 @@ container.add('router', TSimpleRouterFactory.create());
305305
```
306306
container.add('router', TRouterFactory.create());
307307
```
308-
If you need only static URL path pattern, you should use it.
308+
If you need only static URL path pattern, you should use it.
309309

310-
If you create application service provider inherit from `TBasicAppServiceProvider`, it will create default router using `TSimpleRouterFactory` class which is good enough for most applications.
310+
If you create application service provider inherit from `TBasicAppServiceProvider`, it will create default router using `TSimpleRouterFactory` class which is good enough for most applications.
311311

312312
## Replace router instance
313313
If you want to replace router with different implementation, you can override `buildRouter()` method of `TBasicAppServiceProvider`. For example,
@@ -326,19 +326,20 @@ begin
326326
ctnr.add('router', TRouterFactory.create());
327327
result := ctnr['router'] as IRouter;
328328
fRouteMatcher := result as IRouteMatcher;
329-
end;
329+
end;
330330
331331
function TMyAppProvider.getRouteMatcher() : IRouteMatcher;
332332
begin
333333
result := fRouteMatcher;
334-
end;
334+
end;
335335
```
336336
Note that `IRouteMatcher` is interface which is responsible to match request URL and `TRouter` implements it.
337337

338338
## Explore more
339339

340340
- [Dispatcher](/dispatcher).
341341
- [Middlewares](/middlewares).
342+
- [Working with Controller](/working-with-controller).
342343
- [IRouter source](https://github.com/fanoframework/fano/blob/master/src/Router/Contracts/RouterIntf.pas).
343344
- [IRoute source](https://github.com/fanoframework/fano/blob/master/src/Router/Contracts/RouteIntf.pas).
344345
- [IRouteMatcher source](https://github.com/fanoframework/fano/blob/master/src/Router/Contracts/RouteMatcherIntf.pas).

0 commit comments

Comments
 (0)