D
D
di2019-05-26 09:22:26
Laravel
di, 2019-05-26 09:22:26

How to build a URL for a given route?

Reading the Lumen documentation https://lumen.laravel.com/docs/5.8/routing.
Reached Optional parameters.
1. Do I understand correctly that in order to create a route that has an optional id parameter (either a number or nothing), that is, both /profile and /profile/7 should be processed in one place, I must write the following code?

$router->get('profile[/{id:[0-9]+}]', ['as' => 'profile', function ($id=null) {
  if(!$id){
    return "id is empty";
  }
  return "id:$id";
}]);

2. How to build a URL in this case? the standard route() function does not do what I expected
route('profile',['id' => 5]) // вернет http://localhost:8000/profile[/5] а хотелось бы http://localhost:8000/profile/5
route('profile')                //вернет http://localhost:8000/profile[/{id:[0-9]+}] а хотелось бы  http://localhost:8000/profile

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alex Wells, 2019-05-26
@Delgus

$router->get('profiles', '[email protected]');
$router->get('profiles/{id}', 'Pro[email protected]');

These are two different rounds. You need to process them in different places.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question