从 href 调用控制器不返回任何内容(Laravel 5)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31280002/
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
Call to Controller from a href returns nothing (Laravel 5)
提问by Texas
I have to logout the users from my site. I have this in the view:
我必须从我的网站注销用户。我有这个观点:
<a href="{{ URL::route('up_sessions.destroy') }}">Logout</a>
in routes.php I heve this:
在routes.php我heve这个:
Route::resource('up_sessions','SessionsController');
The destroy method from SessionsController does something like
SessionsController 中的 destroy 方法执行类似的操作
session()->put('key', null);
and at the end return Redirect::to('Home');
最后返回 Redirect::to('Home');
When I click on 'Logout' it send me to localhost:8000/up_sessions/%7Bup_sessions%7D and nothing happens. I was expecting a return to 'Home' and this is what I need, click on logout, go to controller, call destroy method, unset the sessions keys and go back to Home. I don't understand why the link has this last segment %7Bup_sessions%7D ...I don't have a route for this and I don't know how to get rid of it. Anyway, I just need to click on logout and logout for good.
当我单击“注销”时,它会将我发送到 localhost:8000/up_sessions/%7Bup_sessions%7D 并且没有任何反应。我期待返回“主页”,这就是我需要的,单击注销,转到控制器,调用销毁方法,取消设置会话键并返回主页。我不明白为什么链接有最后一段 %7Bup_sessions%7D ......我没有这个路线,我不知道如何摆脱它。无论如何,我只需要单击注销和注销即可。
What am I doing wrong ?
我究竟做错了什么 ?
回答by Nehal Hasnayeen
To point your route to your desire controller method use this
要将您的路线指向您想要的控制器方法,请使用此方法
<a href="{{ action('SessionsController@destroy') }}">Logout</a>
回答by Phi Nguyen
You can use link_to_action helper ti accomplish what you want. For example :
您可以使用 link_to_action 助手来完成您想要的操作。例如 :
{!! link_to_action('SessionsController@destroy','Log out',[pramameters,...,...]) !!}
//pass parameter if you have it
Then in your SessionController, you handle how it works
然后在你的 SessionController 中,你处理它是如何工作的
public function destroy($parameter...){
//destroy session...
return redirect('home');
}
回答by Jigs Virani
<a href="{{URL::to('/up_sessions/destroy')}}">Logout</a>