Laravel中HTML :: link的URLEncode [英] URLEncode for HTML::link in Laravel

查看:183
本文介绍了Laravel中HTML :: link的URLEncode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Laravel中建立一个博客,似乎无法弄清楚如何使用HTML::link(..)生成转义的URL.例如,我在Blog中有指向不同类别的链接,例如Department News,我试图获得这样的链接-http://localhost/blog/category/department+news.,其中Department News$post->category生成

I am setting up a blog in Laravel and cannot seem to figure out how to generate escaped URLs with HTML::link(..). For example, I have links to different categories in the Blog such as Department News, I am trying to get a link formatted like so - http://localhost/blog/category/department+news., where Department News is generated by $post->category

我尝试了以下代码,并产生了http://localhost/blog/Department News

I have tried the following code and it produces http://localhost/blog/Department News

{{ HTML::link('admin/blog/category/' . $post->category, $post->category) }}

我该如何转义并生成所需的URL?

How can I escape this and generate the desired URL?

推荐答案

通常,您将使每个帖子类别在数据库中使用slug列作为URL片段,然后使用类似以下内容的东西:

Usually, you'd have each post category use a slug column in the database for the URL fragment, and then use something like:

HTML::link('blog/category/'.$post->category->slug, $post->category->name)

Laravel似乎无法自动对URL的某些部分进行自动编码,因此您必须自己做:

There's appears to be no way with Laravel to automatically encode only certain parts of the URL, so you'd have to do it yourself:

HTML::link(
    'admin/blog/category/'.urlencode(strtolower($post->category)),
    $post->category
)

您可能要考虑使用塞子"方法.您不必将其存储在数据库中,您可以让您的类动态生成它:

You might want to consider using the "slug" approach. You don't necessarily have to store it in the database, you could have your class generate it on the fly:

class Category {
    function slug() {
        return urlencode(strtolower($this->name));
    }
}

我不确定您正在使用什么,但希望您能理解.

I'm not sure what you're working with exactly, but hopefully you get the idea.

这篇关于Laravel中HTML :: link的URLEncode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆