- Notifications
You must be signed in to change notification settings - Fork3
Use your AdonisJs routes in your Inertia.js application
License
izzyjs/route
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Use your AdonisJs routes in JavaScript.
This package provides a JavaScriptroute()
function that can be used to generate URLs for named routes defined in an AdonisJs application.
Install and configure the package using the following command :
node ace add @izzyjs/route
To use theroute()
function in your JavaScript applications, you need to follow these steps:
You can run a command to generate the route definitions from @izzyjs/routes with:
node ace izzy:routes
These type definitions are only needed in a development environment, so they can be generated automatically in the next step.
Add the following line to theadonisrc.ts
file to register the() => import('@izzyjs/route/dev_hook')
ononDevServerStarted
array list.
{// rest of adonisrc.ts fileunstable_assembler:{onBuildStarting:[()=>import('@adonisjs/vite/build_hook')],onDevServerStarted:[()=>import('@izzyjs/route/dev_hook')]// Add this line,}}
Add edge plugin in entry view file@routes
to use theroute()
into javascript.
// resources/views/inertia_layout.edge<!doctype html><html><head> // rest of the file @routes() // Add this line // rest of the file</head><body> @inertia()</body></html>
Now that we've followed all the steps, we're ready to useroute()
on the client side to generate URLs for named routes.
import{route}from'@izzyjs/route/client'consturl=route('users.show',{id:'1'})// /users/1
Is a callback class with a parameter for route(), with information about the method, partern and path itself.
import{route}from'@izzyjs/route/client'consturl=route('users.show',{id:'1'})// /users/1url.method// GETurl.pattern// /users/:idurl.path// /users/1
Is a callback class withoout a parameter for route(), with information about the method, partern and path itself.
The current method returns the current URL of the page or the URL of the page that the user is currently on.
import{route}from'@izzyjs/route/client'// current /users/1route().current()// /users/1route().current('/users/1')// trueroute().current('/users/2')// falseroute().current('users.*')// true
The has method returns a boolean value indicating whether the named route exists in the application.
// start/routes.tsimportrouterfrom'@adonisjs/core/services/router'constusersConstroller=()=>import('#app/controllers/users_controller')router.get('/users',[usersConstroller,'index']).as('users.index')router.get('/users/:id',[usersConstroller,'show']).as('users.show')
import{route}from'@izzyjs/route/client'route().has('users.show')// trueroute().has('users.*')// trueroute().has('dashboard')// false
The params method returns the parameters of the URL of the page that the user is currently on.
import{route}from'@izzyjs/route/client'route().params()// { id: '1' }
These features enable seamless integration of AdonisJs routing within your JavaScript applications, enhancing flexibility and maintainability. By leveraging route(), you can easily manage and navigate your application routes with ease, ensuring a smooth user experience.
Thank you for being interested in making this package better. We encourage everyone to help improve this project with new features, bug fixes, or performance improvements. Please take a little bit of your time to read our guide to make this process faster and easier.
To understand how to submit an issue, commit and create pull requests, check ourContribution Guidelines.
We expect you to follow ourCode of Conduct. You can read it to understand what kind of behavior will and will not be tolerated.
MIT License ©IzzyJs
About
Use your AdonisJs routes in your Inertia.js application