Looking at the manual, I want to define 2 pages
exchanging data with the current locale defined, like :
Route::get('/{locale?}', [HomeController::Class, 'index'])->name('home');
Route::get('/partnership-order/{locale?}', [PartnershipOrderController::class, 'index'])->name('partnership_order.index');
and defined in component :
class PartnershipOrderDialog extends Component
{
use WireUiActions;
use AppCommonTrait;
#[Url]
public ?string $locale;
public function mount(): void
{
if(empty($this->locale)) { // if locale is not defined in url - get from the app
$this->locale = app()->getLocale();
}
I got the error :
Typed property AppLivewirePartnershipOrderDialog::$locale must not be accessed before initialization
if to define :
#[Url]
public ?string $locale = '';
then $locale var is always empty and default value “en” is assigned for url like :
http://sitename/partnership-order/es
Which way is correct ?
ADDITIVE :
In the docs there is reference :
For example, if a user visits the URL
https://example.com/users?search=bob, Livewire will set the initial
value of $search to “bob”.
If I try to run in the browser :
http://local-quizzes11.com/partnership-order/locale=es
Anyway checking locale value of :
public ?string $locale = ''; // config('app.locale');
public function mount(): void
{
Log::info($this->locale);
I see empty value.
a) In which way value from Url is assigned?
b) In routes/web.php I have :
Route::get('/partnership-order/{locale?}', [PartnershipOrderController::class, 'index'])->name('partnership_order.index');
Does it matter in this case which format is it ?
ADDITIVE # 2:
I found that if manually to run in browser url :
http://local-quizzes11.com/services/partnership-order?locale=es
with rule in web.php :
Route::get('/services/partnership-order{locale?}', [PartnershipOrderController::class, 'index'])->name('services.partnership_order.index');
the page is opened ok with valid url passed, but I do not know how to redirect from
header component with menu items in blade:
<a wire:click="redirectTo('services.partnership_order.index')" wire:navigate
and in component :
public function redirectTo(string $routeName) {
return $this->redirect(route($routeName, ['locale' => $this->locale]), navigate: true);
}
where $this->locale can be changed without page reloading.
How to change ny component method(web.php url)
to redirect to page :
http://local-quizzes11.com/services/partnership-order?locale=es
?
7