0

I have simple problem, I have to replace %20 and other crap from URL. At the moment it looks like this http://exmaple/profile/about/Eddies%20Plumbing. As you can see it's profile link.

Yes I could add str_replace values before every hyperlink, but I have like 10 of them and I think it's bad practice. Maybe there is better solution? What solution would you use? Thanks.

4 Answers 4

9

That is not crap, that is a valid unicode representation of a space character. And it's encoded because it's one of the characters that are deemed unsafe by RFC1738:

All unsafe characters must always be encoded within a URL. For example, the character "#" must be encoded within URLs even in systems that do not normally deal with fragment or anchor identifiers, so that if the URL is copied into another system that does use them, it will not be necessary to change the URL encoding.

So in order to have pretty URLs, you should avoid using reserved and unsafe characters which need encoding to be valid as part of a URL:

Reserved characters: $ & + , / : ; = ? @

Unsafe characters: Blank/empty space and < > # % { } | \ ^ ~ [ ] `

Instead replace spaces with dashes, which serve the same purpose visually while being a safe character, for example look at the Stack Overflow URL for this question. The URL below looks just fine and readable without spaces in it:

http://exmaple/profile/about/eddies-plumbing

You can use Laravel's str_slug helper function to do the hard work for your:

str_slug('Eddies Plumbing', '-'); // returns eddies-plumbing

The str_slug does more that replace spaces with dashes, it replaces multiple spaces with a single dash and also strips all non-alphanumeric characters, so there's no reliable way to decode it.

That being said, I wouldn't use that approach in the first place. There are two main ways I generally use to identify a database entry:

1. Via an ID

The route path definition would look like this in your case:

/profiles/about/{id}/{slug?} // real path "/profiles/about/1/eddies-plumbing"

The code used to identify the user would look like this User::find($id) (the slug parameter is not needed, it's just there to make the URL more readable, that's why I used the ? to make it optional).

2. Via a slug

The route path definition would look like this in your case:

/profiles/about/{slug} // real path "/profiles/about/eddies-plumbing"

In this case I always store the slug as a column in the users table because it's a property relevant to that user. So the retrieval process is very easy User::where('slug', $slug). Of course using str_slug to generate a valid slug when saving the user to the database. I usually like this approach better because it has the added benefit of allowing the slug to be whatever you want (not really needing to be generated from the user name). This can also allow users to choose their custom URL, and can also help with search engine optimisation.

Sign up to request clarification or add additional context in comments.

5 Comments

and how about decode in my controller? Is there any static method laravel provides?
You need to provide more context on why do you need to decode the URL in your controller. Are you using the name part to identify the user in the database?
So I send username Eddies Plumbing and I expect same value in my controller, because of my query $user = User::where('username', '=', $username)->first();. So my problem is that now I get replaced value. I need simillar method, but it should give me old value of username (before str_slug). I hope it makes sense.
I've updated my answer to take into account your comments.
That was oppinion I was looking for. I will add extra column to my database to keep it fresh and modern. Thanks!
5

The links are urlencoded. Use urldecode($profileLink); to decode them.

Comments

0

I am parsing the url tha i got in this way ->

    $replacingTitle = str_replace('-',' ',$title);

   <a href="example.com/category/{{ str_slug($article->title) }}/" />

Comments

0

In your view ...

 <a href="{{url('your-url'.str_slug($comm->title))}}">{{$comm->title}</a>

and in controller using parsing your url as

public function showBySlug($slug) {

    $title = str_replace('-',' ',$slug); 

    $post = Community::where('title','=',$title)->first();

    return view('show')->with(array(  
        'post' => $post,
    ));
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.