Php Laravel 4.1中的重定向问题
今天我刚开始尝试Laravel4.1,我不得不使用Laravel4.0的教程,所以我不得不对代码的某些部分进行故障排除。 有一部分我无法排除故障,我需要一些帮助 涉及的路线如下:Php Laravel 4.1中的重定向问题,php,redirect,laravel,frameworks,Php,Redirect,Laravel,Frameworks,今天我刚开始尝试Laravel4.1,我不得不使用Laravel4.0的教程,所以我不得不对代码的某些部分进行故障排除。 有一部分我无法排除故障,我需要一些帮助 涉及的路线如下: Route::get('authors/{id}/edit', array('as'=>'edit_author', 'uses'=>'AuthorsController@get_edit')); Route::put('authors/update', array('uses'=>'Authors
Route::get('authors/{id}/edit', array('as'=>'edit_author', 'uses'=>'AuthorsController@get_edit'));
Route::put('authors/update', array('uses'=>'AuthorsController@put_update'));
以下是控制器中的操作:
public function get_edit($id){
return View::make('authors.edit')->with('title', 'Edit Author')->with('author', Author::find($id));
}
public function put_update(){
$id = Input::get('id');
$author = array(
'name' => Input::get('name'),
'bio' => Input::get('bio'),
);
$validation = Author::validate($author);
if ($validation->fails()){
return Redirect::route('edit_author', $id);
}else{
Author::update($id, $author);
return Redirect::route('view_author', $id);
}
}
注意,在路由中,我使用{id}而不是(:any),因为后者对我不起作用
在我的浏览器上,get_edit函数一开始运行正常,但当我单击submit按钮并执行put_update时,无论它是将我重定向到view_author还是返回edit_author,它只会给我一个NoFoundHttpException
作为补充信息,我使用默认的.htacces,即:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
选项-多视图
重新启动发动机
#重定向尾部斜杠。。。
重写规则^(.*)/$/$1[L,R=301]
#处理前控制器。。。
重写cond%{REQUEST_FILENAME}-D
重写cond%{REQUEST_FILENAME}-F
重写规则^index.php[L]
因为您使用的是4.1
,所以它应该是{id}
而不是(:any)
,并确保使用正确的方法生成表单,如下所示:
Form::open(array('action' => array('AuthorsController@put_update', $author->id), 'method' => 'put'))
还可以使用form::close()
关闭表单。由于您没有使用RESTful
控制器,因此您可以使用方法名称作为update
而不是put\u update
,对于RESTful
方法,请使用putUpdate
而不是put\u update
。因此,您可以使用以下路线:
Route::put('authors/update', array('uses'=>'AuthorsController@update'));
那么方法应该是:
public function update($id)
{
// ...
if ($validation->fails()){
return Redirect::back()->withInput()->withErrors($validation);
}
else{
Author::update($id, $author);
return Redirect::route('view_author', $id);
}
}
所以表格应该是这样的:
Form::open(array('action' => array('AuthorsController@update', $author->id), 'method' => 'put'))
还可以将编辑路线更改为:
Route::get('authors/edit/{id}', array('as'=>'edit_author', 'uses'=>'AuthorsController@edit'));
同时对方法进行更改:
public function edit($id)
{
//...
}
谢谢,这很有帮助。原来我是这样打开我的表单的:……因为这就是我在“创建”表单中所做的,它工作得很好。现在我也改变了我的创建表单开始行。。。另外,我正在使用restful控制器。。。或者我是这么想的,因为我在我的控制器“public$restful=true;”中这样做了。。。不过,这也是我第一天使用laravel,所以如果你知道4.1的教程,我会很感激,因为4.0的tut并不容易。无论如何,非常感谢你的帮助!