Skip to content

Commit

Permalink
Cache off by default, some minor changes and added CHANGELOG and CONT…
Browse files Browse the repository at this point in the history
…RIBUTING
  • Loading branch information
ruthgeridema committed Mar 9, 2019
1 parent bb4b084 commit 97f6eb4
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 66 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
@@ -0,0 +1,3 @@
# Changelog

All notable changes to `laravel-url-rewrites` will be documented in this file.
55 changes: 55 additions & 0 deletions CONTRIBUTING.md
@@ -0,0 +1,55 @@
# Contributing

Contributions are **welcome** and will be fully **credited**.

Please read and understand the contribution guide before creating an issue or pull request.

## Etiquette

This project is open source, and as such, the maintainers give their free time to build and maintain the source code
held within. They make the code freely available in the hope that it will be of use to other developers. It would be
extremely unfair for them to suffer abuse or anger for their hard work.

Please be considerate towards maintainers when raising issues or presenting pull requests. Let's show the
world that developers are civilized and selfless people.

It's the duty of the maintainer to ensure that all submissions to the project are of sufficient
quality to benefit the project. Many developers have different skillsets, strengths, and weaknesses. Respect the maintainer's decision, and do not be upset or abusive if your submission is not used.

## Viability

When requesting or submitting new features, first consider whether it might be useful to others. Open
source projects are used by many developers, who may have entirely different needs to your own. Think about
whether or not your feature is likely to be used by other users of the project.

## Procedure

Before filing an issue:

- Attempt to replicate the problem, to ensure that it wasn't a coincidental incident.
- Check to make sure your feature suggestion isn't already present within the project.
- Check the pull requests tab to ensure that the bug doesn't have a fix in progress.
- Check the pull requests tab to ensure that the feature isn't already in progress.

Before submitting a pull request:

- Check the codebase to ensure that your feature doesn't already exist.
- Check the pull requests to ensure that another person hasn't already submitted the feature or fix.

## Requirements

If the project maintainer has any additional requirements, you will find them listed here.

- **[PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)** - The easiest way to apply the conventions is to install [PHP Code Sniffer](http://pear.php.net/package/PHP_CodeSniffer).

- **Add tests!** - Your patch won't be accepted if it doesn't have tests.

- **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date.

- **Consider our release cycle** - We try to follow [SemVer v2.0.0](http://semver.org/). Randomly breaking public APIs is not an option.

- **One pull request per feature** - If you want to do more than one thing, send multiple pull requests.

- **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash them](http://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting.

**Happy coding**!
File renamed without changes.
137 changes: 77 additions & 60 deletions README.md
@@ -1,61 +1,15 @@
# Easily add URL rewrites to a Laravel app
Very easy to use URL rewrite package. Follow the instructions and you're good to go!

I've used the [Spatie guidelines](https://guidelines.spatie.be/code-style/laravel-php) for better code.


## 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:
```php
UrlRewrite::create('apple-airpods', 'product/5')
```

### Use named routes
You must specify the types in the config.
```php
UrlRewrite::create('apple-airpods', null, 'product', ["id" => 5])
```

To regenerate the target path you can use
```php
UrlRewrite::regenerateRoute($urlRewrite)
UrlRewrite::regenerateAll()
UrlRewrite:;regenerateRoutesFromType($type)
```

### Redirect

301 redirect
```php
UrlRewrite::create('apple-airpods', 'product/5', null, null, 1)
```
302 redirect
```php
UrlRewrite::create('apple-airpods', 'product/5', null, null, 2)
```

### Other functions
```php
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])
```


You can find an example project on my Github: [view example project](https://github.com/ruthgeridema/laravel-url-rewrites-example)
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 5.8 or higher and PHP 7.2 or higher.

This package requires Laravel 5.8 or higher, PHP 7.2 or higher and a database that supports json fields and functions such as MySQL 5.7 or higher.

## Installation

Expand Down Expand Up @@ -95,25 +49,85 @@ php artisan vendor:publish --provider="RuthgerIdema\UrlRewrite\ServiceProvider"

This is the contents of the published config file:

```
```php
<?php

return [
'table-name' => 'url_rewrites',
'repository' => \RuthgerIdema\UrlRewrite\Repositories\UrlRewriteRepository::class,
'model' => \RuthgerIdema\UrlRewrite\Entities\UrlRewrite::class,
'cache' => false,
'cache' => true,
'cache-decorator' => \RuthgerIdema\UrlRewrite\Repositories\Decorators\CachingUrlRewriteRepository::class,
'types' => [
'product' => [
'route' => 'product'
]/*,
'route' => 'product',
'attributes' => ['id'],
],
'category' => [
'route' => 'category'
]*/
]
'route' => 'category',
'attributes' => ['id'],
]
],
];
```

## 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:
```php
UrlRewrite::create('apple-airpods', 'product/5')
```

### Use named routes
You must specify the types in the config.
```php
UrlRewrite::create('apple-airpods', null, 'product', ["id" => 5])
```

To regenerate the target path you can use
```php
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.

```php
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
```php
UrlRewrite::create('apple-airpods', 'product/5', null, null, 1)
```
302 redirect
```php
UrlRewrite::create('apple-airpods', 'product/5', null, null, 2)
```

### Other functions
```php
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

Expand All @@ -138,6 +152,9 @@ If you discover any security related issues, please email ruthger.idema@gmail.co
- [Ruthger Idema](https://github.com/ruthgeridema)
- [All Contributors](../../contributors)

Special thanks for Spatie for their guidelines and their packages as an inspiration
- [Spatie](https://spatie.be)

## License

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
Expand Down
3 changes: 2 additions & 1 deletion config/url-rewrite.php
Expand Up @@ -4,7 +4,8 @@
'table-name' => 'url_rewrites',
'repository' => \RuthgerIdema\UrlRewrite\Repositories\UrlRewriteRepository::class,
'model' => \RuthgerIdema\UrlRewrite\Entities\UrlRewrite::class,
'cache' => true,
'cache' => false,
'cache-tag' => 'url_rewrites',
'cache-decorator' => \RuthgerIdema\UrlRewrite\Repositories\Decorators\CachingUrlRewriteRepository::class,
'types' => [
'product' => [
Expand Down
Expand Up @@ -52,7 +52,7 @@ function () use ($method, $arguments) {
protected function addTagIfPossible()
{
if ($this->cache->getStore() instanceof TaggableStore) {
$this->cache = $this->cache->tags('url_rewrites');
$this->cache = $this->cache->tags(config("url-rewrite.cache-key"));
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/ServiceProvider.php
Expand Up @@ -39,7 +39,7 @@ protected function registerRouteMacro()
{
$router = $this->app['router'];
$router->macro('rewrites', function () use ($router) {
$router->get('/{url}', '\\'.UrlRewriteController::class)->name('url.rewrite');
$router->get('{url}', '\\'.UrlRewriteController::class)->where('url', '.*')->name('url.rewrite');
});
}

Expand Down
10 changes: 7 additions & 3 deletions src/Traits/HasUrlRewrite.php
Expand Up @@ -8,9 +8,7 @@ trait HasUrlRewrite {

public function getUrlAttribute(): string
{
if (! $urlRewrite = UrlRewrite::getByTypeAndAttributes(
config("url-rewrite.types.$this->urlRewriteType.route"), $this->getAttributesArray())
) {
if (! $urlRewrite = $this->getUrlRewrite()) {
return '';
}

Expand All @@ -28,4 +26,10 @@ public function getUrlRewriteAttributesArray(): array
return $mapped;
}

public function getUrlRewrite()
{
return UrlRewrite::getByTypeAndAttributes(
config("url-rewrite.types.$this->urlRewriteType.route"), $this->getUrlRewriteAttributesArray());
}

}

0 comments on commit 97f6eb4

Please sign in to comment.