2

Is it possible to add a parameter to the url from the route Controller?

For example, my URL is: myshop.com/order-finished

And his method controller is:

getOrderFinished() {
  // I want add this param
  myshop.com/order-finished?order_number=W00034335
}

I want the final URL to be: myshop.com/order-finished?order_number=W00034335, but I must add the parameter order_number from the controller method.

EDIT: no redirections, please.

2
  • 1
    yes it is possible, use redirects Commented Jul 25, 2017 at 11:26
  • rewrite rule will help you do so. Commented Jul 25, 2017 at 11:29

7 Answers 7

1

Simply, use redirect()->to() method.

public function getOrderFinished(){

    // your parameter value

    $param = W00034335;

    return redirect()->to('myshop.com/order-finished?order_number='.$param);
}
Sign up to request clarification or add additional context in comments.

Comments

1

You should try this:

use Illuminate\Support\Facades\Redirect;


$paramData = 'W00034335';
return Redirect::to('/order-finished?order_number='. $paramData);

Hope this help for you !!!

Comments

1

Is it possible to add a param to the url from the route Controller?

Yes, it is possible. You can redirect route if you want to go to that url. Say the route name is 'order-finished' then in the controller you can do the following :

return redirect()->route('order-finished', ['order_number'=>'W00034335']);

The implication is that the order_number will be appended as a query parameter to that route.

PS: This answer goes specifically if you are referring to a request call to a route, and not the general url configuration of your app.

2 Comments

And to generate a new url with parameters use : url('route', ['order_number'=>'W00034335']); ;)
its will not be correct to do this, there is a behaviour that it gives you the second parameter's array value as a path parameter to the url, so the above example gives..somepage.com/route/W00034335
1

Since all (or most) of the answers are using redirects, I'll try something else. I feel like this is a very clean approach.

In your routes/web.php file, insert this:

Route::get('order/{id}', 'OrderController@show');

And then in your OrderController, have this:

public function show($id)
    {
        $order = Order::find($id);

        return view('orders.show')->with('order', $order);
    }

It's the basic idea of routing, as you can find in the documentation here.

Modify the naming conventions how you will!

Comments

1

Use the request illuminate class.

$url = 'http://localhost';
$qs = [
   'par1' => 'val1',
   'par2' => 'val2',
];

$url = \Illuminate\Http\Request::create($url)->fullUrlWithQuery($qs);

The above code will return http://localhost?par1=val1&par2=val2.

Comments

0

Yes:

redirect('order-finished?order_number=W00034335');

Comments

0

To add a parameter to the url you need to define in route file

<?php 
Route::get('/order-finished/order_number/{id}', [
                'as' => 'front.order.detail',
                'uses' => 'OrderController@getOrderDetail'
            ]);
{{ route('front.order.detail',$order['id']) }}
?>

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.