Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Ligonsker's avatar

What is the correct way to get route parameters values inside the controller?

Hello,

So far I've used a mix of 2 ways I found working. Assuming I have the following in web.php:

Route::get('/some_url/{route_param}', [MyController::class, 'my_method']);

1:

public function my_method(Request $request)
{
    $route_parameter = $request->route_param;
}

2:

public function my_method(Request $request)
{
    $route_parameter = $request->route('route_param')
}

Is one preferred over the other? Or there is another way other than these 2 that is better?

Thanks

0 likes
7 replies
Ligonsker's avatar

@MichalOravec And if I also need the request I can also use:

public function my_method($request, $route_param)
{
    echo $route_param; 
}

? I just wonder if it would be better not to repeat stuff, or just add extra stuff if not necessary

gych's avatar

@Ligonsker If you also need the request you use

public function my_method(Request $request, $route_param)
{
    echo $route_param; 
}

What you were doing first is adding extra stuff that's not necessary.

1 like
Ligonsker's avatar

@gych but now it's either adding extra stuff to the method's signature, or, add extra stuff inside the method (read property from the request). Although it does look more neat when you put the parameter in the signature

gych's avatar

@Ligonsker Yes I get what you mean but adding the parameter directly to the method's signature is the best practice and how it should be used.

Another example is that when your parameter is an id of a post, and you want to use route model binding https://laravel.com/docs/11.x/routing#route-model-binding

Then you could use this Route

Route::get('/some_url/{post}', [MyController::class, 'my_method']);

Controller

public function my_method(Request $request, Post $post)
{
    
}

$post now contain the post model isntance and you don't have to get the id from the request first and add a query to fetch it from the DB. Which results in less code that needs to be added.

gych's avatar

You can just use it like this in the controller

public function my_method($route_param)
{
   // then you can just use $route_param in the contorller
}
1 like
martinbean's avatar

@ligonsker Route parameters are passed to the corresponding controller method as parameters. So, if you have the following route definition:

Route::get('/posts/{post}', [PostController::class, 'show']);

Then the show method in your PostController will receive a parameter named $post with that parameter’s value:

public function show($post)
{
    // Do something with $post
}

However, when specifying route parameters you’re usually specifying some sort of identifier for a database record, so you can use route–model binding. If type-hint an Eloquent model class, then Laravel will automatically look up that model for you so that you don’t have to do it yourself in the controller method:

public function show(Post $post)
{
    // The $post is a now a Post model instance
    // A post will be looked up using the ID passed as the {post} parameter value
}

If, for some reason, you need the request in the controller as well, then you can simply add it before any route parameters:

public function show(Request $request, Post $post)
{
    // ...
}

Similarly with form requests:

public function store(StoreCommentRequest $request, Post $post)
{
    $post->comments()->create($request->validated());
}

All of this is documented on the Laravel site:

1 like

Please or to participate in this conversation.