Skip to content

Commit

Permalink
Merge pull request #12 from FriendsOfFlarum/cw/use-named-routes
Browse files Browse the repository at this point in the history
Use named routes instead of hard-coded URLs
  • Loading branch information
clarkwinkelmann authored Jun 1, 2020
2 parents 3b2fc61 + dc64a6c commit d7554a2
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 13 deletions.
4 changes: 3 additions & 1 deletion src/Resources/Discussion.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ public function query(): Builder

public function url($model): string
{
return $this->generateUrl('d/' . $model->id . (trim($model->slug) ? '-' . $model->slug : ''));
return $this->generateRouteUrl('discussion', [
'id' => $model->id . (trim($model->slug) ? '-' . $model->slug : ''),
]);
}

public function priority(): float
Expand Down
4 changes: 3 additions & 1 deletion src/Resources/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ public function query(): Builder

public function url($model): string
{
return $this->generateUrl('p/' . $model->id . (trim($model->slug) ? '-' . $model->slug : ''));
return $this->generateRouteUrl('pages.page', [
'id' => $model->id . (trim($model->slug) ? '-' . $model->slug : ''),
]);
}

public function priority(): float
Expand Down
23 changes: 23 additions & 0 deletions src/Resources/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace FoF\Sitemap\Resources;

use Carbon\Carbon;
use Flarum\Http\UrlGenerator;
use Illuminate\Database\Eloquent\Builder;

abstract class Resource
Expand All @@ -20,10 +21,32 @@ public function lastModifiedAt($model): Carbon
return Carbon::now();
}

/**
* Generates an absolute URL to an arbitrary path
* Not actually used by the extension anymore but kept for compatibility with third-party code extending this class
* @param $path
* @return string
*/
protected function generateUrl($path): string
{
$url = app()->url();

return "$url/$path";
}

/**
* Generates an absolute URL to a named route
* @param $name
* @param array $parameters
* @return string
*/
protected function generateRouteUrl($name, $parameters = []): string
{
/**
* @var $generator UrlGenerator
*/
$generator = app(UrlGenerator::class);

return $generator->to('forum')->route($name, $parameters);
}
}
4 changes: 3 additions & 1 deletion src/Resources/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ public function query(): Builder

public function url($model): string
{
return $this->generateUrl("t/{$model->slug}");
return $this->generateRouteUrl('tag', [
'slug' => $model->slug,
]);
}

public function priority(): float
Expand Down
4 changes: 3 additions & 1 deletion src/Resources/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ public function query(): Builder

public function url($model): string
{
return $this->generateUrl("u/{$model->username}");
return $this->generateRouteUrl('user', [
'username' => $model->username,
]);
}

public function priority(): float
Expand Down
18 changes: 9 additions & 9 deletions src/SitemapGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Carbon\Carbon;
use Flarum\Extension\ExtensionManager;
use Flarum\Foundation\Application;
use Flarum\Http\UrlGenerator;
use Flarum\Settings\SettingsRepositoryInterface;
use FoF\Sitemap\Resources\Resource;
use FoF\Sitemap\Sitemap\Frequency;
Expand All @@ -14,28 +15,27 @@ class SitemapGenerator
{
protected $app;
protected $extensions;
protected $settings;
protected $url;

public function __construct(Application $app, ExtensionManager $extensions)
public function __construct(Application $app, ExtensionManager $extensions, SettingsRepositoryInterface $settings, UrlGenerator $url)
{
$this->app = $app;
$this->extensions = $extensions;
$this->settings = $settings;
$this->url = $url;
}

public function getUrlSet()
{
$urlSet = new UrlSet();

$url = $this->app->url();

// Always add the homepage, whichever it is
$urlSet->addUrl($url . '/', Carbon::now(), Frequency::DAILY, 0.9);

/** @var SettingsRepositoryInterface $settings */
$settings = $this->app->make(SettingsRepositoryInterface::class);
$urlSet->addUrl($this->url->to('forum')->base() . '/', Carbon::now(), Frequency::DAILY, 0.9);

// If the homepage is different from /all, also add /all
if ($settings->get('default_route') !== '/all') {
$urlSet->addUrl($url . '/all', Carbon::now(), Frequency::DAILY, 0.9);
if ($this->settings->get('default_route') !== '/all') {
$urlSet->addUrl($this->url->to('forum')->route('index'), Carbon::now(), Frequency::DAILY, 0.9);
}

$resources = $this->app->make('fof.sitemap.resources') ?? [];
Expand Down

0 comments on commit d7554a2

Please sign in to comment.