【问题标题】:How to pass value inside href to laravel controller?如何将href中的值传递给laravel控制器?
【发布时间】:2020-06-10 05:32:31
【问题描述】:

这是我的视图文件中的代码 sn-p。

@foreach($infolist as $info)
 <a href="">{{$info->prisw}} / {{$info->secsw}}</a>
@endforeach

这是我在路由文件中定义的路由

Route::get('switchinfo','SwitchinfoController');

我想将 href 标记内的两个值传递给上述路由并在控制器中检索它们。有人可以提供代码来做这件事吗?

【问题讨论】:

    标签: php laravel


    【解决方案1】:

    由于您尝试将两个参数传递给您的控制器,

    您的控制器可能如下所示:

    <?php namespace App\Http\Controllers;
    
    class SwitchinfoController extends Controller{
    
        public function switchInfo($prisw, $secsw){
           //do stuffs here with $prisw and $secsw
        }
    }
    

    您的路由器可能如下所示

    $router->get('/switchinfo/{prisw}/{secsw}',[
        'uses' => 'SwitchinfoController@switchInfo',
        'as'   => 'switch'
    ]);
    

    然后在你的刀片中

    @foreach($infolist as $info)
      <a href="{!! route('switch', ['prisw'=>$info->prisw, 'secsw'=>$info->secsw]) !!}">Link</a>
    @endforeach
    

    【讨论】:

      【解决方案2】:

      您可以简单地在您的网址中传递参数,例如

      @foreach($infolist as $info)
      <a href="{{ url('switchinfo/'.$info->prisw.'/'.$info->secsw.'/') }}">
      {{$info->prisw}} / {{$info->secsw}}
      </a>
      @endforeach
      

      和路线

      Route::get('switchinfo/{prisw}/{secsw}', 'SwitchinfoController@functionname');
      

      控制器中的功能

      public functionname($prisw, $secsw){
        // your code here
      }
      

      【讨论】:

      • 如何从控制器#Rehan 中检索这两个参数?
      • 在控制器中,上面代码中的函数名 $prisw 和 $secsw 是你的值,你可以使用它们来访问参数值
      • 它将在不安全的浏览器地址字段中可见
      【解决方案3】:

      命名您的路线:

      Route::get('switchinfo/{parameter}', [
           'as'=> 'test',
           'uses'=>'SwitchinfoController@function'
      ]);
      

      用你想要的参数传递一个数组

       <a href="{{route('test', ['parameter' => 1])}}">
          {{$info->prisw}} / {{$info->secsw}}
       </a>
      

      并在控制器功能中使用

      function ($parameter) {
         // do stuff
      }
      

      或者如果不想将参数绑定到 url 并且只想要 $_GET 参数,例如 url/?parameter=1

      你可以这样使用它

      Route::get('switchinfo', [
          'as'=> 'test', 
          'uses'=>'SwitchinfoController@function'
      ]);
      
      function (){
           Input::get('parameter');
      }
      

      Docs

      【讨论】:

        猜你喜欢
        • 2019-03-06
        • 1970-01-01
        • 2020-10-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-05-31
        相关资源
        最近更新 更多