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

查看:27
本文介绍了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 辅助函数使用 Illuminate\Routing\URLGenerator 类来生成 URL,后者又使用 rawurlencode 将根据 RFC 3986 对保留字符进行编码的方法.由于 & 字符是保留的子分隔符,它会自动编码为 %26,这将导致您的 URL 路径看起来像这样 /add1%262.

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;amp;作为 %27的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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