Search code examples
phproutescontrollerslaravel

Laravel Routing space in parameters returns 404


I have a action like the following in my controller (home)

public function action_test($keyword)
{
    echo $keyword;
}

it works fine when i pass parameter without space like the following

http://localhost/laravel/home/test/apple

but it gives 404 error when i pass argument with space like the following

http://localhost/laravel/home/test/green apple

it does not even works when the space is encoded

http://localhost/shop/public/home/test/green+apple

not even this

http://localhost/shop/public/home/test/green%20apple

can anybody please help me in this


Solution

  • Register your route with (:all) instead of (:any).

    From the Laravel docs:

    • (:any) : Allowing a URI segment to be any alpha-numeric string
    • (:all) : Catching the remaining URI without limitation

    Your route could then look something like this:

    Route::get('home/test/(:all?)', 'home@test');
    

    More on that topic in the Laravel forum here.