【问题标题】:routes laravel with a variable使用变量路由 laravel
【发布时间】:2019-02-03 00:33:16
【问题描述】:

你好,这是我的控制器:

public function getValues(Request $request){
    $typ=$request->get('typ');
    $stellentyp=$request->get('stellentyp');
    $bereich=$request->get('bereich');
    $view = 'user.'.$stellentyp;
    return view($view,['typ' => $typ, 'stellentyp', $stellentyp, 'bereich', $bereich]);
}

我希望用户可以选择一个“stellentyp”,然后应该显示带有该“stellentyp”的视图。

但是我的路线有问题,他们不知道变量“stellentyp”。 如何将我的控制器与我的路由连接?

我试过了

Route::post('user/{$stellentyp}', 'StartController@getValues')->name('user.{$stellentyp}');

但它不起作用:(。错误是:

缺少 [Route: user] [URI: 用户/{$stellentyp}]。 (看法: C:\xampp\htdocs\j4ylara\resources\views\user\start.blade.php)

【问题讨论】:

  • 缺少 [Route: user] [URI: user/{$stellentyp}] 的必需参数。 (查看:C:\xampp\htdocs\j4ylara\resources\views\user\start.blade.php)这是错误
  • 确保将所有内容都放在问题中,而不是在 cmets 中。您可以编辑已发布的问题。

标签: php laravel routes


【解决方案1】:

路线:-

Route::post('user/{stellentyp}', 'StartController@getValues');

控制器:-

public function getValues(Request $request,$stellentyp=''){
 $typ=$request->get('typ');
 $stellentyp=$request->get('stellentyp');
 $bereich=$request->get('bereich');
 $view = 'user.'.$stellentyp;
 return view($view,['typ' => $typ, 'stellentyp', $stellentyp, 'bereich', $bereich]);
}

使用此代码获得预期的答案。

【讨论】:

    【解决方案2】:

    我明白了:D

    <form action="{{ action('StartController@getValues') }}" method="post" id="postData">
    {{ csrf_field() }}
    <select name="stellentyp">
        <option value="praktika">Praktika</option>
        <option value="fwd">Freiwilligendienste</option>
        <option value="jobs">Ferien- und/oder Nebenjobs</option>
    </select>
    <button type="submit">Jetzt finden</button>
    

    这是我的blade.php

    和我的控制器是一样的,我的路线看起来像这样

    Route::resource('user/start', 'StartController');
    Route::post('user/angebote', 'StartController@getValues');
    

    【讨论】:

      【解决方案3】:

      路由参数与 POST 参数完全不同。

      当您发送一个 post 参数时,它应该在您的表单中作为输入或选择框等。然后您可以使用 $request 中的 get() 方法在控制器中捕获它们(请求类的实例)

      public function getValues(Request $request){
          $request->get("post")
      }
      

      但是路由参数不是以表格形式发送的,我们用下面的方式发送它们

      在刀片文件中

      <form action="{{route("user.values",$user_id)}}">
      

      在路由文件中

      Route::post('user/{id}', 'StartController@getValues')->name('user.{$stellentyp}');
      

      并将它们作为函数参数捕获到控制器中

      public function getValues(Request $request,$user_id){
          dd($user_id);
      }
      

      【讨论】:

      • 我发送 post 参数并将其捕获在我的控制器中并将其存储在名为 $stellentyp 的变量中。现在我想当我选择“Stellentyp”时,它会向我显示这个视图:www.test/$stellentyp.com 就像这样,但我不知道
      • 我应该在控制器还是路由中传递参数?因为我有一个选择框,我想过滤一些东西,所以我想将它存储在变量中
      • 你从哪里得到 user_id?我没有这个
      • @MonaMuhr 这只是我的一个例子,我认为问题是由你的路线引起的。据我所知,laravel 不支持动态路线名称和路线中的所有特殊字符。您的路线是 Route::post('user/{$stellentyp}', 'StartController@getValues')-&gt;name('user.{$stellentyp}'); ,但应该像 Route::post('user/{stellentyp}', 'StartController@getValues')-&gt;name('user.stellentyp');
      【解决方案4】:

      在 laravel 路由中,变量不需要 $ 符号 ,删除 $ 符号并再次测试您的路线,它应该可以工作......

      Route::post('user/{stellentyp}', 'StartController@getValues')->name('user.stellentyp');
      

      并像这样将 $stellentype 添加到您的控制器方法输入中:

      public function getValues(Request $request,$stellentype)
      

      【讨论】:

        【解决方案5】:
        Route::post('user/{stellentyp}', 'StartController@getValues')->name('user.values');
        

        为了在路由路径中添加变量,您不必使用美元信号来声明它。检查上面的代码。此外,路线的名称也不必动态分配。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2017-06-08
          • 2017-05-18
          • 1970-01-01
          • 2015-03-05
          • 1970-01-01
          • 1970-01-01
          • 2015-03-16
          相关资源
          最近更新 更多