Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel-扩展ResourceController_Php_Laravel - Fatal编程技术网

Php Laravel-扩展ResourceController

Php Laravel-扩展ResourceController,php,laravel,Php,Laravel,我有两种不同的形式,应该使用一个控制器。我在routes.php中使用以下方法将此控制器创建为资源控制器: Route::resource('account', 'AccountController'); 在我使用的第一种形式中 {{ Form::model($user, array('route' => array('account.update', $user->id), 'method' => 'PUT', 'class' => 'form-outside', '

我有两种不同的形式,应该使用一个控制器。我在routes.php中使用以下方法将此控制器创建为资源控制器:

Route::resource('account', 'AccountController');
在我使用的第一种形式中

{{ Form::model($user, array('route' => array('account.update', $user->id), 'method' => 'PUT', 'class' => 'form-outside', 'files' => true)) }}
因为资源控制器知道更新功能,所以它工作得很好

但是,我想创建第二个表单,它与帐户相关,并更新用户配置文件的其他部分。现在我想通过像updateSettings这样的函数来扩展控制器

但是我怎么能做到呢?我在AccountController中创建了一个函数,并在第二种形式中使用了以下内容:

{{ Form::model($user, array('action' => array('AccountController@updateSettings', $user->id), 'method' => 'PUT', 'class' => 'form-outside')) }}
但拉威尔提出了一个例外

路线[AccountController@updateSettings]没有定义

我如何扩展我的资源控制器,使更新设置成为一种有效的方法?或者在没有资源控制器的情况下如何使用模型绑定表单


谢谢

要完全扩展资源控制器,您需要扩展该类 照亮\路由\路由器并覆盖所需的方法。下一步是使用App::bind将旧的Router类替换为您将要编写的新类

更快的方法是在routes.php中为ex使用一个新路由,并给它一个唯一的名称: 应用程序::放置“更新设置”AccountController@updateSettings'. 祝你好运

或者在没有资源的情况下如何使用模型绑定表单 控制器

因此,在这种情况下,您可以从控制器绑定模型,实际上您可以手动传递模型,例如,如果您有一个Post模型并希望编辑Post,则可以从控制器手动加载模型并将其传递到视图以与表单绑定,例如:

class PostController extends BaseController {

    //...
    public function getEdit($id)
    {
        $post = Post::find($id);
        return View::make('post.edit')->with('post', $post);
    }

    public function postUpdate($id)
    {
        //...
    }
}
然后在视图/post/edit.blade.php中,您可以使用如下内容:

{{ Form::model($post, array('route' => array('post.update', $post->id) )) }}
Route::get('post/edit/{id}', array('as' => 'post.edit', 'uses' => 'PostController@getEdit'));
Route::post('post/update/{id}', array('as' => 'post.update', 'uses' => 'PostController@postUpdate'));
现在需要为这些方法创建路由,如下所示:

{{ Form::model($post, array('route' => array('post.update', $post->id) )) }}
Route::get('post/edit/{id}', array('as' => 'post.edit', 'uses' => 'PostController@getEdit'));
Route::post('post/update/{id}', array('as' => 'post.update', 'uses' => 'PostController@postUpdate'));