- Notifications
You must be signed in to change notification settings - Fork0
Named routes for your Vapor app, both on Swift and on Javascript!
License
m1guelpf/ziggy-vapor
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
A better way to organize your routes.
AddZiggy
to the package dependencies (in yourPackage.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"...]),...]
Import Ziggy in yourconfigure.swift
file, then call thesetup
method:
// Sources/App/configure.swiftimport Ziggy// configures your applicationpublicfunc configure(_ app:Application)asyncthrows{ // ... app.ziggy.setup() // ...}
Then, on yourroutes.swift
file (or wherever you define your routes), you can chain thename
method to your routes to give them a name:
// Sources/App/routes.swiftimport Vaporimport Ziggypublicfunc routes(_ app:Application)throws{ app.get{ reqinreturn req.view.render("dashboard")}.name("dashboard") // ...}
You can then use theapp.route
(orreq.route
) function to generate URLs for your routes:
leturl= app.route("home") // /dashboardletedit_user= req.route("users.edit",1) // /users/1/editreturn req.redirect(route:"user.profile","m1guelpf") // Redirects to /@m1guelpf
You can also access theroute
function on your frontend, by adding theroutes
tag to your HTML template and installingtheziggy-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>
This package is open-sourced software licensed under theMIT license
About
Named routes for your Vapor app, both on Swift and on Javascript!