Home > front end >  Access Laravel URL with ID only [closed]
Access Laravel URL with ID only [closed]

Time:09-18

My application is made in laravel 5.8, I want to ask about the best approach for a unique scenario where I want to display some of my customer profiles like

https://www.example.com/1234

I know that in laravel I have to give a unique identifier before the URL like https://www.example.com/profile/1234 so that it know which function to look for. But for some reason I have to build the above URL.

I tried it directly like:

Route::get('/{id}', 'ExampleController@myfunction')->name('profiler');

But this only works when I put it in bottom of my Routes file. If I place it somewhere in the middle or top. All other links like https://www.example.com/about-us stops working.

My Question is:

  • What is the best way to achieve something like this. (Is it fine that I keep it in the bottom of the route file or will it be some kind of threat or any drawbacks to this approach?)
  • Should I use this approach or add a unique identifier instead.

I would really want to use the URL I mentioned in the start of the question.

Thank you all in advance for helping me.

Peace :)

CodePudding user response:

I think you can specify the type of {id} through regular expression.

Route::get('/{id}', function ($id) {
//
})->where('id', '[0-9] ');

this route will only work if id is numeric for

https://www.example.com/about-us

it the route will not work.

hope that's what you are looking for..

CodePudding user response:

You will indeed have to place this route at the end of the route list. This is because it will match any route that starts with the base URL. Your route requires an id path parameter, but this will match about-us as well.

The route without any path parameter, e.g. just /, won't be matched by this rule since id is required.

  • Related