Ziggy

1.0.2

Named routes for your Vapor app, both on Swift and on Javascript!
m1guelpf/ziggy-vapor

What's New

Improve base URL generation

2024-05-28T18:03:16Z

Full Changelog: v1.0.1...v1.0.2

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="https://github.com/m1guelpf/ziggy-vapor/blob/v1.0.2/app.css" />
        <script type="module" src="https://github.com/m1guelpf/ziggy-vapor/raw/v1.0.2/app.js"></script>

		#routes()
	</head>

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

📄 License

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

Description

  • Swift Tools 5.8.0
View More Packages from this Author

Dependencies

Last updated: Thu May 30 2024 13:35:20 GMT-0900 (Hawaii-Aleutian Daylight Time)