rdx/laravel-link-url

可变 Laravel 链接与 URL

2.5 2024-09-23 22:29 UTC

This package is auto-updated.

Last update: 2024-09-23 22:31:52 UTC


README

是什么?

正常的 Laravel 链接、URL 和重定向是不可变的,因为它们是已准备好的字符串。你创建它们,然后就没有了。你无法之后添加查询参数,而且根本不能添加 #fragment

这使得链接/URL 不可读

$link = link_to_route('users.show', 'Some label here', ['user' => 321, 'report' => 123], ['title' => 'The title of the link', 'id' => "link-{$user->id}"]);

哪部分是 URL 组件,哪部分是自定义查询参数,哪部分是 HTML 属性..?

如何实现?

添加服务提供者(自动完成)

rdx\linkurl\LinkUrlServiceProvider::class

确保已安装 laravelcollective/html。它用于创建链接。

链接、URL 和重定向现在是可变对象

// Link: <a href="/users/123?report=14#table-reports">View users</a>
echo linkurl_to_route('users.show', 'View user', [$user])->query('report', 14)->fragment('table-reports');

// Link: <a href="/reports/123/delete?_token=huy6543gy654" class="delete">x</a>
echo linkurl_to_route('reports.delete', 'x', [$report])->add('class', 'delete')->withCsrf();

// Url: https://example.com/users/123/edit?return=https%3A%2F%2Fexample.com%2Fusers
$url = routeurl('users.edit', [$user])->query('return', route('users.index'));

// Redirect: /users/123#table-reports
return redirect()->route('users.show', [$user])->fragment('table-reports');

链接和 URL 在 __toString() 方法中被构建,在最后时刻。