Reputation: 9240
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
I have tried the following code and it produces http://localhost/blog/Department News
{{ HTML::link('admin/blog/category/' . $post->category, $post->category) }}
How can I escape this and generate the desired URL?
Upvotes: 5
Views: 41573
Reputation: 2037
Sorry. I disagree with Wesley, this works...
If you used named routes the 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.
in twig (TwigBridge) this is ...
{{ 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'
)
);
Upvotes: 0
Reputation: 102864
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)
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.
Upvotes: 7