Route file with database request

Published 4 months ago by GroundZero

Hi,

I was wondering if this is possible or not, and if yes how to do it. Short explanation:

url possibility 1: www.mysite.com/intel/

url possibility 2 www.mysite.com/amd-ryzen-7-1800x/

The first one is a "brand" page and the second one a "product page". I dislike the use of regex because I honestly do not like .htm at the end e.g.

www.mysite.com/amd-ryzen-7-1800x.htm

to define if it is a product page or not. Is there any other way to do this? e.g. in the routes file check what is searched for (number of rows from a mysql request e.g. the one below) and then display the correct route?

In the end I want a number of different "pages" eg:

Brand page

Product page

Normal page (e.g. index, login, logout, my account, my order et cetera)

Hope someone can help me out with this :)


if num_rows == 0

SELECT id FROM Pages WHERE pageName= "amd-ryzen-7-1800x"```

et cetera, untill I found what was looked for, if no results then a 404 page or what ever I decide to use :)
Best Answer (As Selected By GroundZero)
bunnypro
mushood

i'm guessing that if you do www.mysite.com/product/amd-ryzen-7-1800x , it would not be okay for you.

Coz then you would simple have to do this in router "/product/{$product}

as for brand www.mysite.com/brand/intel

then in router: "/brand/{$brand}"

Also this way, your user can already know from URL if its a product or a brand.

Routing should be kept simple until otherwise needed. I feel you are complicating the issue when there is no need to.

mikevrind

I think @mushood advice is a nice solution to implement.

You could also have a url based on /{brand}/{product}, assuming that a product will always belong to a (single) brand. I think this kind of URL makes more sense anyway than your current choice.

If you want to stick to the single route segment, another option would be to create a single table which contains all url's (brands and product). The URL should be unique. If you apply a polymorphic relation to this table, you can easily get the correct data. This way, one record will point to a brand while another will point to a product. https://laravel.com/docs/5.4/eloquent-relationships#polymorphic-relations

GroundZero

I get your point mushood, except in this case the "SEO experts" have chosen to keep the deepness of the URL short e.g. no prefix, which I personally would prefer (product, brand, et cetera).

I am totally not familiar with polymorphic relations et cetera mikevrind but I will surely check out the link.

Thank you both for your answers and suggestions!

bunnypro
GroundZero

ah that is exactly what I needed haha, thanks so much for your answer bunnypro!

Please sign in or create an account to participate in this conversation.