php - Laravel URL helper: How to generate a perfect URL with query parameters and hash -


Assume that the path is like this:

  get root: Message / message} ', [' as ''> & gt; message 'show', 'use' => 'messenger controller @ show']);  

Therefore, when we create a URL using Laravel's URL assistant,

  {{route ('messages.show', 12)} }  / Code> 

example.com/messages/12 will appear.

That's right. There is a hash in the URL of

  {{route ('messages.show', [12, '# reply_23'])}}  

this example.com/ Messages / 12 # answer_23 will display.

It feels good to add some query strings instead of the hash.

  {{route ('messages.show', [12, 'ref = email'])}}  

this example.com / Messages / 12? Ref = email .

  {{route ('messages.show', [12, 'ref = email', '# answer_23'])}}  

will display example.com/messages/12?ref=email&#reply_23 . This URL contains & amp; Due to this it looks a bit ugly, although it is not causing much problem, I have to get a clean URL like example.com/messages/12?ref=email#reply_23 . Does the URL contain unnecessary & amp; is there a way to get rid of?

Editing: is an alternative solution, but I have a solid answer.

  & lt; A href = "{{route ('messages.show', [12, 'ref = email'])}}}} # reply_23" & gt; Website to view links & lt; / A & gt;  

Larjal UrlGenerator category # fragment The code responsible for the creation of part url is as follows, and you can see that this query adds string parameters and nothing else:

 Replace $ code = $ ur = strtr (raw $ $ - $ $ -> $ ----- $ root, $ domain- $ parameter); $ this-> substitutes sub parameter ($ path- & gt; Yuri (), $ parameter)), $ This-> DontEncode). $ This- & gt; GetRouteQueryString ($ parameter);  

A quick check of your code reveals that another example you posted:

  {{route ('messages.show' [12, '# answer_23'])}}  

actually generates:

  / message / 12? # Answer_23 // notice "?"  

before "# response_23" then it considers # answer_23 as a parameter instead of a piece.

An alternative to this reduction would be to write a custom assistant function which allows the segment to pass in the form of a third parameter. You can create a file app / helpers.php with your custom function:

  function path_with_fragment ($ name, $ parameters = array (), $ fragment = '', $ Full = true, $ route = zero) {return route ($ name, $ parameter, $ full, $ route). $ Piece; }  

Then add the following line to your app / start / global.php file:

  requires app_path . () '/ Helpers.php';  

You can then use it like this:

  {{route_with_fragment ('messages.show', [12, 'ref = email' ], '# Answer_23')}}  

Of course, whatever you want, you can name the function, if you think that the name I gave is too long.


Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -