Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

passing in arguments to a restful controller in laravel

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?

like image 936
arrowill12 Avatar asked Oct 04 '22 02:10

arrowill12


1 Answers

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.

like image 86
Darwing Avatar answered Oct 07 '22 02:10

Darwing