skip to Main Content

I have a form that sends a get request to a /player/ route. When I click the submit button, the URL looks something like this:

https://examle.com/player?username=johndoe

I want the URL to look like this:

https://examle.com/player/johndoe

The username will then be passed as a parameter to a controller method:

public function player(string $username)
{
}

Is it possible to do this?

Thanks

2

Answers


  1. Just create a simple route for it to accept username

    Route::post("/player/{username}", [PlayerController::class, "player"])
    ->name("player.submit");
    

    The use the url to that accept username

    <form action"{{ route("player.submit", ["username" => $username]) }}">
    ...
    </form>
    

    I think this is basic, you can read on https://laravel.com/docs/10.x/routing#parameters-and-dependency-injection

    If you need get, you can use other url, then redirect to the player location using same route() function. I assume that you want to submit data, so I put post as example

    Login or Signup to reply.
  2. It’s not the way that HTML form operates. You can consider using jquery like below:

    In web.php

    Route::get('player/{username?}', [PlayerController::class, 'search'])->name('user.search')
    

    In view:

    <input name="user_name" id="user_name"/>
    <a href="{{route('user.search')}}" id="btnSearch">Search</a>
    
    <script>
       let href = "{{route('user.search'}}"
       $("#user_name ").on('change', function(e) {
           $("#btnSearch").attr(href, href + "/" + $(this).val())
       })
    </script>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search