Download the PHP package vaened/laroute without Composer
On this page you can find all versions of the php package vaened/laroute. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download vaened/laroute
More information about vaened/laroute
Files in vaened/laroute
Package laroute
Short Description An enhanced Laravel extension to export named routes to JavaScript, allowing you to use route names instead of URLs. Supports route grouping for modular applications.
License MIT
Informations about the package laroute
Laroute
vaened\laroute
is a PHP library inspired by aaronlord/laroute, designed to help Laravel developers
manage their application’s routes in JavaScript
without the need to hardcode URLs
. This library goes a step further by introducing
modular routing, allowing you to separate routes based on URL patterns into different modules. Each module generates its own routes file,
making it easier to manage and scale your application’s routing structure.
Installation
Laroute requires PHP 8.2. To get the latest version, simply require the project using Composer:
Publish the configuration file.
Now, generate the routes based on your configuration and export the JavaScript service.
Usage
The library includes the necessary javascript
service to interpret the exported routes, along with its corresponding d.ts
type
definitions
for typescript
.
The location of the service is configured in the library option within the configuration file.
Javascript / Typescript
Once the service is exported to the location defined in the configuration file, you can easily create a new file and export the service creation by passing the file containing the routes as a parameter.
With this, you’re ready to start.
Additionally, you can export the routes as a TypeScript file to enable route autocompletion in your editor. To achieve this, simply modify
the output
option in the configuration file.
Service
The route service provides three key methods to interact with the generated routes:
- generateFullURL(name, ?params): Generates a full URL, including any query string parameters.
Returns
{host}/api/admin/products/80768395-4208-4fd7-ac60-c429717014ab?name=Notebook
- createURLWithoutQuery(name, ?params):Generates a full URL without any query string.
Returns
{host}/api/admin/products/80768395-4208-4fd7-ac60-c429717014ab
- has(name): Checks if a specific route is defined within the service.
Returns
true
if the route exists,false
otherwise.
Best Practices
To efficiently manage API calls, it’s advisable to create a dedicated file. You might name it Router.{ts, js}
. In this file, you can
utilize fetch
or a library like axios
to streamline your API interactions. An example of what you might export could look like this:
Configuration
The default configuration should be sufficient for most projects, allowing you to simply export the routes and start working without any additional setup.
For customization options, refer to the comments in the configuration file laroute.php.
Advanced
For larger projects, where different clients consume the API, you can configure the route export in a modular way. This allows you to create as many modules as needed, each corresponding to different URL segments.
For example, you could have a module for /api/store
and another for /api/admin
, which would generate two separate route files.
Features
- Modular Routing: Define and separate routes by modules based on URL patterns (e.g., /api, /admin, /user), with each module generating a separate JSON file.
- Seamless Integration with Laravel: Automatically generate JavaScript routes from your Laravel routes, ensuring consistency across your application.
- TypeScript Support: Includes a TypeScript-compatible JavaScript file, allowing you to use strongly-typed route definitions in your front-end code.
- Customizable URLs: Generate absolute or relative URLs with customizable prefixes and root URLs, giving you full control over how URLs are constructed.
- Flexible Configuration: Easily configure modules and route matching criteria to fit the needs of your project, whether it’s a monolithic application or a modular one.
License
This library is licensed under the MIT License. For more information, please see the license
file.
All versions of laroute with dependencies
laravel/framework Version ^10.21|^v11.15
vaened/support Version ^4.0
lambdish/phunctional Version ^2.1