elkex
05/12/2023, 12:48 PMpboivin
05/12/2023, 1:31 PM<a>
tag? The value of the href
is not translated correctly?elkex
05/12/2023, 1:34 PMelkex
05/12/2023, 1:35 PMelkex
05/12/2023, 1:35 PMelkex
05/12/2023, 1:36 PMpboivin
05/12/2023, 1:41 PMelkex
05/12/2023, 1:42 PMelkex
05/12/2023, 1:42 PMelkex
05/12/2023, 1:43 PMelkex
05/12/2023, 1:43 PMelkex
05/12/2023, 1:47 PMpboivin
05/12/2023, 1:57 PMelkex
05/12/2023, 1:58 PMRoute::group([
'prefix' => LaravelLocalization::setLocale(),
'middleware' => ['localize', 'localeSessionRedirect', 'localizationRedirect', 'localeViewPath'],
], function () {
Route::name('welcome')->get('/', [PageController::class, 'welcomeView']);
Route::name('page')->get('{page}', [PageController::class, 'index'])->where('page', '.*');
});
pboivin
05/12/2023, 1:59 PM<a>
tag?elkex
05/12/2023, 2:00 PM@foreach (LaravelLocalization::getSupportedLocales() as $localeCode => $properties)
<li>
<a rel="alternate" hreflang="{{ $localeCode }}"
href="{{ LaravelLocalization::getLocalizedURL($localeCode, null, [], true) }}"
class="ml-2 text-sm {{ app()->getLocale() === $localeCode ? 'font-bold' : '' }}">
{{ strtoupper($localeCode) }}
</a>
</li>
@endforeach
elkex
05/12/2023, 2:02 PM@foreach ($navigation as $item)
<x-nav-link :href="route('page', ['page' => $item->slug])" :active="in_array($item->nestedslug, request::segments())">
{{ $item->title }}
</x-nav-link>
@endforeach
elkex
05/12/2023, 2:03 PMelkex
05/12/2023, 2:05 PMpboivin
05/12/2023, 2:06 PM$item->slug
never returns the correct translated value?elkex
05/12/2023, 2:06 PMelkex
05/12/2023, 2:06 PMelkex
05/12/2023, 2:06 PMpboivin
05/12/2023, 2:07 PMelkex
05/12/2023, 2:07 PMelkex
05/12/2023, 2:07 PMelkex
05/12/2023, 2:07 PMelkex
05/12/2023, 2:07 PMelkex
05/12/2023, 2:08 PMelkex
05/12/2023, 2:08 PMelkex
05/12/2023, 2:08 PMhttps://cdn.discordapp.com/attachments/1106563618176245782/1106583749421969560/Screenshot_2023-05-12_at_16.08.37.png▾
elkex
05/12/2023, 2:08 PMpboivin
05/12/2023, 2:11 PMpboivin
05/12/2023, 2:11 PMresolveRouteBinding()
and getLocalizedRouteKey()
?elkex
05/12/2023, 2:15 PMpublic function resolveRouteBinding($slug, $field = null)
{
$page = app(PageRepository::class)->forSlug($slug);
abort_if(! $page, 404);
return $page;
}
// #region routekey
public function getLocalizedRouteKey($locale)
{
return $this->getSlug($locale);
}
pboivin
05/12/2023, 2:21 PMimplements LocalizedUrlRoutable
?elkex
05/12/2023, 2:26 PMclass Page extends Model implements Sortable, LocalizedUrlRoutable
{
use HasBlocks, HasTranslation, HasSlug, HasMedias, HasFiles, HasPosition, HasNesting, HasMetadata;
pboivin
05/12/2023, 2:32 PMlang/fr/routes.php
and lang/en/routes.php
?pboivin
05/12/2023, 2:33 PMelkex
05/12/2023, 2:33 PM<?php
return [
'page' => '{page}',
];
pboivin
05/12/2023, 2:34 PMelkex
05/12/2023, 2:35 PMelkex
05/12/2023, 2:35 PMelkex
05/12/2023, 2:35 PMelkex
05/12/2023, 2:36 PMelkex
05/12/2023, 2:36 PMelkex
05/12/2023, 2:36 PMpublic function index($slug){
$page = $this->repository->forNestedSlug($slug);
abort_unless($page, 404, 'page');
$this->setMetadata($page);
return view('pages.index', compact('page'));
}
elkex
05/12/2023, 2:37 PMpublic function index(Page $page){
$this->setMetadata($page);
return view('pages.index', compact('page'));
}
elkex
05/12/2023, 2:37 PMpboivin
05/12/2023, 2:41 PMindex(Page $page)
works for top-level pages (like /nl/registratie) ?elkex
05/12/2023, 2:43 PMpboivin
05/12/2023, 2:43 PMelkex
05/12/2023, 2:43 PMelkex
05/12/2023, 2:43 PMelkex
05/12/2023, 2:43 PMpboivin
05/12/2023, 2:44 PMelkex
05/12/2023, 2:44 PMpboivin
05/12/2023, 2:45 PMelkex
05/15/2023, 8:20 AMelkex
05/15/2023, 8:24 AMRoute::name('page')->get(LaravelLocalization::transRoute('routes.page'), [PageController::class, 'index'])->where('page', '.*');
elkex
05/15/2023, 9:09 AMelkex
05/15/2023, 9:14 AMelkex
05/15/2023, 9:22 AMifox
05/15/2023, 10:29 AMelkex
05/15/2023, 10:54 AMpublic function index($slug)
{
$page = $this->repository->forNestedSlug($slug);
$this->setMetadata($page);
return view('pages.index', compact('page'));
}
elkex
05/15/2023, 10:55 AMpublic function index(Page $page)
{
$this->setMetadata($page);
return view('pages.index', compact('page'));
}
elkex
05/15/2023, 10:55 AMpboivin
05/17/2023, 1:17 PMresolveRouteBinding
? Something like this :
php
public function resolveRouteBinding($slug, $field = null)
{
if ($page = app(PageRepository::class)->forNestedSlug($slug)) {
return $page;
}
if ($page = app(PageRepository::class)->forSlug($slug)) {
return $page;
}
abort(404);
}
(not tested...)pboivin
05/17/2023, 1:21 PMif
might not even be necessary.elkex
05/22/2023, 7:14 AMelkex
05/22/2023, 7:14 AMpublic function index(Page $page)
{
$this->setMetadata($page);
return view('pages.index', compact('page'));
}
elkex
05/22/2023, 7:15 AMpublic function index($slug)
{
$page = $this->repository->forNestedSlug($slug);
$this->setMetadata($page);
return view('pages.index', compact('page'));
}
pboivin
05/22/2023, 11:37 AM3.x
routes/web.php
php
Route::get(LaravelLocalization::transRoute('routes.page'), function (SitePage $page) {
return view('site.pages.show', ['page' => $page]);
})
->where(['page' => '.*'])
->name('page');
(This is basically equivalent to your first controller example above.)
Model
php
public function resolveRouteBinding($slug, $field = null)
{
$page = app(SitePageRepository::class)->forNestedSlug($slug);
abort_if(! $page, 404);
return $page;
}
public function getLocalizedRouteKey($locale)
{
return $this->getSlug($locale);
}
public function getAncestorsSlug($locale = null)
{
return collect($this->ancestors->sortByDesc('position') ?? [])
->map(function ($i) use ($locale) { return $i->getSlug($locale); })
->implode('/');
}
I had to override getAncestorsSlug()
in the model to a previous version of the method. I'm not exactly sure why the new one doesn't work in my case.elkex
05/22/2023, 2:10 PMelkex
05/22/2023, 2:10 PMifox
05/22/2023, 2:10 PMifox
05/22/2023, 2:11 PMelkex
05/22/2023, 2:13 PMifox
05/22/2023, 2:14 PMelkex
05/22/2023, 2:18 PMphp
@foreach (LaravelLocalization::getSupportedLocales() as $localeCode => $properties)
<li>
<a rel="alternate" hreflang="{{ $localeCode }}"
href="{{ LaravelLocalization::getLocalizedURL($localeCode, null, [], true) }}"
class="ml-2 text-sm {{ app()->getLocale() === $localeCode ? 'font-bold' : '' }}">
{{ strtoupper($localeCode) }}
</a>
</li>
@endforeach
ifox
05/22/2023, 2:18 PMelkex
05/22/2023, 2:19 PMelkex
05/22/2023, 2:37 PMphp
public function index($slug)
{
$page = $this->repository->forNestedSlug($slug);
$this->setMetadata($page);
return view('pages.index', compact('page'));
}
And not with the model as param.. It returns 404
php
public function index(Page $page)
{
$this->setMetadata($page);
return view('pages.index', compact('page'));
}
elkex
05/22/2023, 2:41 PMelkex
05/22/2023, 2:42 PMelkex
05/22/2023, 2:46 PMhttps://cdn.discordapp.com/attachments/1106563618176245782/1110217189472096326/Screenshot_2023-05-22_at_16.46.31.png▾
elkex
05/23/2023, 7:54 AMpboivin
05/23/2023, 11:58 AMelkex
05/24/2023, 8:43 AMelkex
05/24/2023, 8:48 AM