【问题标题】:Issues with Redirect in Laravel 4.1Laravel 4.1 中的重定向问题
【发布时间】:2014-06-03 15:30:22
【问题描述】:

我今天刚开始尝试 Laravel 4.1,我不得不使用 Laravel 4.0 的教程,所以我不得不对代码的某些部分进行故障排除。 有一个部分我无法解决,我需要一些帮助。

这些是涉及的路线:

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 函数起初运行正常,但是当我单击提交按钮并且它应该执行 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>

【问题讨论】:

    标签: php redirect laravel frameworks


    【解决方案1】:

    由于您使用的是4.1,所以它应该是{id} 而不是(:any),并确保您使用正确的方式生成表单,如下所示:

    Form::open(array('action' => array('AuthorsController@put_update', $author->id), 'method' => 'put'))
    

    同时使用Form::close() 关闭表单。由于您没有使用RESTful 控制器,因此您可以使用方法名称为update 而不是put_update,对于RESTful 方法使用putUpdate 而不是put_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 更改为:

    Route::get('authors/edit/{id}', array('as'=>'edit_author', 'uses'=>'AuthorsController@edit'));
    

    也改变方法:

    public function edit($id)
    {
        //...
    }
    

    【讨论】:

    • 谢谢,这很有帮助。原来我是这样打开我的表单的: 'authors/update','_method'=>'PUT')); ?> ...因为这就是我在“创建”形式中所做的事情,并且工作正常。现在我也改变了我的创建表单开头行......另外,我正在使用一个安静的控制器......或者我认为,因为我在我的控制器“public $restful = true;”中做了这个......但是然后再次,这是我使用 laravel 的第一天,所以如果你知道 4.1 的教程,我会很感激,因为使用 4.0 tut 并不容易。无论如何,非常感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-25
    • 1970-01-01
    • 2017-04-17
    • 2020-09-01
    • 2016-11-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多