Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing parameter to controller from route in laravel

Tags:

php

laravel

THIS IS A QUESTION FOR LARAVEL 3

Given the following route

Route::get('groups/(:any)', array('as' => 'group', 'uses' => 'groups@show')); 

And the URL I would like to use,

http://www.example.com/groups/1 

I would like to be able to use the (:any) value in my controller.

My controller looks like

class Groups_Controller extends Base_Controller {      public $restful = true;          public function get_show($groupID) {         return 'I am group id ' . $groupID;     }     } 

How is this possible to do? I have tried a few things including the following

Route::get('groups/(:any)', array('as' => 'group', 'uses' => 'groups@show((:1))')); 

but it did not work.

UPDATE

Anytime I try to pass in the arguments as show above i get a 404 error.

Thanks for the help!

like image 380
bretterer Avatar asked Sep 28 '12 21:09

bretterer


2 Answers

You don't need anything special for adding paramaters. Just like you had it.

Route::get('groups/(:any)', array('as' => 'group', 'uses' => 'groups@show'));   class Groups_Controller extends Base_Controller {      public $restful = true;          public function get_show($groupID) {         return 'I am group id ' . $groupID;     }     } 
like image 67
dardub Avatar answered Oct 03 '22 06:10

dardub


This is what you need in 1 line of code.

Route::get('/groups/{groupId}', 'GroupsController@getShow');

Suggestion: Use CamelCase as opposed to underscores, try & follow PSR-* guidelines.

Hope it helps.

like image 29
Eddy Ferreira Avatar answered Oct 03 '22 08:10

Eddy Ferreira