From 2dff596a4b00b97efc223c56099722985beea5d8 Mon Sep 17 00:00:00 2001 From: vikin <986465329@qq.com> Date: Sun, 18 Sep 2016 11:22:20 +0800 Subject: [PATCH 1/3] create routing controller --- routing/controller | 53 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 routing/controller diff --git a/routing/controller b/routing/controller new file mode 100644 index 00000000..aa6b7932 --- /dev/null +++ b/routing/controller @@ -0,0 +1,53 @@ +--- +currentMenu: routing-controller +--- + +# Introduction + +Instead of defining all of your request handling logic as Closures in route files, you may wish to organize this behavior +using Controller classes. Controllers can group related request handling logic into a single class. Controllers are stored +in the Sources/App/Controllers directory. + +# Basic Controller + +## Defining Controllers + +```swift +import Vapor +import HTTP + +final class FirstController { + func index(request: Request) throws -> ResponseRepresentable { + return try JSON(node: [ + "message": "This is FirstController's index method" + ]) + } +} +``` +You can define a route to this controller action like so: + +```swift +drop.get("getindex") {request in + return try FirstController().index(request: request) +} +``` +Now, when a request matches the specified route URI, the Index method on the FirstController +class will be executed. Of course, the route parameters will also be passed to the method. + +-------- + +# Resource Controllers + +Vapor resource routing assigns the typical "CRUD" routes to a controller with a single line of code. + +```swift +drop.resource("URI", Controller()) +``` +This single route declaration creates multiple routes to handle a variety of actions on the resource. +The generated controller will already have methods stubbed for each of these actions, including +notes informing you of the HTTP verbs and URIs they handle. + +| Verb | URI | Action| +| ------------- |:-------------:| -----:| + + From 739cb89630bb104c1b648c8ef747e662c11bc9d1 Mon Sep 17 00:00:00 2001 From: vikin <986465329@qq.com> Date: Tue, 18 Oct 2016 10:15:17 +0800 Subject: [PATCH 2/3] highlighted path --- routing/controller | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/routing/controller b/routing/controller index aa6b7932..e1a8393e 100644 --- a/routing/controller +++ b/routing/controller @@ -6,7 +6,7 @@ currentMenu: routing-controller Instead of defining all of your request handling logic as Closures in route files, you may wish to organize this behavior using Controller classes. Controllers can group related request handling logic into a single class. Controllers are stored -in the Sources/App/Controllers directory. +in the `Sources/App/Controllers` directory. # Basic Controller From aa4d71d879c2a222ec7dc7a1e3aa51d855fe30f5 Mon Sep 17 00:00:00 2001 From: vikin <986465329@qq.com> Date: Fri, 21 Oct 2016 00:06:53 +0800 Subject: [PATCH 3/3] Resource table finished --- routing/controller | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/routing/controller b/routing/controller index e1a8393e..6abc2dd9 100644 --- a/routing/controller +++ b/routing/controller @@ -47,7 +47,28 @@ This single route declaration creates multiple routes to handle a variety of act The generated controller will already have methods stubbed for each of these actions, including notes informing you of the HTTP verbs and URIs they handle. -| Verb | URI | Action| -| ------------- |:-------------:| -----:| +| Verb | URI | Action | +| :-------------: | :-------------: | :-----------: | +| GET | test/index | test.index | +| POST | test/create | test.create | +| GET | test/show | test.show | +| PUT | test/replace | test.replace | +| PATCH | test/destroy | test.destroy | +| DELETE | test/destroy | test.destroy | +| DELETE | test/clear | test.clear | +You can also custom method name, add `makeResource` method in the controller +```swift + func makeResource() -> Resource { + return Resource( + index: index, + store: create, + show: show, + replace: replace, + modify: update, + destroy: delete, + clear: clear + ) + } +```