php 如何在 Laravel URL 参数中传递 Id

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

How to Pass Id in Laravel URL Parameter

phplaravel

提问by Gokulanathan

How to Pass $val->idin Laravel URL Parameter. I use Following Code. Its Return Error

如何通过$val->idLaravel URL参数。我使用以下代码。它的返回错误

href="{{url('/dashboard/medicinepending/{{$val->id}}')}}"

采纳答案by vipul sorathiya

Try this:

尝试这个:

href="{{ URL('/dashboard/medicinepending/'.$val->id )}}"

回答by Alexey Mezenin

Try this:

尝试这个:

{{ url('/dashboard/medicinepending/', [$val->id]) }}

But it's a bad idea to use relative paths, because if you'll want to change URL scheme, you will need to manually find tens or hundreds links in your views and controllers and change them manually. In this case you'll definitely forget to change some of them and some pages of your app will be broken.

但是使用相对路径是一个坏主意,因为如果您想更改 URL 方案,您将需要在视图和控制器中手动查找数十或数百个链接并手动更改它们。在这种情况下,您肯定会忘记更改其中的一些,并且您的应用程序的某些页面将被破坏。

A good practice is to use links to route:

一个好的做法是使用链接来路由:

{{ route('name.route', ['id' => $val->id]) }}

Or links to action:

或操作链接:

{{ action('MyController@show', $val->id) }}

回答by ash

Quick Answer

快速回答

As previously explained the way in which to build a URL within a blade template you can use the urlhelper; for your example this would work:

如前所述,您可以使用url帮助程序在刀片模板中构建 URL 的方式;对于您的示例,这将起作用:

{{ url('dashboard/medicinepending/', [$val->id] }}

Best Practice

最佳实践

It is not advised to use url, routeor even actionwhen building a url for your views. This is because the URLs will not change, if they do you should have 301 redirects set-up in order for your SEO score to remain valid and good.

不建议使用url,route甚至action在为您的视图构建 url 时。这是因为 URL 不会改变,如果它们改变,您应该设置 301 重定向,以便您的 SEO 分数保持有效和良好。

So simply do something like:

所以只需执行以下操作:

<a href="/dashboard/medicinepending/{{ $val->id }}">

But if you REALLYwant to use a wrapper then urlis the second best thing.

但是,如果您真的想使用包装器,那么这url是第二好的选择。

The only purpose of these helpers are for e-mails or commands via artisan, when fired the cron does not know about the URL only what is defined in the config: app.url

这些助手的唯一目的是通过 发送电子邮件或命令artisan,当被触发时,cron 不知道 URL 只知道配置中定义的内容:app.url

Explanation

解释

The way in which URLs are built with laravel is by prepending the string with the value of app.urlconfig; found: inside config/app.php

使用 laravel 构建 URL 的方式是在字符串前面加上app.urlconfig的值;找到:在 config/app.php 里面

This is mainly for the CLI, when you have cron processes running and generating e-mails from a queue with links, therefore this could be used within your application. Otherwise do not use it at all.

这主要用于 CLI,当您运行 cron 进程并从带有链接的队列生成电子邮件时,这可以在您的应用程序中使用。否则根本不要使用它

Using the urlhelper is good because you can change the value of the app.urlconfig by your own Service Provider. This is very useful for when you have a website in multiple languages and/or load a sites language based on the domain name; example:

使用urlhelper 很好,因为您可以app.url通过自己的Service Provider更改配置的值。当您拥有多语言网站和/或基于域名加载网站语言时,这非常有用;例子:

  • domain.com - would load the US website
  • domain.uk - would load the UK website
  • domain.eu - would load a generic EU website
  • domain.de - would load the German website
  • domain.jp - would load the Japanese website
  • etc
  • domain.com - 将加载美国网站
  • domain.uk - 将加载英国网站
  • domain.eu - 将加载一个通用的欧盟网站
  • domain.de - 将加载德国网站
  • domain.jp - 将加载日本网站
  • 等等

As also mentioned you can build urls from a route; this is only valid if you name your routes which is not always the case.

如前所述,您可以从路由构建 url;这仅在您命名路线时才有效,但并非总是如此。

{{ route('name.route', ['id' => $val->id] }}

This will find the route by the name route.nameand then parse that end-point through app('url')which happens to be the UrlGenerator- so this is no better than the urlhelper - do not go out of your way to name all routes - that is bad practice.

这将通过名称route.name找到路由,然后解析app('url')恰好是UrlGenerator 的端点- 所以这并不比url帮助器更好- 不要特意命名所有路由 - 这很糟糕实践。

Another practice mentioned which is also not a good idea is using actionby providing the class and action to find a route:

提到的另一个也不是一个好主意的做法是action通过提供类和操作来查找路线:

{{ action('MyController@show', $val->id) }}

This is a bad idea unless MyControlleris actually an interface NOTa physical class, in that you need Inversion of Control to inject the class that implements MyControllerotherwise your application will not conform to the S.O.L.I.D Principle

这是一个坏主意,除非MyController实际上是一个接口而不是一个物理类,因为您需要控制反转来注入实现MyController的类,否则您的应用程序将不符合SOLID 原则

回答by Naeem Mushtaq

By the way,i also agree with @Alexey Mezenin, you should prefer route() helper function instead of using url(), route() will give you more flexibility to pass number of params along-with the main URL.

顺便说一句,我也同意@Alexey Mezenin,你应该更喜欢 route() 辅助函数而不是使用 url(),route() 会给你更大的灵活性来传递参数的数量以及主 URL。

Anyways, you shouldn't use blade code syntax ({{}}) again inside another couple of brackets and one more thing, try to avoid starting and ending slashed in the first part of url like below.

无论如何,您不应该在另一对括号内再次使用刀片代码语法({{}}),还有一件事,请尽量避免在 url 的第一部分中开始和结束斜线,如下所示。

You should update your code with the following one, this will hopefully work.

您应该使用以下代码更新您的代码,这有望奏效。

href="{{ url('dashboard/medicinepending'.$val->id )}}"OR

href="{{ url('dashboard/medicinepending'.$val->id )}}"

href="{{ url('dashboard/medicinepending',$val->id )}}"

href="{{ url('dashboard/medicinepending',$val->id )}}"

回答by rome ?

This working for me:

这对我有用:

<a href="{{asset('/dashboard/medicinepending/'.$val->id)}}"></a>