Skip to content

Commit f464f15

Browse files
committed
feat: initial commit
0 parents  commit f464f15

File tree

12 files changed

+310
-0
lines changed

12 files changed

+310
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
---
2+
name: Bug report
3+
about: Create a report to help us improve
4+
title: ''
5+
labels: ''
6+
assignees: ''
7+
8+
---
9+
10+
**Describe the bug**
11+
A clear and concise description of what the bug is.
12+
13+
**To Reproduce**
14+
Steps to reproduce the behavior:
15+
1. Go to '...'
16+
2. Click on '....'
17+
3. Scroll down to '....'
18+
4. See error
19+
20+
**Expected behavior**
21+
A clear and concise description of what you expected to happen.
22+
23+
**Screenshots**
24+
If applicable, add screenshots to help explain your problem.
25+
26+
**Desktop (please complete the following information):**
27+
- OS: [e.g. iOS]
28+
- Browser [e.g. chrome, safari]
29+
- Version [e.g. 22]
30+
31+
**Smartphone (please complete the following information):**
32+
- Device: [e.g. iPhone6]
33+
- OS: [e.g. iOS8.1]
34+
- Browser [e.g. stock browser, safari]
35+
- Version [e.g. 22]
36+
37+
**Additional context**
38+
Add any other context about the problem here.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
name: Feature request
3+
about: Suggest an idea for this project
4+
title: ''
5+
labels: ''
6+
assignees: ''
7+
8+
---
9+
10+
**Is your feature request related to a problem? Please describe.**
11+
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
12+
13+
**Describe the solution you'd like**
14+
A clear and concise description of what you want to happen.

.github/pull_request_template.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
### Description
2+
3+
Explain what are you submitting.
4+
5+
### Fixes
6+
7+
Enter the issue you are fixing (e.g. This fixes #1.).
8+
9+
10+
11+

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/.idea

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2020 CodeTech
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Laravel Sluggable
2+
3+
Laravel package for managing models' slugs.
4+
5+
[![Latest version](https://img.shields.io/github/release/CodeTechPt/laravel-sluggable?style=flat-square)](https://github.com/CodeTechPt/laravel-sluggable/releases)
6+
[![GitHub license](https://img.shields.io/github/license/CodeTechPt/laravel-sluggable?style=flat-square)](https://github.com/CodeTechPt/laravel-sluggable/blob/master/LICENSE)
7+
8+
9+
## Installation
10+
11+
Add the package to your Laravel app using composer
12+
13+
```
14+
composer require codetech/laravel-sluggable
15+
```
16+
17+
18+
### Service Provider
19+
20+
Register the package's service provider in config/app.php. In Laravel versions 5.5 and beyond, this step can be skipped if package auto-discovery is enabled.
21+
22+
```
23+
'providers' => [
24+
25+
...
26+
Codetech\Sluggable\Providers\SluggableServiceProvider::class,
27+
...
28+
29+
];
30+
```
31+
32+
33+
## Usage
34+
35+
Use the trait in your models.
36+
37+
```
38+
39+
use CodeTech\Sluggable\Traits\HasSlug;
40+
41+
class Theme extends Model
42+
{
43+
use HasSlug;
44+
45+
...
46+
```
47+
48+
49+
---
50+
51+
52+
## License
53+
54+
**codetech/laravel-sluggable** is open-sourced software licensed under the [MIT license](https://github.com/CodeTechPt/laravel-sluggable/blob/master/LICENSE).
55+
56+
57+
## About CodeTech
58+
59+
[CodeTech](https://www.codetech.pt) is a web development agency based on Matosinhos, Portugal. Oh, and we LOVE Laravel!

composer.json

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"name": "codetech/laravel-sluggable",
3+
"description": "A Laravel package that allows you to assign settings to your models.",
4+
"keywords": [
5+
"codetech",
6+
"laravel-sluggable",
7+
"slug",
8+
"laravel",
9+
"laravel-package",
10+
"eloquent-models"
11+
],
12+
"authors": [
13+
{
14+
"name": "José Osório",
15+
"email": "jfrosorio@gmail.com",
16+
"role": "Developer"
17+
}
18+
],
19+
"license": "MIT",
20+
"require": {
21+
"php": "^7.2",
22+
"laravel/framework": "^6.0|^7.0"
23+
},
24+
"autoload": {
25+
"psr-4": {
26+
"CodeTech\\Sluggable\\": "src/"
27+
}
28+
},
29+
"minimum-stability": "dev",
30+
"prefer-stable": true,
31+
"extra": {
32+
"laravel": {
33+
"providers": [
34+
"CodeTech\\Sluggable\\Providers\\SluggableServiceProvider"
35+
]
36+
}
37+
}
38+
}

src/HasSlug.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace CodeTech\Sluggable;
4+
5+
use CodeTech\Sluggable\Observers\HasSlugObserver;
6+
7+
trait HasSlug
8+
{
9+
/**
10+
* Initializes the trait.
11+
*/
12+
public function initializeHasSlug()
13+
{
14+
$this->fillable[] = 'slug';
15+
}
16+
17+
/**
18+
* Boots the trait
19+
*
20+
* @return void
21+
*/
22+
public static function bootHasSlug()
23+
{
24+
static::observe(HasSlugObserver::class);
25+
}
26+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace CodeTech\Sluggable\Http\Controllers;
4+
5+
use App\Http\Controllers\Controller;
6+
use Illuminate\Http\JsonResponse;
7+
use Illuminate\Http\Request;
8+
use Illuminate\Support\Str;
9+
10+
class HasSlugController extends Controller
11+
{
12+
13+
/**
14+
* Slugifies a text string.
15+
*
16+
* @param Request $request
17+
* @return JsonResponse
18+
*/
19+
public function slugify(Request $request): JsonResponse
20+
{
21+
$text = $request->get('input') ?? null;
22+
23+
return response()->json([
24+
'text' => $text,
25+
'slug' => Str::slug($text)
26+
]);
27+
}
28+
}

src/Observers/HasSlugObserver.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace CodeTech\Sluggable\Observers;
4+
5+
use Illuminate\Support\Str;
6+
7+
class HasSlugObserver
8+
{
9+
/**
10+
* Listen to the saved event.
11+
*
12+
* @param $model
13+
*/
14+
public function saving($model)
15+
{
16+
$modelSlug = empty($model->slug) ? '' : Str::slug($model->slug);
17+
18+
$index = 1;
19+
$suffix = '';
20+
21+
do {
22+
$slug = $modelSlug . $suffix;
23+
24+
$suffix = '-' . $index;
25+
$index++;
26+
27+
$matches = $model::where('slug', $slug);
28+
29+
if ($model->id !== null) {
30+
$matches->where('id', '!=', $model->id);
31+
}
32+
} while ($matches->count());
33+
34+
$model->slug = $slug;
35+
}
36+
}

0 commit comments

Comments
 (0)