Laravel 5框架学习之子视图和表单复用
我们需要处理编辑文章的问题。当然我们可以手工添加新的路由,就像这样:
1 | Route::get( '/articles/{id}/edit' , 'ArticleController@edit' ); |
让我们在命令行下使用 artisan 的 route:list 来查看我们当前的路由:
1 | php artisan route:list |
1 | Route::resource( 'articles' , 'ArticlesController' ); |
现在在控制器中添加方法:
1 2 3 4 5 | public function edit( $id ) { $article = Article::findOrFail( $id ); return view( 'articles.edit' , compact( 'article' )); } |
1 2 3 4 5 6 7 8 | @ extends ( 'layout' ) @section( 'content' ) <h1>Edit: {!! $article ->title !!} </h1> <hr/> ... |
1 | {!! Form::open([ 'method' => 'PATCH' , 'url' => 'articles/' . $article ->id]) !!} |
一问题是,我们编辑文章,但是文章的信息并没有显示出来,我们修改一下视图:
1 | {!! Form::model( $article , [ 'method' => 'PATCH' , 'url' => 'articles/' . $article ->id]) !!} |
OK,everything's ok,除了 published_on 字段仍然设置为当前日期,后面我们来处理。
现在在控制器中添加方法:
1 2 3 4 5 6 | public function update( $id , \Illuminate\Http\Request $request ) { $article = Article::findOrFail( $id ); $article ->update( $request ->all()); return redirect( 'articles' ); } |
1 2 3 4 5 6 | public function update( $id , Requests\ArticleRequest $request ) { $article = Article::findOrFail( $id ); $article ->update( $request ->all()); return redirect( 'articles' ); } |
我们直接在 views/articles 下面新建文件 list.blade.php,并把错误处理代码从 create.blade.php 中拷贝出来:
1 2 3 4 5 6 7 | @ if ( $errors ->any()) <ul class = "alert alert-danger" > @ foreach ( $errors ->all() as $error ) <li>{{ $error }}</li> @ endforeach </ul> @ endif |
1 | @ include ( 'articles.list' ) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <div class = "form-group" > {!! Form::label( 'title' , 'Title:' ) !!} {!! Form::text( 'title' , null, [ 'class' => 'form-control' ]) !!} </div> <div class = "form-group" > {!! Form::label( 'body' , 'Body:' ) !!} {!! Form::textarea( 'body' , null, [ 'class' => 'form-control' ]) !!} </div> <div class = "form-group" > {!! Form::label( 'published_at' , 'Publish On:' ) !!} {!! Form::input( 'date' , 'published_at' , date ( 'Y-m-d' ), [ 'class' => 'form-control' ]) !!} </div> <div class = "form-group" > {{--这里要设置变量,依据是编辑还是修改来改变,当然也可以不放置在partial中--}} {!! Form::submit( $submitButtonText , [ 'class' => 'btn btn-primary form-control' ]) !!} </div> |
修改 create.blade.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | @ extends ( 'layout' ) @section( 'content' ) <h1>Write a New Article</h1> <hr/> @ include ( 'articles.list' ) {{--使用我们添加的 illuminate\html 开源库--}} {!! Form::open([ 'url' => 'articles' ]) !!} @ include ( 'articles.form_partial' , [ 'submitButtonText' => 'Add Article' ]) {!! Form::close() !!} @stop |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | @ extends ( 'layout' ) @section( 'content' ) <h1>Edit: {!! $article ->title !!} </h1> <hr/> @ include ( 'articles.list' ) {{--使用我们添加的 illuminate\html 开源库--}} {!! Form::model( $article , [ 'method' => 'PATCH' , 'url' => 'articles/' . $article ->id]) !!} @ include ( 'articles.form_partial' , [ 'submitButtonText' => 'Update Article' ]) {!! Form::close() !!} @stop |
以上就是本文给大家介绍的全部内容了,希望能够对大家熟练掌握Laravel5框架有所帮助。
扫码二维码 获取免费视频学习资料
- 本文固定链接: http://phpxs.com/post/6847/
- 转载请注明:转载必须在正文中标注并保留原文链接
- 扫码: 扫上方二维码获取免费视频资料
查 看2022高级编程视频教程免费获取