Reputation: 313
I have a problem with route parameters in laravel, Here is the 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>
and here is the function in controller :
public function generator(Request $inputkeyword)
{
echo $productname = $inputkeyword->input('inputkeyword');
}
finally, here is the route :
Route::any('/generator/{inputkeyword}', [ 'as' => 'generatorindex', 'uses' => 'MainController@generator' ]);
but it 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)
What am I doing wrong ? Where should I define inputkeyword parameter ?
Upvotes: 0
Views: 2050
Reputation: 4114
<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>
Either pass some value where I have written "??" in above code
OR
Make inputkeyword parameter optional by suffixing it with "?" into route.php file like this:
Route::any('/generator/{inputkeyword?}', [ 'as' => 'generatorindex', 'uses' => 'MainController@generator' ]);
Please read this:
https://laravel.com/docs/5.3/helpers#method-route
https://laravel.com/docs/5.3/routing#parameters-optional-parameters
EDIT
As per your requirement you told me in chat:
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());
}
view
<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>
Upvotes: 2