「Php/laravel/laravel5/restful」の版間の差分
提供: 初心者エンジニアの簡易メモ
(→一覧テンプレート) |
(→一覧テンプレート) |
||
行81: | 行81: | ||
public function create() | public function create() | ||
{ | { | ||
− | return view(' | + | return view('articles.create'); |
} | } | ||
public function store(Request $request) | public function store(Request $request) | ||
{ | { | ||
− | $user = \App\ | + | $user = \App\article::create(); |
$user->name = $request->name; | $user->name = $request->name; | ||
$user->email = $request->email; | $user->email = $request->email; | ||
$user->save(); | $user->save(); | ||
− | return redirect()->to('/ | + | return redirect()->to('/article'); |
} | } | ||
public function show($id) | public function show($id) | ||
{ | { | ||
− | $ | + | $article = \App\article::find($id); |
− | return view(' | + | return view('articles.show')->with('article',$article); |
} | } | ||
public function edit($id) | public function edit($id) | ||
{ | { | ||
− | $ | + | $article = \App\article::find($id); |
− | return view(' | + | return view('articles.edit')->with('article',$article); |
} | } | ||
public function update(Request $request, $id) | public function update(Request $request, $id) | ||
{ | { | ||
− | $user = \App\ | + | $user = \App\article::find($id); |
$user->name = $request->name; | $user->name = $request->name; | ||
$user->email = $request->email; | $user->email = $request->email; | ||
$user->save(); | $user->save(); | ||
− | return redirect()->to('/ | + | return redirect()->to('/article'); |
} | } | ||
public function destroy($id) | public function destroy($id) | ||
{ | { | ||
− | $user = \App\ | + | $user = \App\article::find($id); |
$user->delete(); | $user->delete(); | ||
− | return redirect()->to('/ | + | return redirect()->to('/article'); |
} | } | ||
-articles/index.blade.php | -articles/index.blade.php | ||
行121: | 行121: | ||
<nowiki><</nowiki>div class="row"> | <nowiki><</nowiki>div class="row"> | ||
<nowiki><</nowiki>div class="col-sm-12"> | <nowiki><</nowiki>div class="col-sm-12"> | ||
− | <nowiki><</nowiki>a href="/ | + | <nowiki><</nowiki>a href="/article/create" class="btn btn-primary" style="margin:20px;">新規登録</a> |
− | </div> | + | <nowiki><<nowiki>/div> |
− | </div> | + | <nowiki><<nowiki>/div> |
<nowiki><</nowiki>table class="table table-striped"> | <nowiki><</nowiki>table class="table table-striped"> | ||
− | @foreach($ | + | @foreach($articles as $article) |
<nowiki><</nowiki>tr> | <nowiki><</nowiki>tr> | ||
− | <nowiki><</nowiki>td>{{$ | + | <nowiki><</nowiki>td>{{$article->id}}</td> |
− | <nowiki><</nowiki>td>{{$ | + | <nowiki><</nowiki>td>{{$article->name}}</td> |
− | <nowiki><</nowiki>td>{{$ | + | <nowiki><</nowiki>td>{{$article->email}}</td> |
− | <nowiki><</nowiki>td><a href="/ | + | <nowiki><</nowiki>td><a href="/article/{{$article->id}}" class="btn btn-primary btn-sm">詳細</a></td> |
− | <nowiki><</nowiki>td><a href="/ | + | <nowiki><</nowiki>td><a href="/article/{{$article->id}}/edit" class="btn btn-primary btn-sm">編集</a></td> |
<nowiki><</nowiki>td> | <nowiki><</nowiki>td> | ||
− | <nowiki><</nowiki>form method="post" action="/ | + | <nowiki><</nowiki>form method="post" action="/article/{{$article->id}}"> |
{!! method_field('delete') !!} | {!! method_field('delete') !!} | ||
<nowiki><</nowiki>input type="hidden" name="_token" value="{{csrf_token()}}"> | <nowiki><</nowiki>input type="hidden" name="_token" value="{{csrf_token()}}"> | ||
行150: | 行150: | ||
<nowiki><</nowiki>div class="row"> | <nowiki><</nowiki>div class="row"> | ||
<nowiki><</nowiki>div class="col-sm-12"> | <nowiki><</nowiki>div class="col-sm-12"> | ||
− | <nowiki><</nowiki>a href="/ | + | <nowiki><</nowiki>a href="/articles" class="btn btn-primary" style="margin:20px;">一覧に戻る</a> |
− | </div> | + | <nowiki><<nowiki>/div> |
− | </div> | + | <nowiki><<nowiki>/div> |
− | <nowiki><</nowiki>form method="post" action="/ | + | <nowiki><</nowiki>form method="post" action="/articles/store"> |
<nowiki><</nowiki>div class="form-group"> | <nowiki><</nowiki>div class="form-group"> | ||
<nowiki><</nowiki>label>名前</label> | <nowiki><</nowiki>label>名前</label> | ||
<nowiki><</nowiki>input type="text" name="name" value="" class="form-control"> | <nowiki><</nowiki>input type="text" name="name" value="" class="form-control"> | ||
− | </div> | + | <nowiki><<nowiki>/div> |
<div class="form-group"> | <div class="form-group"> | ||
<nowiki><</nowiki>label>Email</label> | <nowiki><</nowiki>label>Email</label> | ||
<nowiki><</nowiki>input type="text" name="email" value="" class="form-control"> | <nowiki><</nowiki>input type="text" name="email" value="" class="form-control"> | ||
− | </div> | + | <nowiki><<nowiki>/div> |
<nowiki><</nowiki>input type="hidden" name="_token" value="{{csrf_token()}}"> | <nowiki><</nowiki>input type="hidden" name="_token" value="{{csrf_token()}}"> | ||
<nowiki><</nowiki>input type="submit" value="登録" class="btn btn-primary"> | <nowiki><</nowiki>input type="submit" value="登録" class="btn btn-primary"> | ||
行173: | 行173: | ||
<nowiki><</nowiki>div class="row"> | <nowiki><</nowiki>div class="row"> | ||
<nowiki><</nowiki>div class="col-sm-12"> | <nowiki><</nowiki>div class="col-sm-12"> | ||
− | <nowiki><</nowiki>a href="/ | + | <nowiki><</nowiki>a href="/article" class="btn btn-primary" style="margin:20px;">一覧に戻る</a> |
− | </div> | + | <nowiki><<nowiki>/div> |
− | </div> | + | <nowiki><<nowiki>/div> |
− | <nowiki><</nowiki>form method="post" action="/ | + | <nowiki><</nowiki>form method="post" action="/article/update/{{$article->id}}"> |
<div class="form-group"> | <div class="form-group"> | ||
<nowiki><</nowiki>label>名前</label> | <nowiki><</nowiki>label>名前</label> | ||
− | <nowiki><</nowiki>input type="text" name="name" value="{{$ | + | <nowiki><</nowiki>input type="text" name="name" value="{{$article->name}}" class="form-control"> |
− | </div> | + | <nowiki><<nowiki>/div> |
<nowiki><</nowiki>div class="form-group"> | <nowiki><</nowiki>div class="form-group"> | ||
<nowiki><</nowiki>label>Email</label> | <nowiki><</nowiki>label>Email</label> | ||
− | <nowiki><</nowiki>input type="text" name="email" value="{{$ | + | <nowiki><</nowiki>input type="text" name="email" value="{{$article->email}}" class="form-control"> |
− | </div> | + | <nowiki><<nowiki>/div> |
<nowiki><</nowiki>input type="hidden" name="_token" value="{{csrf_token()}}"> | <nowiki><</nowiki>input type="hidden" name="_token" value="{{csrf_token()}}"> | ||
<nowiki><</nowiki>input type="submit" value="更新" class="btn btn-primary"> | <nowiki><</nowiki>input type="submit" value="更新" class="btn btn-primary"> | ||
行196: | 行196: | ||
<nowiki><</nowiki>div class="row"> | <nowiki><</nowiki>div class="row"> | ||
<nowiki><</nowiki>div class="col-sm-12"> | <nowiki><</nowiki>div class="col-sm-12"> | ||
− | <nowiki><</nowiki>a href="/ | + | <nowiki><</nowiki>a href="/article" class="btn btn-primary" style="margin:20px;">一覧に戻る</a> |
− | </div> | + | <nowiki><<nowiki>/div> |
− | </div> | + | <nowiki><<nowiki>/div> |
<nowiki><</nowiki>table class="table table-striped"> | <nowiki><</nowiki>table class="table table-striped"> | ||
− | <nowiki><</nowiki>tr><td>ID</td><td>{{$ | + | <nowiki><</nowiki>tr><td>ID</td><td>{{$article->id}}</tr> |
− | <nowiki><</nowiki>tr><td>名前</td><td>{{$ | + | <nowiki><</nowiki>tr><td>名前</td><td>{{$article->name}}</tr> |
− | <nowiki><</nowiki>tr><td>E-Mail</td><td>{{$ | + | <nowiki><</nowiki>tr><td>E-Mail</td><td>{{$article->email}}</tr> |
</table> | </table> | ||
@endsection | @endsection |
2016年8月9日 (火) 17:13時点における版
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 | |
テンプレートdirも作る
mkdir resources/views/article
一覧テンプレート
-ArticleController.php
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) { $user = \App\article::create(); $user->name = $request->name; $user->email = $request->email; $user->save(); return redirect()->to('/article'); } public function show($id) { $article = \App\article::find($id); return view('articles.show')->with('article',$article); } public function edit($id) { $article = \App\article::find($id); return view('articles.edit')->with('article',$article); } public function update(Request $request, $id) { $user = \App\article::find($id); $user->name = $request->name; $user->email = $request->email; $user->save(); return redirect()->to('/article'); } public function destroy($id) { $user = \App\article::find($id); $user->delete(); return redirect()->to('/article'); }
-articles/index.blade.php
@extends('layouts.app') @section('content') <h1>一覧表示</h1> <div class="row"> <div class="col-sm-12"> <a href="/article/create" class="btn btn-primary" style="margin:20px;">新規登録</a> <<nowiki>/div> <nowiki><<nowiki>/div> <nowiki><table class="table table-striped"> @foreach($articles as $article) <tr> <td>{{$article->id}}</td> <td>{{$article->name}}</td> <td>{{$article->email}}</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> @endsection
-articles/created.blade.php
@extends('layouts.app') @section('content') <h1>新規作成</h1> <div class="row"> <div class="col-sm-12"> <a href="/articles" class="btn btn-primary" style="margin:20px;">一覧に戻る</a> <<nowiki>/div> <nowiki><<nowiki>/div> <nowiki><form method="post" action="/articles/store"> <div class="form-group"> <label>名前</label> <input type="text" name="name" value="" class="form-control"> <<nowiki>/div> <div class="form-group"> <nowiki><label>Email</label> <input type="text" name="email" value="" class="form-control"> <<nowiki>/div> <nowiki><input type="hidden" name="_token" value="テンプレート:Csrf token()"> <input type="submit" value="登録" class="btn btn-primary"> </form> @endsection
-articles/edit.blade.php
@extends('layouts.app') @section('content') <h1>情報編集</h1> <div class="row"> <div class="col-sm-12"> <a href="/article" class="btn btn-primary" style="margin:20px;">一覧に戻る</a> <<nowiki>/div> <nowiki><<nowiki>/div> <nowiki><form method="post" action="/article/update/{{$article->id}}">
<label>名前</label> <input type="text" name="name" value="{{$article->name}}" class="form-control"> <<nowiki>/div> <nowiki><div class="form-group"> <label>Email</label> <input type="text" name="email" value="{{$article->email}}" class="form-control"> <<nowiki>/div> <nowiki><input type="hidden" name="_token" value="テンプレート:Csrf token()"> <input type="submit" value="更新" class="btn btn-primary"> </form> @endsection
-articles/show.blade.php
@extends('layouts.app') @section('content') <h1>詳細表示</h1> <div class="row"> <div class="col-sm-12"> <a href="/article" class="btn btn-primary" style="margin:20px;">一覧に戻る</a> <<nowiki>/div> <nowiki><<nowiki>/div> <nowiki><table class="table table-striped"> <tr><td>ID</td><td>{{$article->id}}</tr> <tr><td>名前</td><td>{{$article->name}}</tr> <tr><td>E-Mail</td><td>{{$article->email}}</tr> </table> @endsection
参考
https://laravel.com/docs/5.0/controllers
http://readouble.com/laravel/5/0/dev/ja/controllers.html
http://qiita.com/zaburo/items/9fefa3f6834b2e79b734