将参数传递给 laravel 中的宁静控制器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18520944/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 08:20:57  来源:igfitidea点击:

passing in arguments to a restful controller in laravel

phplaravellaravel-4

提问by arrowill12

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.

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

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?

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

采纳答案by Darwing

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/mytablewhere mytableis the parameter for the function.

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

EDITYou must enable restful feature as follow:

编辑您必须按如下方式启用restful功能:

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/hiI get the output: Im the action getTable with the parameter hi.

在那个例子中,当我去路线时,mc/table/hi我得到输出:Im the action getTable with the parameter hi.