路由参数在 laravel 中不起作用
Route Parameters not working in laravel
我在 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' ]);
但是 returns :
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 参数?
<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>
要么在我写“??”的地方传递一些值在上面的代码中
或
通过后缀使 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>
我在 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' ]);
但是 returns :
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 参数?
<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>
要么在我写“??”的地方传递一些值在上面的代码中
或
通过后缀使 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>