简体   繁体   English

如何在 Laravel URL 参数中传递 Id

[英]How to Pass Id in Laravel URL Parameter

How to Pass $val->id in Laravel URL Parameter .如何在Laravel URL 参数中传递$val->id I use Following Code.我使用以下代码。 Its Return Error它的返回错误

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

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.但是使用相对路径是一个坏主意,因为如果您想更改 URL 方案,您将需要在视图和控制器中手动查找数十或数百个链接并手动更改它们。 In this case you'll definitely forget to change some of them and some pages of your app will be broken.在这种情况下,您肯定会忘记更改其中的一些,并且您的应用程序的某些页面将被破坏。

A good practice is to use links to route:一个好的做法是使用链接来路由:

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

Or links to action:或操作链接:

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

试试这个:

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

Quick Answer快速回答

As previously explained the way in which to build a URL within a blade template you can use the url helper ;正如前面解释的在刀片模板中构建 URL 的方式,您可以使用url助手 for your example this would work:对于您的示例,这将起作用:

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

Best Practice最佳实践

It is not advised to use url , route or even action when building a url for your views.在为您的视图构建 url 时,不建议使用urlroute甚至action 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 不会改变,如果它们改变,您应该设置 301 重定向,以便您的 SEO 分数保持有效和良好。

So simply do something like:所以只需执行以下操作:

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

But if you REALLY want to use a wrapper then url is 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.url config;使用 laravel 构建 URL 的方式是在字符串前面加上app.url config 的值; found: inside config/app.php找到: 在 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.这主要用于 CLI,当您运行 cron 进程并从带有链接的队列生成电子邮件时,这可以在您的应用程序中使用。 Otherwise do not use it at all .否则根本不要使用它

Using the url helper is good because you can change the value of the app.url config by your own Service Provider .使用url helper 很好,因为您可以通过自己的Service Provider更改app.url配置的值。 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:例子:

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

As also mentioned you can build urls from a route;如前所述,您可以从路由构建 url; this is only valid if you name your routes which is not always the case.这仅在您命名路线时才有效,但并非总是如此。

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

This will find the route by the name route.name and then parse that end-point through app('url') which happens to be the UrlGenerator - so this is no better than the url helper - do not go out of your way to name all routes - that is bad practice.这将通过名称route.name找到路由,然后通过app('url')解析该端点,它恰好是UrlGenerator - 所以这并不比url helper 好 - 不要特意去命名所有路线 - 这是不好的做法。

Another practice mentioned which is also not a good idea is using action by providing the class and action to find a route:提到另一种做法,这也是不是一个好主意是用action通过提供类和行动找到一个途径:

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

This is a bad idea unless MyController is actually an interface NOT a physical class, in that you need Inversion of Control to inject the class that implements MyController otherwise your application will not conform to the SOLID Principle这是一个坏主意,除非MyController实际上是一个接口而不是一个物理类,因为您需要控制反转来注入实现MyController的类,否则您的应用程序将不符合SOLID 原则

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 )}}"

这对我有用:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM