0

Is there a way to change one or more "Route's parameters", and re-build URL based on the modified parameters, ex: change "$page" value.

I tried many ways:

Route::current()->setParameter('page', ++$page);
// also try
Request::route()->setParameter('page',  ++$page);

but always when re-request, URL:

$request->path(); //return path with origin parameter value
$request->url(); //return path with origin parameter value

Is there a way to replace "parameter" value then retrieve URL with updated value? thanks,

4
  • What exactly are you trying to accomplish? Commented Jan 30, 2017 at 6:42
  • update page in URL: ..home/category/1 => ..home/category/2 (Note: page not must at the end of URL) Commented Jan 30, 2017 at 6:43
  • And return next page URL in the response, Commented Jan 30, 2017 at 6:44
  • This logic has to be done inside the controller. You can pass arguments as {page} in routes and then add that to controller like RouteController(Request $request, $page) {}. Commented Jan 30, 2017 at 6:47

2 Answers 2

2
$url = Request::url(); // url without query
$query = Request::query(); // query

//Replace parameter:
$newFullUrl = $url.'?'. http_build_query(array_merge($query, ['parameter' => 'value']);
Sign up to request clarification or add additional context in comments.

1 Comment

While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.
0

I handle it this way: (in controller function)

    Request::route()->setParameter('page', ++$page); //new parameter value
    $parameters = Route::current()->Parameters();
    $uri = Route::current()->uri();
    $uri = substr($uri, 0, strpos($uri, '/{'));

    $full_url = Request::fullUrl();
    $full_url = substr($full_url, 0, strpos($full_url, $uri));
    $full_url = $full_url.$uri.'/'.implode('/',$parameters);
    return $full_url;

Steps:

  1. Update parameter value.
  2. Clear origin route from parameters.
  3. Clear full URL from intersected folders with "origin route".
  4. Re-construct the URL.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.