Skip to content

Commit

Permalink
Use slug driver system properly (#31)
Browse files Browse the repository at this point in the history
Closes #30
  • Loading branch information
askvortsov1 authored Sep 6, 2021
1 parent e25ec9f commit 20c5c35
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Resources/Discussion.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function query(): Builder
public function url($model): string
{
return $this->generateRouteUrl('discussion', [
'id' => $model->id.(trim($model->slug) ? '-'.$model->slug : ''),
'id' => $this->generateModelSlug(Model::class, $model),
]);
}

Expand Down
12 changes: 12 additions & 0 deletions src/Resources/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
namespace FoF\Sitemap\Resources;

use Carbon\Carbon;
use Flarum\Database\AbstractModel;
use Flarum\Foundation\Config;
use Flarum\Http\SlugManager;
use Flarum\Http\UrlGenerator;
use Illuminate\Database\Eloquent\Builder;

Expand Down Expand Up @@ -64,4 +66,14 @@ protected function generateRouteUrl($name, $parameters = []): string

return $generator->to('forum')->route($name, $parameters);
}

protected function generateModelSlug(string $modelClass, AbstractModel $model): string
{
/**
* @var SlugManager
*/
$slugManager = resolve(SlugManager::class);

return $slugManager->forResource($modelClass)->toSlug($model);
}
}
2 changes: 1 addition & 1 deletion src/Resources/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function query(): Builder
public function url($model): string
{
return $this->generateRouteUrl('user', [
'username' => $model->username,
'username' => $this->generateModelSlug(Model::class, $model),
]);
}

Expand Down

0 comments on commit 20c5c35

Please sign in to comment.