将参数传递给laravel中的一个安静的控制器

arr*_*l12 5 php laravel laravel-4

我刚开始在laravel 4中实现restful控制器.我不明白在使用这种路由方式时如何将参数传递给控制器​​中的函数.

控制器:

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));
    }

}
Run Code Online (Sandbox Code Playgroud)

路线:

 Route::controller('mc', 'McController');
Run Code Online (Sandbox Code Playgroud)

我能够访问这两个URL,因此我的路由工作正常.使用此路由和控制器方法时,如何将参数传递给此控制器?

Dar*_*ing 4

当你在 Laravel 中定义一个 Restful 控制器时,你可以通过 URI 访问操作,例如 withRoute::controller('mc', 'McController')将与路由匹配mc/{any?}/{any?}等。对于你的函数getTable,你可以使用路由来访问,mc/table/mytable其中mytable是函数的参数。

编辑 您必须启用宁静功能,如下所示:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

通过该示例,当我转到路线时,mc/table/hi我得到输出:Im the action getTable with the parameter hi

  • 我的代码中没有 `protected static $restful = true;` 我认为这在 laravel 4 中是不必要的。 (3认同)