Laravel 4:什么作为参数传递给 Url 类?

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

Laravel 4: What to pass as parameters to the Url class?

laravellaravel-4

提问by Dirk

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

有人可以解释 Laravel 4 UrlGenerator 类的语法吗?我在文档中找不到它。

I have the following route:

我有以下路线:

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

It took me long time to figure out that this:

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

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

generates the desired html:

生成所需的 html:

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

I looked in UrlGenerator.php

我查看了 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.

..但这并没有真正让我更进一步。

What can I pass as $parameters?

我可以通过$parameters什么?

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

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

采纳答案by Jason Lewis

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}

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::action('UsersController@show', ['123'])生成的 URL 将生成 URL localhost/project/public/users/123,就像它已经为您生成的一样。

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.

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

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

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

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

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

The URL would look something like localhost/project/public/users/123/posts/99

该 URL 看起来像 localhost/project/public/users/123/posts/99

回答by Codebryo

Well there is a better way of generating URLs when working with resources.

在处理资源时,有一种更好的生成 URL 的方法。

URL::route('users.index') // Show all users links to UserController@index

URL::route('users.show',$user->id) // Show user with id links to UserController@show($id)

URL::route('users.create') // Show Userform links to UserController@create

URL::route('users.store') // Links to UserController@store

URL::route('users.edit',$user->id) // Show Editform links to UserController@edit($id)

URL::route('users.update',$user->id) // Update the User with id links to UserController@update($id)

URL::route('users.destroy',$user->id) // Deletes a user with the id links to UserController@destroy

Hope that clears things up. Some Documentation on this can be found here http://laravel.com/docs/controllers#resource-controllers

希望这能说明问题。可以在此处找到有关此的一些文档http://laravel.com/docs/controllers#resource-controllers

回答by Dr. Gravy

For those using PHP 5.3, this should be:

对于使用 PHP 5.3 的用户,这应该是:

URL::action('UsersController@show', array('123') )