Skip to content

Latest commit

 

History

History
97 lines (71 loc) · 2.32 KB

README.md

File metadata and controls

97 lines (71 loc) · 2.32 KB

Ziggy for Vapor

A better way to organize your routes.

Swift Version Vapor Version GitHub license

Installation

Add Ziggy to the package dependencies (in your Package.swift file):

dependencies: [
    ...,
    .package(url: "https://github.com/m1guelpf/ziggy-vapor.git", from: "1.0.0")
]

as well as to your target (e.g. "App"):

targets: [
    ...
    .target(
        name: "App",
        dependencies: [... "Ziggy" ...]
    ),
    ...
]

Getting started 🚀

Import Ziggy in your configure.swift file, then call the setup method:

// Sources/App/configure.swift
import Ziggy

// configures your application
public func configure(_ app: Application) async throws {
    // ...

    app.ziggy.setup()

    // ...
}

Then, on your routes.swift file (or wherever you define your routes), you can chain the name method to your routes to give them a name:

// Sources/App/routes.swift
import Vapor
import Ziggy

public func routes(_ app: Application) throws {
    app.get { req in
        return req.view.render("dashboard")
    }.name("dashboard")

    // ...
}

You can then use the app.route (or req.route) function to generate URLs for your routes:

let url = app.route("home") // /dashboard
let edit_user = req.route("users.edit", 1) // /users/1/edit

return req.redirect(route: "user.profile", "m1guelpf") // Redirects to /@m1guelpf

You can also access the route function on your frontend, by adding the routes tag to your HTML template and installing the ziggy-js package:

<!doctype html>
<html lang="en">
	<head>
		<meta charset="utf-8">
        <link rel="stylesheet" href="/app.css" />
        <script type="module" src="/app.js"></script>

		#routes()
	</head>

	<body>
		<!-- ... -->
	</body>
</html>

📄 License

This package is open-sourced software licensed under the MIT license