sparkouttech / laravel-url-rewrites
Easy URL rewrites in your Laravel application
Installs: 2 187
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 3
Forks: 0
Open Issues: 0
Requires
- php: ^8.1
- laravel/framework: *
Requires (Dev)
- orchestra/testbench: ~3.8.0
- phpunit/phpunit: ^8.0
This package is auto-updated.
Last update: 2024-11-19 13:40:05 UTC
README
Very easy to use URL rewrite package. Follow the instructions and you're good to go!
You can find an example project on my Github: view example project This example project features the following:
- Eloquent observers to add URL rewrites automatically
- Usage of the trait
- Some use cases
Requirements
This package requires Laravel 8 or higher, PHP 8.1 or higher and a database that supports json fields and functions such as MySQL 5.7 or higher.
Installation
You can install the package via composer:
composer require sparkouttech/laravel-url-rewrites
The package will automatically register itself.
Register the routes the feeds will be displayed on using the rewrites
-macro.
You need to place it at the bottom of your routes file.
// In routes/web.php Route::rewrites();
You can publish the migration with:
php artisan vendor:publish --provider="RuthgerIdema\UrlRewrite\ServiceProvider" --tag="migrations"
After the migration has been published you can create the url_rewrites
table by running the migration:
php artisan migrate
You can optionally publish the config file with:
php artisan vendor:publish --provider="RuthgerIdema\UrlRewrite\ServiceProvider" --tag="config"
This is the contents of the published config file:
<?php return [ 'table-name' => 'url_rewrites', 'repository' => \RuthgerIdema\UrlRewrite\Repositories\UrlRewriteRepository::class, 'model' => \RuthgerIdema\UrlRewrite\Entities\UrlRewrite::class, 'cache' => true, 'cache-decorator' => \RuthgerIdema\UrlRewrite\Repositories\Decorators\CachingUrlRewriteRepository::class, 'types' => [ 'product' => [ 'route' => 'product', 'attributes' => ['id'], ], 'category' => [ 'route' => 'category', 'attributes' => ['id'], ] ], ];
Laravel Nova
Using Laravel Nova? You can publish the Nova class to App/Nova with the following command
php artisan vendor:publish --provider="RuthgerIdema\UrlRewrite\ServiceProvider" --tag="nova"
In the near future I will publish a Laravel Nova package with features like reindexing the URL rewrites.
Usage
Forward request
Let's say you've got a controller route 'product/{id}' and you have a product 'Apple Airpods' with id=5. When you visit 'apple-airpods' this package will forward the request to the controller but keeps the clean url.
The following code adds this to the database:
UrlRewrite::create('apple-airpods', 'product/5')
Use named routes
You must specify the types in the config.
UrlRewrite::create('apple-airpods', null, 'product', ["id" => 5])
To regenerate the target path you can use
UrlRewrite::regenerateRoute($urlRewrite) UrlRewrite::regenerateAll() UrlRewrite::regenerateRoutesFromType($type)
To automatically add the URL attribute to an Eloquent model, you have to add the HasUrlRewrite trait to an Eloquent model. You also need to add the urlRewriteType and optionally add 'url' to the appends array.
use HasUrlRewrite; public $urlRewriteType = 'category'; protected $appends = ['url'];
Once this is done you can simply call Model::find(1)->url
to get the url of the model.
Redirect
301 redirect
UrlRewrite::create('apple-airpods', 'product/5', null, null, 1)
302 redirect
UrlRewrite::create('apple-airpods', 'product/5', null, null, 2)
Other functions
UrlRewrite::all() UrlRewrite::find($id) UrlRewrite::delete($id) UrlRewrite::update($data, $id) UrlRewrite::getByRequestPath('apple-airpods') UrlRewrite::getByTargetPath('product/5') UrlRewrite::getByTypeAndAttributes('product', ["id" => 5])
Testing
- Copy
.env.example
to.env
and fill in your database credentials. - Run
composer test
.
Changelog
Please see CHANGELOG for more information what has changed recently.
Contributing
Please see CONTRIBUTING for details.
License
The MIT License (MIT). Please see License File for more information.