php 如何将href中的值传递给laravel控制器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34810479/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:14:31  来源:igfitidea点击:

How to pass value inside href to laravel controller?

phplaravel

提问by ShanWave007

This is code snippet from my view file.

这是我的视图文件中的代码片段。

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

Here is my route which I defined inside route file

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

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

I want to pass two values inside href tag to above route and retrieve them in controller. Can someone provide code to do this thing?

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

回答by Emeka Mbah

Since you are trying to pass two parameters to your controller,

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

You controller could look like this:

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

<?php namespace App\Http\Controllers;

class SwitchinfoController extends Controller{

    public function switchInfo($prisw, $secsw){
       //do stuffs here with $prisw and $secsw
    }
}

Your router could look like this

你的路由器可能看起来像这样

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

Then in your Blade

然后在你的刀锋中

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

回答by Shoaib Rehan

You can simply pass parameter in your url like

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

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

and route

和路线

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

and function in controller

和控制器中的功能

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

回答by Froxz

Name your route:

命名您的路线:

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

Pass and array with parameters you want

使用您想要的参数传递和数组

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

and in controller function use

并在控制器功能中使用

function ($parameter){}

Or if don't want to bind parameter to url and want just $_GETparameter like url/?parameter=1

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

You may use it like this

你可以这样使用它

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

function (){
     Input::get('parameter');
}

Docs

文档