Skip to content

Optional parameters not working #933

@KamikX

Description

@KamikX

Describe the bug
Optional parameters not working, issue is related to 681 and 373

To Reproduce
Steps to reproduce the behavior:

routes/web.php
Route::group([
    'prefix' => LaravelLocalization::setLocale(),
    'middleware' => ['localeSessionRedirect', 'localizationRedirect',]
], function () {
    Route::get(LaravelLocalization::transRoute('routes.service'), [ServiceController::class, 'detail'])->name('service.detail');
});
// lang/en/routes.php
"service" => "services/{type?}",
//"service" => "services/{type}", this works
// lang/sk/routes.php
"service" => "sluzby/{type?}",
//"service" => "sluzby/{type}", this works
// app/Http/Controllers/ServiceController.php

    public function detail(?string $type = null)
    {

        $currentLocale = LaravelLocalization::getCurrentLocale();
        $path = Route::current()->uri();
        // FIX for getRouteNameFromAPath (708)
        $path = parse_url($path)['path'];
        //$path = trim(str_replace('/'.$currentLocale.'/', '', $path), "/");  // => en/services/{type}
        $path = trim(str_replace($currentLocale.'/', '', $path), "/");  // => services/{type}
        $transKey = LaravelLocalization::getRouteNameFromAPath($path);
        dd($transKey);
    }

Go to
http://127.0.0.1:8082/en/service/cloud

Expected behavior
Translation key routes.service

More info:

  • Version of Laravel: 12.1.0
  • Version of the Laravel-localization package 2.3.0
  • Which middleware is used in Route::groups: localeSessionRedirect, localizationRedirect
  • Copy of the config file ( or at least setting of supportedLocales, useAcceptLanguageHeader and hideDefaultLocaleInURL).
 'useAcceptLanguageHeader' => true,
 'hideDefaultLocaleInURL' => true,
 'supportedLocales' => [
  'en'          => ['name' => 'English',                'script' => 'Latn', 'native' => 'English', 'regional' => 'en_GB'],
  'sk'          => ['name' => 'Slovak',                 'script' => 'Latn', 'native' => 'slovenčina', 'regional' => 'sk_SK'],
]
  • Minimal steps to reproduce on a clean Laravel installation.
    Same as in section To Reproduce

Additional context
Main problem is parse_url in

and probably in all places where this method is used, temporary it would be nice to clearly mention this problem in the common-issues section, to save others time.

$path = "services/{type?}"
$path = parse_url($path)['path'];  => return "services/{type" and not found match in lang/en/routes.php 

This maybe resolve problem

$path = "service/{type?}/{category?}?x=1#y
$path= preg_replace('/\?(?![^{]*})[^#]*|#.*$/', '', $path);  => return "services/{type?}/{category?}"

Side issue, input should be path not full url and str_replace('/'.$currentLocale.'/', '', $path) not works, only way to get route pattern with params is Route::current()->uri()

$path = trim(str_replace('/'.$this->currentLocale.'/', '', $path), "/");

$path = Route::current()->uri(); // => return only  "en/services/{type?}" without leading slash
$path = parse_url($path)['path'];

BTW: getter for the $translatedRoutes could be public.

Thank you for the great job 👍

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions