【问题标题】:Sending slug in route using post in form使用表单中的帖子发送 slug
【发布时间】:2017-05-16 08:24:14
【问题描述】:

我正在尝试在路由中发送 {slug}:

Route::post('page-edit/{slug}', 'PageController@postSavePage');

在我看来:

{!! Form::open(array('action' => 'PageController@postSavePage')) !!}

在控制器中:

public function postSavePage($slug = null){
dd($slug);
}

但我有错误:

Missing required parameters for [Route: ] [URI: page-edit/{slug}]. (View: /var/www/html/CMS/resources/views/admin/pages/page-edit.blade.php)

正确的语法是什么?

【问题讨论】:

    标签: php laravel-5 blade slug


    【解决方案1】:

    如果您希望 slug 为 optional,则必须在 routes.php 文件中的参数名称中添加一个问号 (?)。

    像这样:

    Route::post('page-edit/{slug?}', 'PageController@postSavePage');
    

    如果您不这样做,则必须将 slug 添加到表单的 url。像这样:

    {!! Form::open(array('action' => array('PageController@postSavePage', 'slug'))) !!}
    

    更新:

    我想您发送此请求的页面与您尝试发布的页面相同。在这种情况下,最好的办法是将表单的操作字段留空。这将确保表单提交到相同的 url。

    所以你可以这样做:

    {!! Form::open() !!}
    

    【讨论】:

    • 我的网址是:http://localhost:8000/page-edit/strona-2 所以在 slug 中应该是 'strona-2' 但如果我使用 {!! Form::open(array('action' => array('PageController@postSavePage', 'slug'))) !!} 并转储 dd($slug); 显示:'slug'
    • 您必须将'slug' 替换为您的网址中必须包含的值。
    • 非常感谢!真正解决了这个问题:表单提交到同一个url
    【解决方案2】:

    我认为您也缺少在刀片模板中发送 {slug} 参数:

    {!! Form::open(array('action' => 'PageController@postSavePage', $slug))) !!}
    

    在你的控制器中:

    public function postSavePage($slug = null){
      // here's you can define $slug var, for example : 
      $slug = 3;
      return view('admin.pages.page-edit', compact('slug'));
    }
    

    【讨论】:

    • 当我在表单中使用$slug 时出现错误:Undefined variable: slug (View: /var/www/html/CMS/resources/views/admin/pages/page-edit.blade.php)
    • 我已经更新了我的答案,我写的视图的名称可能不正确,你必须检查它。您必须将包含 slug 的 var 传递给视图,然后将其传递给 Form。希望这会有所帮助;)
    猜你喜欢
    • 1970-01-01
    • 2013-03-16
    • 1970-01-01
    • 1970-01-01
    • 2018-02-09
    • 1970-01-01
    • 2017-03-15
    • 2021-10-15
    • 2018-08-26
    相关资源
    最近更新 更多