I have just started implenting restful controllers in laravel 4. I do not understand how to pass parameters to the functions in my controllers when using this way of routing.
Controller:
class McController extends BaseController
{
private $userColumns = array("stuff here");
public function getIndex()
{
$apps = Apps::getAllApps()->get();
$apps=$apps->toArray();
return View::make('mc')->nest('table', 'mc_child.table',array('apps'=>$apps, 'columns'=>$this->userColumns));
}
public function getTable($table)
{
$data = $table::getAll()->get();
$data=$data->toArray();
return View::make('mc')->nest('table', 'mc_child.table',array('apps'=>$apps, 'columns'=>$this->userColumns));
}
}
route:
Route::controller('mc', 'McController');
I am able to reach both URLs so my routing is working. How do I pass arguments to this controller when using this method of routing and controllers?
When you define a restful controller in Laravel, you can access the actions throgh the URI, e.g. with Route::controller('mc', 'McController')
will match with routes mc/{any?}/{any?}
etc. For your function getTable
, you can access with the route mc/table/mytable
where mytable
is the parameter for the function.
EDIT You must enable restful feature as follow:
class McController extends BaseController
{
// RESTFUL
protected static $restful = true;
public function getIndex()
{
echo "Im the index";
}
public function getTable($table)
{
echo "Im the action getTable with the parameter ".$table;
}
}
With that example, when I go to the route mc/table/hi
I get the output: Im the action getTable with the parameter hi
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With