Laravel 4.2链接到动作链接&为%27 [英] Laravel 4.2 link to action links & as %27

查看:91
本文介绍了Laravel 4.2链接到动作链接&为%27的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我有以下一行:

{{ link_to_action('FriendController@add', 'Friend ' .explode(" ", $user->name)[0],[Auth::user()->id,$user->id]) }}

和定义为

Route::post('friend/add{id}&{fid}', ['as' => 'friend.add', 'uses' => 'FriendController@add']);

但是url无法正确解析2个参数,因为它显示为/add1%262而不是/add?1& 2

however the url isn't parsing the 2 arguments correctly as it comes up as /add1%262 instead of /add?1&2

我不知道为什么,它在以前工作.此外,它附带的[show]方法通过时不存在.

I cant figure out why, it was working before. Also it comes up with method [show] does not exist when it does go through.

推荐答案

这里有两个问题,与您对应该发生的事情的期望不符:

There are two problems here, that fail to match your expectations about what is supposed to happen:

1.首先,也是最重要的一点是,link_to_action辅助函数使用 rawurlencode 方法,该方法将根据

1. The first and most importat one, is that the link_to_action helper function uses the to method in the Illuminate\Routing\URLGenerator class to generate the URL, which in turn makes use of the rawurlencode method that will encode reserved characters according to RFC 3986. Since the & character is a reserved sub-delimiter character it will automatically get encoded to %26, which will result in your URL path looking like this /add1%262.

2..第二个问题是,您像friend/add{id}&{fid}这样定义路由路径,但是您希望其中包含?.即使像friend/add?{id}&{fid}这样添加路由定义,它也仍然无法使用,因为?是定界符并且也将被编码,因此最终会以/add%3F1%262结束.

2. The second issue is that you define your route path like this friend/add{id}&{fid}, yet you expect it to have a ? in there. Even if you were to add it the route definition like so friend/add?{id}&{fid}, it would still not work because ? is a delimiter character and will get encoded as well, so you'll end up with /add%3F1%262.

此处的中心问题是,您不应在Laravel路由定义中定义查询字符串参数.因此,您可以将参数从路由定义移至查询字符串,然后手动构建HTML链接和URL:

The central issue here is that you should not be defining query string parameters in your Laravel route definition. So you either move the parameters from the route definition to the query string and build your HTML link and the URL manually:

// routes.php
Route::post('friend/add', ['as' => 'friend.add', 'uses' => 'FriendController@add']);

// Your Blade template
<a href="{{ action('FriendController@add') }}?{{ Auth::user()->id }}&amp;{{ $user->id }}">
    Friend {{ explode(" ", $user->name)[0] }}
</a>

或更改路由定义,使其不包含任何可能被编码的保留字符:

Or change the route definition so it doesn't contain any reserved characters that might get encoded:

// routes.php
Route::post('friend/add/{id}/{fid}', ['as' => 'friend.add', 'uses' => 'FriendController@add']);

// Your Blade template
{{ link_to_action('FriendController@add', 'Friend ' .explode(" ", $user->name)[0],[Auth::user()->id,$user->id]) }}


话虽如此,即使使用当前生成路径/add1%262的设置,Laravel仍会知道对参数进行解码,因此在控制器操作方法中,您仍将以用户身份获得12 ID:


That being said, even with your current setup that generates the path /add1%262, Laravel would still know to decode the parameters, so in your controller action method you'll still get 1 and 2 as the user IDs:

public function add($id, $fid)
{
    // $id = 1
    // $fid = 2
}

这篇关于Laravel 4.2链接到动作链接&amp;为%27的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆