{"metadata":{"image":[],"title":"","description":""},"api":{"url":"","auth":"required","results":{"codes":[]},"settings":"","params":[]},"next":{"description":"","pages":[]},"title":"Providers","type":"basic","slug":"providers","excerpt":"","body":"Most Vapor extension packages will come with a public Provider class that you can append to the providers array. When the application boots, the Provider will be given a chance to apply any necessary adjustments.\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Adding Providers\"\n}\n[/block]\nProviders are passed to the Application's initializer.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"import VaporMustache\\nimport Vapor\\n\\nlet app = Application(providers: [\\n    VaporMustache.Provider()\\n])\\n\\n...\\n\\napp.start()\",\n      \"language\": \"swift\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Creating Providers\"\n}\n[/block]\nThe provider protocol has a number of optional properties that can be set when the provider is initializer. If set, the property will override the application's default.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"public protocol Provider {\\n    func boot(with application: Application)\\n    var server: ServerDriver.Type? { get }\\n    var router: RouterDriver? { get }\\n    var session: SessionDriver? { get }\\n    var hash: HashDriver? { get }\\n    var console: ConsoleDriver? { get }\\n}\\n\",\n      \"language\": \"swift\"\n    }\n  ]\n}\n[/block]\nProviders should use the `boot` function to do any setup or configuration necessary to provide.\n\nTo learn more about the various types of drivers, read their documentation in Vapor's source code or `Command+Click` on them in Xcode.\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Example Provider\"\n}\n[/block]\nAn example provider that supplies a \"super fast server\" is shown. This assumes the server conforms to Vapor's server driver protocol.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"import SuperFastServer\\nimport Vapor\\n\\npublic class SuperFastServerProvider: Vapor.Provider {\\n    let server: Vapor.ServerDriver.Type?\\n    \\n    init() {\\n        server = SuperFastServer.Server.Type\\n    }\\n    \\n    func boot(with application: Vapor.Application) {\\n        app.console.output(\\\"Custom server added.\\\", style: .info)    \\n    }\\n}\",\n      \"language\": \"swift\"\n    }\n  ]\n}\n[/block]\nUsing this new provider would look like the following.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"import Vapor\\nimport VaporSuperFastServer\\n\\nlet app = Application(providers: [\\n    VaporSuperFastServer.Provider()\\n])\\n\\napp.start() // output, \\\"Custom server added.\\\"\",\n      \"language\": \"swift\"\n    }\n  ]\n}\n[/block]","updates":[],"order":7,"isReference":false,"hidden":false,"sync_unique":"","link_url":"","link_external":false,"_id":"5768701b72cd4c0e000fad24","user":"57086f3d447c410e00221bd1","version":{"version":"0.12","version_clean":"0.12.0","codename":"Client Clyde","is_stable":false,"is_beta":true,"is_hidden":false,"is_deprecated":true,"categories":["5768701b72cd4c0e000fad19","5768701b72cd4c0e000fad1a","5768701b72cd4c0e000fad1b","5768701b72cd4c0e000fad1c"],"_id":"5768701b72cd4c0e000fad18","releaseDate":"2016-06-20T22:37:15.641Z","__v":1,"createdAt":"2016-06-20T22:37:15.641Z","project":"57086fe855a4690e005de407"},"githubsync":"","parentDoc":null,"__v":0,"category":{"sync":{"isSync":false,"url":""},"pages":[],"title":"Guide","slug":"getting-started","order":1,"from_sync":false,"reference":false,"_id":"5768701b72cd4c0e000fad1a","createdAt":"2016-04-09T04:31:52.489Z","__v":0,"version":"5768701b72cd4c0e000fad18","project":"57086fe855a4690e005de407"},"createdAt":"2016-06-13T16:03:28.228Z","project":"57086fe855a4690e005de407"}
Most Vapor extension packages will come with a public Provider class that you can append to the providers array. When the application boots, the Provider will be given a chance to apply any necessary adjustments. [block:api-header] { "type": "basic", "title": "Adding Providers" } [/block] Providers are passed to the Application's initializer. [block:code] { "codes": [ { "code": "import VaporMustache\nimport Vapor\n\nlet app = Application(providers: [\n VaporMustache.Provider()\n])\n\n...\n\napp.start()", "language": "swift" } ] } [/block] [block:api-header] { "type": "basic", "title": "Creating Providers" } [/block] The provider protocol has a number of optional properties that can be set when the provider is initializer. If set, the property will override the application's default. [block:code] { "codes": [ { "code": "public protocol Provider {\n func boot(with application: Application)\n var server: ServerDriver.Type? { get }\n var router: RouterDriver? { get }\n var session: SessionDriver? { get }\n var hash: HashDriver? { get }\n var console: ConsoleDriver? { get }\n}\n", "language": "swift" } ] } [/block] Providers should use the `boot` function to do any setup or configuration necessary to provide. To learn more about the various types of drivers, read their documentation in Vapor's source code or `Command+Click` on them in Xcode. [block:api-header] { "type": "basic", "title": "Example Provider" } [/block] An example provider that supplies a "super fast server" is shown. This assumes the server conforms to Vapor's server driver protocol. [block:code] { "codes": [ { "code": "import SuperFastServer\nimport Vapor\n\npublic class SuperFastServerProvider: Vapor.Provider {\n let server: Vapor.ServerDriver.Type?\n \n init() {\n server = SuperFastServer.Server.Type\n }\n \n func boot(with application: Vapor.Application) {\n app.console.output(\"Custom server added.\", style: .info) \n }\n}", "language": "swift" } ] } [/block] Using this new provider would look like the following. [block:code] { "codes": [ { "code": "import Vapor\nimport VaporSuperFastServer\n\nlet app = Application(providers: [\n VaporSuperFastServer.Provider()\n])\n\napp.start() // output, \"Custom server added.\"", "language": "swift" } ] } [/block]