Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • balping/laravel-hashslug
  • aamirchaudhary/laravel-hashslug
  • jijoel/laravel-hashslug
  • rognales/laravel-hashslug
  • sdkakcy/laravel-hashslug
5 results
Show changes
Commits on Source (1)
......@@ -191,6 +191,20 @@ You can set the alphabet globally too, by adding the following line to `config/h
'alphabet' => 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
```
### Prefix
You can set a custom prefix to hashslug:
```php
class Post extends Model {
use HasHashSlug;
protected static $hashSlugPrefix = 'post-';
}
```
This would return slugs in the form of `post-K4Nkd`.
## Similar packages and how is this one different
#### [Laravel Hashids](https://github.com/vinkla/laravel-hashids)
......
This diff is collapsed.
......@@ -36,7 +36,7 @@ trait HasHashSlug {
private static $hashIds = null;
/**
* Returns a chached Hashids instanse
* Returns a chached Hashids instance
* or initialises it with salt
*
* @return \Hashids\Hashids
......@@ -86,7 +86,7 @@ trait HasHashSlug {
$this->hashslug = $hashids->encode($this->{$this->getKeyName()});
}
return $this->hashslug;
return self::getHashSlugPrefix() . $this->hashslug;
}
public function getRouteKeyName(){
......@@ -110,6 +110,19 @@ trait HasHashSlug {
return parent::where($this->getKeyName(), $id)->first();
}
/**
* Prefix to applied in front of hashslug
* @return string
*/
public static function getHashSlugPrefix(){
$prefix = '';
if(isset(self::$hashSlugPrefix)) {
$prefix = self::$hashSlugPrefix;
}
return $prefix;
}
/**
* Decodes slug to id
* @param string $slug
......@@ -118,6 +131,14 @@ trait HasHashSlug {
public static function decodeSlug($slug){
$hashids = self::getHashids();
// remove prefix
$prefix = self::getHashSlugPrefix();
if($prefix != '' && !\Str::startsWith($slug, $prefix)){
// slug is not correctly prefixed
return null;
}
$slug = substr($slug, strlen($prefix));
$decoded = $hashids->decode($slug);
if(! isset($decoded[0])){
......
......@@ -101,7 +101,7 @@ class HashSlugTest extends \Orchestra\Testbench\TestCase
}
/** @test */
public function find_byinvalid_slug_returns_null(){
public function find_by_invalid_slug_returns_null(){
$post = Post::forceCreate(["title" => "title1"]);
$slug = 'XX' . $post->slug();
......@@ -129,6 +129,35 @@ class HashSlugTest extends \Orchestra\Testbench\TestCase
$this->assertEquals(10, strlen($post->slug()));
}
/** @test */
public function prefix_can_be_customised(){
$post = PostCustomPrefix::forceCreate(["title" => "title1"]);
$this->assertStringStartsWith('post-', $post->slug());
}
/** @test */
public function model_can_be_found_by_prefixed_slug(){
$post = PostCustomPrefix::forceCreate(["title" => "title1"]);
$slug = $post->slug();
$foundPost = PostCustomPrefix::findBySlugOrFail($slug);
$this->assertEquals($post->id, $foundPost->id);
}
/** @test */
public function find_by_invalid__prefixed_slug_returns_null(){
$post = PostCustomPrefix::forceCreate(["title" => "title1"]);
$slug = 'XX' . $post->slug();
$foundPost = PostCustomPrefix::findBySlug($slug);
$this->assertNull($foundPost);
}
/** @test */
public function model_salt_can_be_customised(){
$post = PostCustomSalt::forceCreate(["title" => "title1"]);
......@@ -225,6 +254,14 @@ class PostCustomAlphabet extends Model {
protected $table = "posts";
}
class PostCustomPrefix extends Model {
use HasHashSlug;
protected static $hashSlugPrefix = 'post-';
protected $table = "posts";
}
class CommentCustomSalt extends Model {
use HasHashSlug;
......