「Php/laravel/laravel5/restful」の版間の差分
提供: 初心者エンジニアの簡易メモ
(→テンプレートも作る) |
(→一覧テンプレート) |
||
(同じ利用者による、間の29版が非表示) | |||
行68: | 行68: | ||
| | GET|HEAD | article/{article}/edit | article.edit | App\Http\Controllers\ArticleController@edit | | | | | GET|HEAD | article/{article}/edit | article.edit | App\Http\Controllers\ArticleController@edit | | | ||
− | == | + | *indexがリスト表示 |
+ | *createが新規入力フォーム画面表示 | ||
+ | *storeが新規追加処理 | ||
+ | *showが個別詳細表示 | ||
+ | *editが個別入力フォーム更新画面表示 | ||
+ | *updateが個別更新処理 | ||
+ | *destoryが個別削除処理 | ||
+ | |||
+ | ==テンプレートdirも作る== | ||
mkdir resources/views/article | mkdir resources/views/article | ||
− | == | + | ==restful controller== |
-ArticleController.php | -ArticleController.php | ||
+ | class ArticleController extends Controller | ||
+ | { | ||
public function index() | public function index() | ||
{ | { | ||
$articles = \App\Article::get(); | $articles = \App\Article::get(); | ||
$data = ['articles' => $articles]; | $data = ['articles' => $articles]; | ||
− | return view(' | + | return view('articles.index', $data); |
} | } | ||
+ | public function create() | ||
+ | { | ||
+ | return view('articles.create'); | ||
+ | } | ||
+ | public function store(Request $request) | ||
+ | { | ||
+ | $article = \App\Article::create(); | ||
+ | $article->title= $request->title; | ||
+ | $article->body= $request->body; | ||
+ | $article->save(); | ||
+ | return redirect()->to('/article'); | ||
+ | } | ||
+ | public function show($id) | ||
+ | { | ||
+ | $article = \App\Article::find($id); | ||
+ | $data = ['article' => $article]; | ||
+ | return view('articles.show', $data); | ||
+ | } | ||
+ | public function edit($id) | ||
+ | { | ||
+ | $article = \App\Article::find($id); | ||
+ | $data = ['article' => $article]; | ||
+ | return view('articles.edit', $data); | ||
+ | } | ||
+ | public function update(Request $request, $id) | ||
+ | { | ||
+ | $article = \App\Article::find($id); | ||
+ | $article->title= $request->title; | ||
+ | $article->body= $request->body; | ||
+ | $article->save(); | ||
+ | return redirect()->to('/article'); | ||
+ | } | ||
+ | public function destroy($id) | ||
+ | { | ||
+ | $article = \App\Article::find($id); | ||
+ | $article->delete(); | ||
+ | return redirect()->to('/article'); | ||
+ | } | ||
+ | } | ||
− | - | + | ==一覧テンプレート== |
+ | -layouts/app.blade.php | ||
+ | <!DOCTYPE HTML> | ||
+ | <html lang="ja"> | ||
+ | <head> | ||
+ | <meta charset="utf-8" /> | ||
+ | </head> | ||
+ | <body> | ||
+ | @yield('content') | ||
+ | </body> | ||
+ | </html> | ||
+ | -articles/index.blade.php | ||
@extends('layouts.app') | @extends('layouts.app') | ||
@section('content') | @section('content') | ||
− | <h1> | + | <nowiki><</nowiki>div class="container"> |
− | <table class="table table-striped"> | + | <nowiki><</nowiki>h1>一覧</h1> |
− | @foreach($ | + | <nowiki><</nowiki>div class="row"> |
− | <tr> | + | <nowiki><</nowiki>div class="col-sm-12"> |
− | <td>{{$ | + | <nowiki><</nowiki>a href="/article/create" class="btn btn-primary" style="margin:20px;">新規登録</a> |
− | <td>{{$ | + | </div> |
− | <td>{{$ | + | </div> |
+ | <nowiki><</nowiki>table class="table table-striped"> | ||
+ | @foreach($articles as $article) | ||
+ | <nowiki><</nowiki>tr> | ||
+ | <nowiki><</nowiki>td>{{$article->id}}</td> | ||
+ | <nowiki><</nowiki>td>{{$article->title}}</td> | ||
+ | <nowiki><</nowiki>td>{{$article->body}}</td> | ||
+ | <nowiki><</nowiki>td><a href="/article/{{$article->id}}" class="btn btn-primary btn-sm">詳細</a></td> | ||
+ | <nowiki><</nowiki>td><a href="/article/{{$article->id}}/edit" class="btn btn-primary btn-sm">編集</a></td> | ||
+ | <nowiki><</nowiki>td> | ||
+ | <nowiki><</nowiki>form method="post" action="/article/{{$article->id}}"> | ||
+ | {!! method_field('delete') !!} | ||
+ | <nowiki><</nowiki>input type="hidden" name="_token" value="<nowiki>{{csrf_token()}}</nowiki>"> | ||
+ | <nowiki><</nowiki>input type="submit" value="削除" class="btn btn-danger btn-sm btn-destroy"> | ||
+ | </form> | ||
+ | </td> | ||
</tr> | </tr> | ||
@endforeach | @endforeach | ||
</table> | </table> | ||
+ | </div> | ||
+ | @endsection | ||
+ | |||
+ | -articles/create.blade.php | ||
+ | @extends('layouts.app') | ||
+ | @section('content') | ||
+ | <nowiki><</nowiki>div class="container"> | ||
+ | <nowiki><</nowiki>h1>新規</h1> | ||
+ | <nowiki><</nowiki>div class="row"> | ||
+ | <nowiki><</nowiki>div class="col-sm-12"> | ||
+ | <nowiki><</nowiki>a href="/article" class="btn btn-primary" style="margin:20px;">一覧に戻る</a> | ||
+ | </div> | ||
+ | </div> | ||
+ | <nowiki><</nowiki>form method="post" action="/article"> | ||
+ | <nowiki><</nowiki>div class="form-group"> | ||
+ | <nowiki><</nowiki>label>タイトル</label> | ||
+ | <nowiki><</nowiki>input type="text" name="title" value="" class="form-control"> | ||
+ | </div> | ||
+ | <nowiki><</nowiki>div class="form-group"> | ||
+ | <nowiki><</nowiki>label>本文</label> | ||
+ | <nowiki><</nowiki>input type="text" name="body" value="" class="form-control"> | ||
+ | </div> | ||
+ | <nowiki><</nowiki>input type="hidden" name="_token" value="<nowiki>{{csrf_token()}}</nowiki>"> | ||
+ | <nowiki><</nowiki>input type="submit" value="登録" class="btn btn-primary"> | ||
+ | </form> | ||
+ | </div> | ||
+ | @endsection | ||
+ | |||
+ | -articles/edit.blade.php | ||
+ | @extends('layouts.app') | ||
+ | @section('content') | ||
+ | <nowiki><</nowiki>div class="container"> | ||
+ | <nowiki><</nowiki>h1>編集</h1> | ||
+ | <nowiki><</nowiki>div class="row"> | ||
+ | <nowiki><</nowiki>div class="col-sm-12"> | ||
+ | <nowiki><</nowiki>a href="/article" class="btn btn-primary" style="margin:20px;">一覧に戻る</a> | ||
+ | </div> | ||
+ | </div> | ||
+ | <nowiki><</nowiki>form method="post" action="/article/{{$article->id}}"> | ||
+ | <nowiki>{!! method_field('put') !!}</nowiki> | ||
+ | <nowiki><</nowiki>div class="form-group"> | ||
+ | <nowiki><</nowiki>label>タイトル</label> | ||
+ | <nowiki><</nowiki>input type="text" name="title" value="{{$article->title}}" class="form-control"> | ||
+ | </div> | ||
+ | <nowiki><</nowiki>div class="form-group"> | ||
+ | <nowiki><</nowiki>label>本文</label> | ||
+ | <nowiki><</nowiki>input type="text" name="body" value="{{$article->email}}" class="form-control"> | ||
+ | </div> | ||
+ | <nowiki><</nowiki>input type="hidden" name="_token" value="<nowiki>{{csrf_token()}}</nowiki>"> | ||
+ | <nowiki><</nowiki>input type="submit" value="更新" class="btn btn-primary"> | ||
+ | </form> | ||
+ | </div> | ||
+ | @endsection | ||
+ | |||
+ | -articles/show.blade.php | ||
+ | @extends('layouts.app') | ||
+ | @section('content') | ||
+ | <nowiki><</nowiki>div class="container"> | ||
+ | <nowiki><</nowiki>h1>詳細</h1> | ||
+ | <nowiki><</nowiki>div class="row"> | ||
+ | <nowiki><</nowiki>div class="col-sm-12"> | ||
+ | <nowiki><</nowiki>a href="/article" class="btn btn-primary" style="margin:20px;">一覧に戻る</a> | ||
+ | </div> | ||
+ | </div> | ||
+ | <nowiki><</nowiki>table class="table table-striped"> | ||
+ | <nowiki><</nowiki>tr><td>ID</td><td>{{$article->id}}</tr> | ||
+ | <nowiki><</nowiki>tr><td>タイトル</td><td>{{$article->title}}</tr> | ||
+ | <nowiki><</nowiki>tr><td>本文</td><td>{{$article->body}}</tr> | ||
+ | </table> | ||
+ | </div> | ||
@endsection | @endsection | ||
2017年11月24日 (金) 12:00時点における最新版
RESTfulなcontrollerを作成
$ php artisan make:controller ArticleController
以下ができる
vi app/Http/Controllers/ArticleController.php
namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Http\Requests; class ArticleController extends Controller { // }
vi app/Http/routes.php
Route::resource('article', 'ArticleController');
vi app/Http/Controllers/ArticleController.php
namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Article; use App\Http\Requests; use App\Http\Controllers\Controller; class ArticleController extends Controller { public function index() { return 'index'; } public function create() { return 'create'; } public function store() { return 'store'; } public function show($id) { return 'show' . $id; } public function edit($id) { return 'edit' . $id; } public function update($id) { return 'update' . $id; } public function destroy($id) { return 'destroy' . $id; } }
ttp://localhost/article/1でshow1が表示
その他は以下の通り
$ php artisan route:list +--------+-----------+------------------------+-----------------+------------------------------------------------+------------+ | Domain | Method | URI | Name | Action | Middleware | +--------+-----------+------------------------+-----------------+------------------------------------------------+------------+ | | POST | article | article.store | App\Http\Controllers\ArticleController@store | | | | GET|HEAD | article | article.index | App\Http\Controllers\ArticleController@index | | | | GET|HEAD | article/create | article.create | App\Http\Controllers\ArticleController@create | | | | DELETE | article/{article} | article.destroy | App\Http\Controllers\ArticleController@destroy | | | | PUT|PATCH | article/{article} | article.update | App\Http\Controllers\ArticleController@update | | | | GET|HEAD | article/{article} | article.show | App\Http\Controllers\ArticleController@show | | | | GET|HEAD | article/{article}/edit | article.edit | App\Http\Controllers\ArticleController@edit | |
- indexがリスト表示
- createが新規入力フォーム画面表示
- storeが新規追加処理
- showが個別詳細表示
- editが個別入力フォーム更新画面表示
- updateが個別更新処理
- destoryが個別削除処理
テンプレートdirも作る
mkdir resources/views/article
restful controller
-ArticleController.php
class ArticleController extends Controller { public function index() { $articles = \App\Article::get(); $data = ['articles' => $articles]; return view('articles.index', $data); } public function create() { return view('articles.create'); } public function store(Request $request) { $article = \App\Article::create(); $article->title= $request->title; $article->body= $request->body; $article->save(); return redirect()->to('/article'); } public function show($id) { $article = \App\Article::find($id); $data = ['article' => $article]; return view('articles.show', $data); } public function edit($id) { $article = \App\Article::find($id); $data = ['article' => $article]; return view('articles.edit', $data); } public function update(Request $request, $id) { $article = \App\Article::find($id); $article->title= $request->title; $article->body= $request->body; $article->save(); return redirect()->to('/article'); } public function destroy($id) { $article = \App\Article::find($id); $article->delete(); return redirect()->to('/article'); } }
一覧テンプレート
-layouts/app.blade.php
<!DOCTYPE HTML> <html lang="ja"> <head> <meta charset="utf-8" /> </head> <body> @yield('content') </body> </html>
-articles/index.blade.php
@extends('layouts.app') @section('content') <div class="container"> <h1>一覧</h1> <div class="row"> <div class="col-sm-12"> <a href="/article/create" class="btn btn-primary" style="margin:20px;">新規登録</a> </div> </div> <table class="table table-striped"> @foreach($articles as $article) <tr> <td>{{$article->id}}</td> <td>{{$article->title}}</td> <td>{{$article->body}}</td> <td><a href="/article/{{$article->id}}" class="btn btn-primary btn-sm">詳細</a></td> <td><a href="/article/{{$article->id}}/edit" class="btn btn-primary btn-sm">編集</a></td> <td> <form method="post" action="/article/{{$article->id}}"> {!! method_field('delete') !!} <input type="hidden" name="_token" value="{{csrf_token()}}"> <input type="submit" value="削除" class="btn btn-danger btn-sm btn-destroy"> </form> </td> </tr> @endforeach </table> </div> @endsection
-articles/create.blade.php
@extends('layouts.app') @section('content') <div class="container"> <h1>新規</h1> <div class="row"> <div class="col-sm-12"> <a href="/article" class="btn btn-primary" style="margin:20px;">一覧に戻る</a> </div> </div> <form method="post" action="/article"> <div class="form-group"> <label>タイトル</label> <input type="text" name="title" value="" class="form-control"> </div> <div class="form-group"> <label>本文</label> <input type="text" name="body" value="" class="form-control"> </div> <input type="hidden" name="_token" value="{{csrf_token()}}"> <input type="submit" value="登録" class="btn btn-primary"> </form> </div> @endsection
-articles/edit.blade.php
@extends('layouts.app') @section('content') <div class="container"> <h1>編集</h1> <div class="row"> <div class="col-sm-12"> <a href="/article" class="btn btn-primary" style="margin:20px;">一覧に戻る</a> </div> </div> <form method="post" action="/article/{{$article->id}}"> {!! method_field('put') !!} <div class="form-group"> <label>タイトル</label> <input type="text" name="title" value="{{$article->title}}" class="form-control"> </div> <div class="form-group"> <label>本文</label> <input type="text" name="body" value="{{$article->email}}" class="form-control"> </div> <input type="hidden" name="_token" value="{{csrf_token()}}"> <input type="submit" value="更新" class="btn btn-primary"> </form> </div> @endsection
-articles/show.blade.php
@extends('layouts.app') @section('content') <div class="container"> <h1>詳細</h1> <div class="row"> <div class="col-sm-12"> <a href="/article" class="btn btn-primary" style="margin:20px;">一覧に戻る</a> </div> </div> <table class="table table-striped"> <tr><td>ID</td><td>{{$article->id}}</tr> <tr><td>タイトル</td><td>{{$article->title}}</tr> <tr><td>本文</td><td>{{$article->body}}</tr> </table> </div> @endsection
参考
https://laravel.com/docs/5.0/controllers