如何将数据从单击按钮发送到 Laravel 4 中的控制器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22523735/
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-09-14 09:14:04  来源:igfitidea点击:

How to send data from a button click to a controller in laravel 4

phplaravellaravel-4

提问by user1072337

I am trying to send the id data from a button back to one of my controllers on button click. Depending on the id sent, I want the controller function to redirect the user to different views.

我试图在单击按钮时将 id 数据从按钮发送回我的控制器之一。根据发送的 id,我希望控制器功能将用户重定向到不同的视图。

Button:

按钮:

<a href="/oauth/facebook" id="{{$artist->id}}" class="sign">

/oauth/facebook route:

/oauth/facebook 路线:

Route::get('oauth/{provider}', 'Oauth2Controller@action_session')->before('guest');

Function action_session in Oauth2Controller:

Oauth2Controller 中的函数 action_session:

public function action_session($provider) {

        $id=Input::get('id');

        if($id > 5) {
                return Redirect::to('/fans');
                }
                else {
                return Redirect::to('/artists');
                }
}

I tried using ajax but it seems that the oauth/facebook route is called on button click first, before the ajax request can go through (so the $id field is blank when the controller runs). Is there any way to do this? Thank you.

我尝试使用 ajax,但似乎在单击按钮时首先调用 oauth/facebook 路由,然后才能通过 ajax 请求(因此 $id 字段在控制器运行时为空)。有没有办法做到这一点?谢谢你。

采纳答案by petercoles

You won't find the id in the input using this approach, but you could extend the route with an optional (unless you want it for all providers) parameter like this:

您不会使用这种方法在输入中找到 id,但是您可以使用一个可选的(除非您希望所有提供者都使用它)参数来扩展路由,如下所示:

Route

路线

Route::get('oauth/{provider}/{id?}', 'Oauth2Controller@action_session')->before('guest');

Controller

控制器

public function action_session($provider, $id = null) {

    if ($id > 5) {
        return Redirect::to('fans');
    } else {
        return Redirect::to('artists');
    }
}

Button

按钮

<a href="/oauth/facebook/{{$artist->id}}" class="sign">...</a>