Download the PHP package neurony/laravel-url without Composer
On this page you can find all versions of the php package neurony/laravel-url. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package laravel-url
Generate custom URLs for any Eloquent model
- Overview
- Installation
- Usage
- Customisations
- Extra
Overview
This package allows any Eloquent model record to have a custom URL associated to it through a polymorphic one-to-one relationship.
The URL will be automatically saved inside the urls
table, along with the model using the created
and updated
Eloquent events.
When the model is force deleted, the corresponding URL will also be deleted by leveraging the deleted
Eloquent event.
Installation
Install the package via Composer:
Publish the migration file with:
After the migration has been published you can create the urls
table by running:
Usage
Step 1
Insert the following as your last line in your routes/web.php
file:
This route will catch any URL and will compare it against the urls
table.
If the URL matches a record in that table, it will dispatch the request to the designated controller and action specified in step 2.
Step 2
Your Eloquent models should use the Neurony\Url\Traits\HasUrl
trait and the Neurony\Url\Options\UrlOptions
class.
The trait contains an abstract method getUrlOptions()
that you must implement yourself.
Here's an example of how to implement the trait:
The getUrlOptions()
should always return a Neurony\Url\Options\UrlOptions
instance.
You can view all options and their methods of implementation inside the Neurony\Url\Options\UrlOptions
class.
Step 3
Create the controller and action specified in the getUrlOptions()
method.
Once created, you can fetch your Eloquent model instance corresponding to the URL visited by using the getUrlable()
or getUrlableOrFail()
methods, present in the Neurony\Url\Models\Url
class.
The difference between
getUrlable()
andgetUrlableOrFail()
is that when no record is found, the first returnsnull
while the second throws aModelNotFoundException
.
Customisations
Set a prefix
You can define a prefix for your URLs using the prefixUrlWith()
method in your definition of the getUrlOptions()
method.
The example above illustrates how to prefix an URL using a callable.
Please note that you can also pass a string or an array to the prefixUrlWith()
method:
Set a suffix
You can define a suffix for your URLs using the suffixUrlWith()
method in your definition of the getUrlOptions()
method.
The example above illustrates how to suffix an URL using a callable.
Please note that you can also pass a string or an array to the prefixUrlWith()
method:
Set a separator
You can specify with what string to separate the different segments of an URL by using the glueUrlWith()
method in your definition of the getUrlOptions()
method.
The default glue value is /
.
Disable cascade update
Let's say that in your urls
table you have the following 2 URLs: posts/my-posts
and posts/my-posts/post-one
.
By default, when updating an URL, all its "children" URLs will be updated also.
So if you update the posts/my-posts
URL to become posts/my-latest-posts
, the trait will automatically update all the underlying URLs, meaning that the URL posts/my-posts/post-one
will become posts/my-latest-posts/post-one
.
To disable this automation, you can use the doNotUpdateCascading()
method in your definition of the getUrlOptions()
method.
Disable creating/updating an URL
If you want to save an Eloquent model that uses the HasUrl
trait, but you don't want to create or update its corresponding URL, you can do so by calling the doNotGenerateUrl()
method.
Extra
You can get the absolute URL of your Eloquent model record by using:
You can get the relative URL of your Eloquent model record by using:
You can access the Neurony\Url\Models\Url
instance from your Eloquent models using the url
relationp:
You can access your Eloquent model instance from the Neurony\Url\Models\Url
model using the urlable
relation:
You can find a specific URL by using the following query scope:
Credits
- Andrei Badea
- All Contributors
Security
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
License
The MIT License (MIT). Please see LICENSE for more information.
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
All versions of laravel-url with dependencies
illuminate/contracts Version ^6.0
illuminate/support Version ^6.0
illuminate/database Version ^6.0