Download the PHP package svanthuijl/laravel-routable-models without Composer
On this page you can find all versions of the php package svanthuijl/laravel-routable-models. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download svanthuijl/laravel-routable-models
More information about svanthuijl/laravel-routable-models
Files in svanthuijl/laravel-routable-models
Package laravel-routable-models
Short Description This packages allows you to create eloquent models which have their own routes.
License MIT
Informations about the package laravel-routable-models
Associate routes with Eloquent models
This packages allows you to create eloquent models which have their own routes. The routes will automatically be generated when the model is saved and the routes will automatically be registered in the router.
Localized routes
The package also offers support for localized routes for translatable models. This is built on top of "spatie/laravel-translatable".
Installation
This package can be installed through Composer.
Migrations
Migrations are automatically loaded. Optionally, you can publish the migrations of this package with this command:
Config
Optionally, you can publish the config file of this package with this command:
The config file was built to be configured with dotenv variables.
Service provider
The service provider for this package is not discovered automatically. Register the RoutableServiceProvider manually in your config/app.php service providers section.
Important! Make sure that this is the last service provider to be registered since it does check for previously registered routes.
Using the package
Implement the package
To make a model routable implement the interface and use the trait in your model.
Configure the routes
Configuration of the routes is done by adding a method to your model. In this method you call to create a route. The first property is the name of the route, the second is the used route generator. By default the name is and the used route generator is
This example will create two routes (get and post on the same path) for every model that will be created.
Retrieve the routes
The default route is accessible with route accessor.
Handle the routes
The package will automatically register/capture the created routes and will call the configured action. The model will be passed to the action as .
Localization
The package supports two types of localized routes.
- Multiple localized routes for translated modes using
- Single route prepended with locale for models with a property
All localized routed will have the locale prepended to the route path.
Enabling localization for the package
Localized routes are enabled by configuring the and in the dotenv file.
Implementing localization for a translatable model
For this feature first of all make sure that you have implemented for yur model. Then add to the route configuration.
Using localized translatable routes
Now for this model multiple routes will be created.
Handling localized routes
The package will automatically set the locale for the request when a localized route was accessed.
Implementing localization for not translated model
A model can be sat as being localized by calling on the route generator. The route generator now expects a property on the model to contain the locale. The property name can be overwritten by adding parameters to the call.
Usage:
Also in this case when accessing one of these routes the app()->locale() will be automatically be set to the proper locale.
Disabling routes
Route generation for a specific model can be disable by overwriting the method to return .
The route generator
Routes can be manipulated with the options shown in the example below. You can also replace the default route generator with your own for more advanced implementations. Make sure your custom route generator class implements the
Testing
Run the tests with: