缺少参数 1 Laravel 4 控制器功能
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21106002/
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
Missing Argument 1 Laravel 4 Controller function
提问by Creationist
Okay, so a bit new to Laravel 4, and I am stuck on a problem.
好的,对 Laravel 4 来说有点新,我遇到了一个问题。
I am using resource controllers on my routes, but I also have some other functions in my controller. As I said, I am new to Laravel, so I am not even sure that is proper practice.
我在我的路线上使用资源控制器,但我的控制器中还有一些其他功能。正如我所说,我是 Laravel 的新手,所以我什至不确定这是正确的做法。
So, my issue is, is that when I call the edit($id) function as a GET method, and then have the update($id) method as POST, it works fine.
所以,我的问题是,当我将 edit($id) 函数作为 GET 方法调用,然后将 update($id) 方法作为 POST 调用时,它工作正常。
Routes.php
路由.php
Route::get('tasks/edit/{id}', 'TasksController@edit');
Route::post('tasks/edit', 'TasksController@update');
There routes that aren't working are:
有不工作的路线是:
Route::get('tasks/complete/{id}', 'TasksController@complete');
Route::post('tasks/complete', array('as' => 'tasks.completed', 'uses' =>'TasksController@completed')); //I've tried this route a few different ways
In my view, I'm calling the method with the Form::open() call like so:
在我看来,我正在使用 Form::open() 调用调用该方法,如下所示:
{{ Form::open(array('route' => array('tasks.completed', $task->id))) }}
And in my TasksController.php my methods are:
在我的 TasksController.php 中,我的方法是:
/**
* Complete the task
*
* @param int $id
* @return Response
*/
public function complete($id) //GET
{
//Find the task by id and allow to complete
return View::make('tasks.complete')->with('task', Task::find($id));
}
/**
* Update the completion
*
* @param int $id
* @return Response
*/
public function completed($id) //POST
{
$tasks = Task::find($id);
$tasks->complete = Task::completion(); //scope query from Model
$tasks->save();
//Redirect to main tasks list
return Redirect::to('/');
}
No matter what I do, I get the continued error of: Missing Argument 1 for TasksController::completed().
无论我做什么,我都会继续出现以下错误:TasksController::completed() 缺少参数 1。
I don't understand why the edit resource will work with no issues, yet the custom functions won't. I am almost positive I am overlooking something, but I just can't seem to figure out what.
我不明白为什么编辑资源可以正常工作,而自定义函数却不能。我几乎肯定我忽略了一些东西,但我似乎无法弄清楚是什么。
Thanks for your help in advance!
提前感谢您的帮助!
回答by Antonio Carlos Ribeiro
You have to define the id parameter also in the POST route:
您还必须在 POST 路由中定义 id 参数:
Route::post('tasks/complete/{id}', array('as' => 'tasks.completed', 'uses' =>'TasksController@completed'));
The difference in the form tag is subtle.
表单标签的差异是微妙的。
Without the ID on it it builds:
如果没有 ID,它会构建:
<form method="POST" action="http://site/tasks/complete?1" accept-charset="UTF-8">
And with it:
并有了它:
<form method="POST" action="http://site/tasks/complete/1" accept-charset="UTF-8">
But this is enough to the routing system not pass that parameter to your controller and then it's always missing.
但这足以让路由系统不将该参数传递给您的控制器,然后它总是丢失。
回答by swaroop suthar
Laravel default POSTmethod used in form tag
表单标签中使用的Laravel 默认POST方法
and when used get method then defined
当使用 get 方法然后定义
<form method="GET" action="http://site/tasks/complete/1" accept-charset="UTF-8">