Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/m1guelpf/ziggy-vapor
Named routes for your Vapor app, both on Swift and on Javascript!
https://github.com/m1guelpf/ziggy-vapor
routing vapor ziggy
Last synced: 2 months ago
JSON representation
Named routes for your Vapor app, both on Swift and on Javascript!
- Host: GitHub
- URL: https://github.com/m1guelpf/ziggy-vapor
- Owner: m1guelpf
- License: mit
- Created: 2024-05-24T04:34:27.000Z (8 months ago)
- Default Branch: main
- Last Pushed: 2024-05-28T18:03:16.000Z (8 months ago)
- Last Synced: 2024-10-30T06:59:14.619Z (2 months ago)
- Topics: routing, vapor, ziggy
- Language: Swift
- Homepage: https://swiftpackageindex.com/m1guelpf/ziggy-vapor
- Size: 8.79 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Ziggy for Vapor
> A better way to organize your routes.
[![Swift Version](https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2Fm1guelpf%2Fziggy-vapor%2Fbadge%3Ftype%3Dswift-versions&color=brightgreen)](http://swift.org)
[![Vapor Version](https://img.shields.io/badge/Vapor-4-30B6FC.svg)](http://vapor.codes)
[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/m1guelpf/ziggy-vapor/main/LICENSE)# Installation
Add `Ziggy` to the package dependencies (in your `Package.swift` file):
```swift
dependencies: [
...,
.package(url: "https://github.com/m1guelpf/ziggy-vapor.git", from: "1.0.0")
]
```as well as to your target (e.g. "App"):
```swift
targets: [
...
.target(
name: "App",
dependencies: [... "Ziggy" ...]
),
...
]
```## Getting started 🚀
Import Ziggy in your `configure.swift` file, then call the `setup` method:
```swift
// 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:
```swift
// Sources/App/routes.swift
import Vapor
import Ziggypublic 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:
```swift
let url = app.route("home") // /dashboard
let edit_user = req.route("users.edit", 1) // /users/1/editreturn 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](https://www.npmjs.com/package/ziggy-js):
```leaf
#routes()
```
## 📄 License
This package is open-sourced software licensed under the [MIT license](http://opensource.org/licenses/MIT)