Laravel 4:将什么作为参数传递给 Url 类? [英] Laravel 4: What to pass as parameters to the Url class?

查看:23
本文介绍了Laravel 4:将什么作为参数传递给 Url 类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人能解释一下 Laravel 4 UrlGenerator 类的语法吗?我在文档中找不到它.

Can somebody explain the syntax of the Laravel 4 UrlGenerator class? I can't find it in the documentation.

我有以下路线:

Route::resource('users', 'UsersController');

我花了很长时间才弄明白:

It took me long time to figure out that this:

{{ Url::action('UsersController@show', ['users' => '123']) }}

生成所需的html:

http://localhost/l4/public/users/123

我查看了 UrlGenerator.php

I looked in UrlGenerator.php

/**
 * Get the URL to a controller action.
 *
 * @param  string  $action
 * @param  mixed   $parameters
 * @param  bool    $absolute
 * @return string
 */
public function action($action, $parameters = array(), $absolute = true)

..但这并没有真正让我走得更远.

..but that doesn't really bring me further.

我可以将什么作为 $parameters 传递?

What can I pass as $parameters?

我现在知道 ['users' =>'123'] 有效,但这是什么背景?还有其他传递数据的方法吗?

I now know that ['users' => '123'] works, but what's the background of this? And are there other ways of passing data?

推荐答案

您实际上并不需要将参数的名称作为数组的键.据我所知,如果没有提供名称,替换将从左到右进行.

You aren't actually required to give the name of the parameter as the key of the array. The replacements will happen from left to right if no names are provided, as far as I can remember.

例如,您的资源控制器路由定义如下所示:

As an example, your resource controllers route definition will look something like this:

/users/{users}

所以,像 URL::action('UsersController@show', ['123']) 这样生成的 URL 将生成 URL localhost/project/public/users/123,就像它已经为你准备的一样.

So, a URL generated like URL::action('UsersController@show', ['123']) will generate the URL localhost/project/public/users/123, much like it has already for you.

因此,您传入的是正确生成 URL 所需的参数.如果资源是嵌套的,则定义可能如下所示.

So what you're passing in are the parameters required for the URL to be generated correctly. If the resource was nested, a definition might look something like this.

/users/{users}/posts/{posts}

要生成 URL,您需要同时传递用户 ID 和帖子 ID.

To generate a URL you'd need to pass both the user ID and the post ID.

URL::action('UsersPostsController@show', ['123', '99']);

URL 类似于 localhost/project/public/users/123/posts/99

这篇关于Laravel 4:将什么作为参数传递给 Url 类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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