laravel. Replace in url

2019-09-05 16:30发布

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.

标签: php laravel url
4条回答
唯我独甜
2楼-- · 2019-09-05 17:07

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

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

   <a href="example.com/category/{{ str_slug($article->title) }}/" />
查看更多
爷、活的狠高调
3楼-- · 2019-09-05 17:16

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

查看更多
何必那么认真
4楼-- · 2019-09-05 17:18

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.

查看更多
萌系小妹纸
5楼-- · 2019-09-05 17:19

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,
    ));
}
查看更多
登录 后发表回答