php Laravel 中 HTML::link 的 URLEncode

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14200587/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:50:52  来源:igfitidea点击:

URLEncode for HTML::link in Laravel

phplaravel

提问by NightMICU

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 Newsis generated by $post->category

我正在 Laravel 中建立一个博客,但似乎无法弄清楚如何使用HTML::link(..). 例如,我有指向博客中不同类别的链接,例如部门新闻,我试图获得一个格式如下的链接 - http://localhost/blog/category/department+news.,其中Department News$post->category

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

我试过下面的代码,它产生 http://localhost/blog/Department News

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

How can I escape this and generate the desired URL?

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

回答by Wesley Murch

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

通常,您会让每个帖子类别使用slug数据库中的一列作为 URL 片段,然后使用类似的内容:

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

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:

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

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:

您可能需要考虑使用“slug”方法。您不必将它存储在数据库中,您可以让您的类动态生成它:

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.

我不确定你在做什么,但希望你能明白。

回答by Artistan

Sorry. I disagree with Wesley, this works...

对不起。我不同意韦斯利,这有效......

If you used named routesthe Url::route('category_browse',[category])call will encode the values. The second parameter in the route method allows mixed content. So if you have only one parameter in your route you can pass a single value, otherwise an array.

如果您使用命名路由,Url::route('category_browse',[category])调用将对值进行编码。route 方法中的第二个参数允许混合内容。因此,如果您的路由中只有一个参数,则可以传递一个值,否则传递一个数组。

in twig (TwigBridge) this is ...

在树枝(TwigBridge)中,这是......

{{ url_route('category_browse',[category]) }} 

otherwise (Blade) it should be ...

否则(刀片)它应该是......

{{ Url::route('category_browse',[category]) }}

and your names route should be like this ...

你的名字路线应该是这样的......

Route::any('/blog/category/{category}',
    array(
        /*'before'=>'auth_check', -- maybe add a pre-filter */
        'uses' =>'BlogController@category',
        'as' => 'category_browse'
    )
);