路线参数在laravel中不起作用

时间:2017-02-17 09:58:35

标签: php laravel laravel-5 laravel-5.2 laravel-5.4

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参数?

1 个答案:

答案 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>