如何在 Laravel 的 Blade 模板中链接到一个安静的资源?

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

How to link to a restful Resource within a Blade template in Laravel?

phprestlaravellaravel-4laravel-routing

提问by PeterG

I'm trying to figure out the "Laravel" way of referring to a resource link from within a blade template.

我试图找出从刀片模板中引用资源链接的“Laravel”方式。

The context is a CRUD Admin Panel I'm building. The URLs are as follows:

上下文是我正在构建的 CRUD 管理面板。网址如下:

"list" camera resources:
  /admin/cameras

"show" a camera resource:
  /admin/cameras/12

These are working fine.

这些工作正常。

My routes.php:

我的路线.php:

Route::resource('admin/cameras', 'MyAdmin\Controllers\CamerasController');

In the template for the "list" action, I'm trying to add a link on each row to go to the "show" action for that resource. My current working code in views/cameras/index.blade.php:

在“列表”操作的模板中,我尝试在每一行添加一个链接以转到该资源的“显示”操作。我当前在 views/cameras/index.blade.php 中的工作代码:

<span>{{ link_to('/admin/cameras/'.$r['id'], $v); }}</span>

...where $r is the 'iterator' from the enclosing loop. Naturally this generates a url like the "show" above.

...其中 $r 是封闭循环中的“迭代器”。这自然会生成一个类似于上面“show”的 url。

It seems a cleaner way would be to use link_to_route or link_to_action, but I haven't had much luck with either. Based on what I've read so far it doesn't seem possible to setup a named route on a resource. Is the code above optimal or does Laravel have something more elegant to offer here?

似乎更简洁的方法是使用 link_to_route 或 link_to_action,但我对这两种方法都不太走运。根据我目前所读到的内容,似乎不可能在资源上设置命名路由。上面的代码是最优的还是 Laravel 在这里提供了更优雅的东西?

NOTE: I've seen some similar questions, but mine is specific to 'resource' routes.

注意:我看过一些类似的问题,但我的问题是针对“资源”路线的。

回答by JofryHS

If you look at laravel.com/docs/controllers#resource-controllers Resource-ful routes are automatically assigned route names:

如果您查看 laravel.com/docs/controllers#resource-controllers 资源丰富的路由会自动分配路由名称:

+-----------+---------------------------+---------+------------------+
|   Verb    |           Path            | Action  |    Route Name    |
+-----------+---------------------------+---------+------------------+
| GET       | /resource                 | index   | resource.index   |
| GET       | /resource/create          | create  | resource.create  |
| POST      | /resource                 | store   | resource.store   |
| GET       | /resource/{resource}      | show    | resource.show    |
| GET       | /resource/{resource}/edit | edit    | resource.edit    |
| PUT/PATCH | /resource/{resource}      | update  | resource.update  |
| DELETE    | /resource/{resource}      | destroy | resource.destroy |
+-----------+---------------------------+---------+------------------+

Hence if your resource name is admin/cameras, you'll just need to substitute resourcewith admin/cameras

因此,如果您的资源名称是admin/cameras,则只需替换resourceadmin/cameras

And to generate the link to the route:

并生成到路线的链接:

link_to_route('admin/cameras.show', $v, array('id' => $r['id']))

回答by justrohu

You can simple state

你可以简单的状态

Route::[action]('admin/cameras',array('as' => 'name.route','uses' => function(){ }))

then you can do like

那么你可以像

link_to_route('name.route',$title,$paramerter=array(),$attirbutes=array())