首页 > 解决方案 > 有没有办法删除 Laravel URL 中的 %20 ?

问题描述

这是我到目前为止所拥有的。

在刀片模板中

<a href='{{ url("/businessprofile/$business->id/$business->name") }}'>

并在 web.php

Route::get('/businessprofile/{id}/{name}', 'BusinessController@show')

表明

localhost:8000/businessprofile/User%20Info

有没有办法删除 %20 而只显示 localhost:8000/businessprofile/UserInfo ?

标签: laravel

解决方案


Str::slug方法从给定的字符串生成一个 URL 友好的“slug”:

{{ url("/businessprofile/$business->id"."/" . Str::slug($business->name)) }}'>}}

或者,

{{ url("/businessprofile/$business->id"."/" . str_slug($business->name)) }}

如果上述方法不起作用,则将您的路线和视图更改为:

路线

Route::get('/businessprofile/{id}/{name}', 'BusinessController@show')->name('businessprofile.show');

观点

{{ route('businessprofile.show', ['id' => $business->id, 'name' => str_slug($business->name) ]) }}

在此处查看官方文档


推荐阅读