Support the ongoing development of Laravel.io →
Requests Blade Forms
Last updated 2 years ago.
0

@vbmark its ok, but there is a better way to generate urls using named routes...

Route::get('cars/{id}/parts',['as'=>'car.parts', function ($id )
{
    // echo $id;
    // whatever goes here

}]);

having a route registered like above we can generate a url simply using:

URL::route('car.parts',$car->id);

Further information can be found here: http://laravel.com/docs/routing#named-routes

Last updated 2 years ago.
0

That look great, but how to I pass the $id to my controller?

I know I need to put something in "// whatever goes here" but everything I've tried so far doesn't work, e.g.

return Controller:: call('PartsController@index',array($id));
//or
return Controller:: callAction('PartsController@index',array($id));

I also tried various things with

Route::controller...

with no success.

Last updated 2 years ago.
0
Solution

In laravel we can either specify a closure or a controller action to handle a specific request. When using controllers you can register a route to a perticular controller action :

Route::get('cars/{id}/parts',['as'=>'car.parts', 'uses' => 'PartsController@index']);

and the index action :

Class PartsController extends BaseController {
    public function index($id)
       {
            echo $id;
        }
}

Now whenever you will access http://yourdomain.com/cars/123/parts, Index action will be called with 123 as id.

This is equivalent to the previous code.

Last updated 2 years ago.
0

Never mind. I didn't need to change anything except add $id as a parameter to the controller's index function.

EDIT: Thanks usm4n. I saw your answer right after I posted. That's exactly what I did. Thanks!

Last updated 2 years ago.
0

:) no problem!

Last updated 2 years ago.
0

Wow! I now see how awesome named routes are. I've just refactored everything and I can't stop clicking my links and looking at my code.

I want to create a t-shirt, "I <3 Named Routes".

Last updated 2 years ago.
0

lolzzz!

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

neon5 neon5 Joined 21 Feb 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.