arshdeep05's avatar

Need help for url creation

hi, Is there any method to create Url in laravel like "domain.com/blog/read-{{title}}-here-for-free"

0 likes
14 replies
jlrdw's avatar

-here-for-free

I'd stay away from dashes, perhaps use underscore _

click's avatar

@jlrdw any reason for your statement?

Google thinks differently, no clue why though but

Consider using punctuation in your URLs. The URL http://www.example.com/green-dress.html is much more useful to us than http://www.example.com/greendress.html. We recommend that you use hyphens (-) instead of underscores (_) in your URLs.

Source: https://support.google.com/webmasters/answer/76329?hl=en & https://www.mattcutts.com/blog/dashes-vs-underscores/

1 like
Cronix's avatar

Yes, ask any SEO expert. Dashes are better than underscores.

1 like
jlrdw's avatar

Start using dashes in queries, no reply required.

Cronix's avatar

Not sure why you bring up queries when the question is about urls?

jlrdw's avatar

Well it might be or could be because things taken from the URL are sometimes ran through a query, i.e., an Id taken from query string is used all the time. It's hard to consider one part of an application without giving careful consideration to other parts of the application, and how things will be used.

If OP's Dashes aren't used where a dash is not supposed to be I guess it's fine.

But someone new to this stuff may not be aware of the problem a dash can cause.

click's avatar

@jlrdw this is completely unrelated. Dashes in an url going to a query? What? First of all this has nothing todo with the question of the OP, second of all... I won't have a clue why dashes in an url or dashes in any other value should cause problems with a query. If it is causing problems you have other problems than only dashes in an url.

But to answer the question of the OP. Yes it is possible like cronix already explained. A short example:

Route::get('blog/read-{title}-here-for-free', function($title){
    dd($title);
});

If you now visit your website and go to: /blog/read-some-article-here-for-free you will see some-article on your screen.

jlrdw's avatar

@m-rk

Dashes in an url going to a query? What?

You never get a parameter from uri? Gees ma neeze an id for example is passed all the time in a uri to be used in a query.

Or has laravel changed since yesterday to not allow anything passed in the uri to be used in a query.

I could care less if OP uses a -. OP can use anything OP wants to use. But I was giving my opinion that I would not use a dash.

So you never do this:

public function getIndex(Request $request)
    {
    $param1=$request->input('param1');
    ...
    }

Where param1 comes from the uri. I thought everyone at some point passed data in a querystring or a parameter. If that $param1 has a - could be a problem and I said

If

used in a query.

I guess you did not understand the simple explanation.

For further reading:

http://php.net/manual/en/language.variables.basics.php

A valid variable name starts with a letter or underscore, followed by any number of letters, numbers, or underscores.

So if -here-for-free has to be put in a variable to be later used as a search in a query, I would not use hyphens. I said I, but OP can do anything OP wants to do.

click's avatar

Sorry @jlrdw, maybe it is me but I can't follow your logic. And I do not want to continue with the discussion here because it is not related to the topic.

The reference to variable names is not relevant here. A variable name has no relation to the value of the the route parameter and that is what we are talking about here.

arshdeep05's avatar

@m-rk Yes exactly i need this type of route.

Route::get('blog/read-{title}-here-for-free', function($title){ dd($title); });

I tried it as u said but it gives error "Sorry, the page you are looking for could not be found."

alaki's avatar

I think you are better to do domain.com/blog/Read{{title}}HereForFree and then by simple method just like str_replace convert it to domain.com/blog/read-{{title}}-here-for-free!!

click's avatar

@arshdeep05 what is the url you were trying to visit?

Try: yourdomain.com/blog/read-some-article-here-for-free

Did you place the route in your web.php file?

Is there maybe some other route that could that could match the route you just added? Maybe blog/{title} somewhere?

mfoote's avatar

I'd stay away from dashes, perhaps use underscore _

That made me lol...

Please or to participate in this conversation.