「Php/laravel/laravel5/restful」の版間の差分
提供: 初心者エンジニアの簡易メモ
(→テンプレートも作る) |
|||
行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 | | | ||
− | == | + | ==テンプレートdriも作る== |
mkdir resources/views/article | mkdir resources/views/article | ||
+ | ==一覧テンプレート== | ||
+ | -ArticleController.php | ||
+ | public function index() | ||
+ | { | ||
+ | $articles = \App\Article::get(); | ||
+ | $data = ['articles' => $articles]; | ||
+ | return view('article.index', $data); | ||
+ | } | ||
+ | |||
+ | -article/index.blade.php | ||
+ | @extends('layouts.app') | ||
+ | @section('content') | ||
+ | <h1>一覧表示</h1> | ||
+ | <table class="table table-striped"> | ||
+ | @foreach($fusers as $fuser) | ||
+ | <tr> | ||
+ | <td>{{$fuser->id}}</td> | ||
+ | <td>{{$fuser->name}}</td> | ||
+ | <td>{{$fuser->email}}</td> | ||
+ | </tr> | ||
+ | @endforeach | ||
+ | </table> | ||
+ | @endsection | ||
==参考== | ==参考== |
2016年8月9日 (火) 15:47時点における版
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 | |
テンプレートdriも作る
mkdir resources/views/article
一覧テンプレート
-ArticleController.php
public function index() { $articles = \App\Article::get(); $data = ['articles' => $articles]; return view('article.index', $data); }
-article/index.blade.php
@extends('layouts.app') @section('content')
一覧表示
{{$fuser->id}} | {{$fuser->name}} | {{$fuser->email}} |
@endsection
参考
https://laravel.com/docs/5.0/controllers