5

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?

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

6 Comments

I can access the rout with mc/table. the get is what lets laravel know to call it for the action get, Therefore making it restful. when i pass another string into the url ex. mc/table/mytable it looks for 'mytable' as a function in the controller. that is the issue I am having. I would like to know if it is even possible to pass arguments to the controller using this way of routing.
This looks like you are using laravel 3. there may be some difference for laravel 4.
No. That example was made with Laravel 4 and I think there is not much difference in L3. It's weird you can pass parameters. Maybe trying a new installation of L4.
I do not have protected static $restful = true; in my code I thought that this was not necessary in laravel 4.
ahhh I guess it ended up being the code in my function! it is working now. thank you!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.