@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
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.
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.
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!
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".
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community