在 Laravel 5 中使用 slugs? [英] Using slugs in laravel 5?
问题描述
我在我的应用程序上做了雄辩的工作.鼻涕虫保存得很好.Buuuut...我如何使用它来创建一个漂亮的 url?
I have made eloquent-sluggable work on my app. Slugs are saved just fine. Buuuut... How do I use it to create a pretty url?
如果可能,我想在我的网址中使用它们而不是身份证号码.
If possible, I would like to use them in my url instead of ID numbers.
推荐答案
是的,你可以在你的 route
和生成的 url
中使用 slug
,例如,如果你声明一个像这样的路由:
Yes, you can use slug
in your route
and generated url
, for example, if you declare a route something like this:
Route::get('users/{username}', 'UserController@profile')->where('profile', '[a-z]+');
然后在您的控制器中,您可以像这样声明方法:
Then in your controller, you may declare the method like this:
public function profile($username)
{
$user = User::where('username', $username)->first();
}
username
是你在这里的 slug,它必须是一个字符串,因为 where()...
在路由声明中.如果传递了 integer
,则找不到 route
并且会抛出 404
错误.
The username
is your slug here and it must be a string because of where()...
in the route declaration. If an integer
is passed then route
couldn't be found and 404
error will be thrown.
这篇关于在 Laravel 5 中使用 slugs?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!