laravel中的路由参数有问题, 这是main.blade.php:
<body>
<div class="flex-center position-ref full-height">
<div class="content">
<div class="title m-b-md">
Generate Your Domain Now
</div>
{!! Form::open(['route'=>'generatorindex' , 'method' => 'post']) !!}
<input type="hidden" name="_token" value="{!! csrf_token() !!}">
<input name="inputkeyword" type="text" placeholder="Enter your keyword">
<button type="submit" value="Generate"></button>
{!! Form::close() !!}
</div>
</div>
</body>
这是控制器中的功能:
public function generator(Request $inputkeyword)
{
echo $productname = $inputkeyword->input('inputkeyword');
}
最后,这是路线:
Route::any('/generator/{inputkeyword}', [ 'as' => 'generatorindex', 'uses' => 'MainController@generator' ]);
但它返回:
ErrorException in UrlGenerationException.php line 17:
Missing required parameters for [Route: generatorindex] [URI: generator/{inputkeyword}]. (View: C:\Users\Mostafa\myapp\resources\views\main.blade.php)
我做错了什么?我应该在哪里定义inputkeyword参数?
答案 0 :(得分:2)
<body>
<div class="flex-center position-ref full-height">
<div class="content">
<div class="title m-b-md">
Generate Your Domain Now
</div>
{!! Form::open(['route'=> ['generatorindex', '??'] , 'method' => 'post']) !!}
<input type="hidden" name="_token" value="{!! csrf_token() !!}">
<input name="inputkeyword" type="text" placeholder="Enter your keyword">
<button type="submit" value="Generate"></button>
{!! Form::close() !!}
</div>
</div>
</body>
要么在我写“??”的地方传递一些值在上面的代码中
OR
通过后缀使inputkeyword参数可选 它与“?”到这样的route.php文件:
Route::any('/generator/{inputkeyword?}', [ 'as' => 'generatorindex', 'uses' => 'MainController@generator' ]);
请阅读:
https://laravel.com/docs/5.3/helpers#method-route
https://laravel.com/docs/5.3/routing#parameters-optional-parameters
修改强>
根据您的要求,您在聊天时告诉我:
<强> routes.php文件强>
Route::post('generator', [ 'as' => 'generatorindex', 'uses' => 'MainController@generator' ]);
Route::get('generator/{inputkeyword}', [ 'as' => 'generatorindexurl', 'uses' => 'MainController@generatorindexurl' ]);
<强> MainController.php 强>
public function generator() {
return redirect()->route('generatorindexurl', request('inputkeyword'));
}
public function generatorindexurl()
{
dd(request());
}
查看强>
<body>
<div class="flex-center position-ref full-height">
<div class="content">
<div class="title m-b-md">
Generate Your Domain Now
</div>
{!! Form::open(['route'=> ['generatorindex'] , 'method' => 'post']) !!}
<input type="hidden" name="_token" value="{!! csrf_token() !!}">
<input name="inputkeyword" type="text" placeholder="Enter your keyword">
<button type="submit" value="Generate"></button>
{!! Form::close() !!}
</div>
</div>
</body>