Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mstdmstd's avatar

How in livewire to pass locale parameter with #[Url] defined?

Looking manual at https://livewire.laravel.com/docs/url#initializing-properties-from-the-url I want to define 2 pages exchanging data with 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 error :

Typed property App\Livewire\PartnershipOrderDialog::$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 fore url like :

http://sitename/partnership-order/es

Which way is correct ?

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 ?

0 likes
0 replies

Please or to participate in this conversation.