Use form parameters in a route with Laravel 5

2019-07-24 18:22发布

问题:

Imagine I have the following in my routes.php file:

Route::get('/test/{system}/{link}', function($system,$link)
{
    return "test ok";
});

Then, I wanna access this route with a form from my view:

<form action="/" method="get">
    <input type="text" name="system">
    <input type="text" name="link">
    <button type="submit">Go!</button>
<form>

What would be the best way to do that? I know I can do something like:

Route::get('/',function(){
    $input = Request::only('link','system');
    $url = 'test/'.$input['system'].'/'.$input['link'];
    return redirect($url);
});

But I'm not sure if it's the correct way to achieve it.

Thanks in advance!

回答1:

You could have js change the form action and then submit it. Your way is ok, but make sure to put it at the end of the routes file. I would actually have the form submit to some specific route (instead of / maybe /redirect-to or something) and from there redirect.



回答2:

You can do this. I'm not sure if this is the best process.

In routes.php, do this:

Route::get('/form', function(){
    return View::make('formview');
});

Route::get('/formaction', function(){
    $link = Request::input('link');
    $system = Request::input('system');
    return "$link";
});

And in the view, as usual, the following:

<form method="get" action="formaction">
    System: <input type="text" name="system" />
    Link: <input type="text" name="link" />
    <input type="submit" value="ok" />
</form>

The Request of Laravel does not depend on the verb used for the request.