Custom routes on Laravel 5.4?

Asked

Viewed 324 times

0

To those who work with Laravel, would know to inform me sources so I can research on how to create URL's clean and friendly so that, I can pass a parameter, for example, category_name, or product_name, and the system of routes recognize and make the call from view correspondent?

Detail: I don’t want to have to pass a prefix like categoria/category_name, produto/product_name, because so I took the test and it works right.

The idea is, take the value of the url, arrive in the database in the related tables and see which match to decide whether it is a category, a subcategory, a store, a product, I believe that the way is not this, even because the Laravel tends to make everything simple, and so is complex.

I thank anyone who can give me some guidance to follow, an article talking about it would be of great help and a good start for me.

  • I ask you: you want to pass for example: rota/{variavel} depending on the variable, have a decision is a control method that makes the call of a given model and its view correspondent???

1 answer

-1

To retrieve some data from the URL, you can use a direct parameter in the route using a variable name between keys, this way:

Route::get('categoria/{categoria}', function($categoria) { return response($categoria); })

The result of this can be tested as follows, by going into: localhost:8000/categoria/carros, the value "cars" will be placed in the variable $categoria, that can be used anywhere.
(In this case, an example sponse carros will be printado onscreen)

More details can be found in the documentation on Route Parameters.

  • "Detail: I don’t want to have to pass a pre-fixed category/category_name, product/product_name". Thanks for the friendly answer, but this way I already did. I want to do without the prefix

  • @Samueldossantos Route::get('categoria/{categoria}', function($categoria) { return view('category_' . $categoria); }) navigate /categoria/xyz will load the view category_xyz and Route::get('produto/{produto}', function($produto) { return view('product_' . $produto); }) navigate /produto/xyz will load the view product_xyz

  • @Guilhermenascimento is another idea, but the url is not 'clean'. Zoom example https://www.zoom.com.br/notebook

  • I thought of for example: pass the url with prefix category/category_name and then store in the session. Once stored, I redirect to category_name and work with the session on the routes. Is it a recommended method or would it be a gambiarra this type of procedure?

  • @Samueldossantos Using session just to clear the URL is a bad idea, first it will break your SEO, second it is the use by a denecessary whim, third the URL has to be intuitive to the user, If you have the /category/ and the /product/ you can even clean but will leave much more confusing. Besides, it would be a lot of work for practically no return.

  • @Samueldossantos Ah, I had not understood it right, so, what you want is even possible, but it would be strange if you have more than just categories (if you notice the example of Zoom is only categories), you can simulate something using JS, SPA style, the routes are given both in the Laravel and in an Angular of life, in JS you use a route more "clean" but pull the data of the most "complex" route of the Laravel, but expensive if it is only to be cleaner, you will be creating a lot of difficulty by perfumery.

  • Indeed, after the conversation here with you and some programmers friends, I saw that it is not worth investing in this type of solution for now. Thank you all for your answers and dedicated time.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.