2017年9月29日 星期五

Laravel,Validation,建立可發文,編輯,刪除的系統


//檔案:routes/web.php
Route::resource('/posts','PostController');


//下指令
php artisan make:controller PostsController


//下指令
php artisan route:list


//結果
+--------+-----------+-------------------+---------------+----------------------------------------------+--------------+
| Domain | Method | URI | Name | Action | Middleware |
+--------+-----------+-------------------+---------------+----------------------------------------------+--------------+
| | GET|HEAD | / | | Closure | web |
| | GET|HEAD | api/user | | Closure | api,auth:api |
| | GET|HEAD | posts | posts.index | App\Http\Controllers\PostsController@index | web |
| | POST | posts | posts.store | App\Http\Controllers\PostsController@store | web |
| | GET|HEAD | posts/create | posts.create | App\Http\Controllers\PostsController@create | web |
| | GET|HEAD | posts/{post} | posts.show | App\Http\Controllers\PostsController@show | web |
| | PUT|PATCH | posts/{post} | posts.update | App\Http\Controllers\PostsController@update | web |
| | DELETE | posts/{post} | posts.destroy | App\Http\Controllers\PostsController@destroy | web |
| | GET|HEAD | posts/{post}/edit | posts.edit | App\Http\Controllers\PostsController@edit | web |
+--------+-----------+-------------------+---------------+----------------------------------------------+--------------+


<!-- 檔案:resources/views/layouts/app.blade.php -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<div class="container">
@yield('content')
</div>
@yield('footer')
</body>
</html>


<!-- 檔案:resources/views/posts/create.blade.php -->
@extends('layouts.app')
@section('content')

<form action="/posts" method="post">
<!-- 前提:要設定apache的vhost -->
<input type="text" name="title" placeholder="寫下標題">
{{csrf_field()}}
<!-- name要跟表格欄位名稱一樣 -->
<input type="submit" name="submit">
</form>

@endsection('footer')


Laravel 提供簡單的方法保護你的應用程式不受到 跨網站請求偽造 攻擊。跨網站請求偽造是一種惡意的攻擊,藉以透過經過身份驗證的使用者身份執行未經授權的命令。

Laravel 會自動產生了一個 CSRF token 給每個活動使用者受應用程式管理的 Session。該 token 用來驗證使用者為實際發出請求至應用程式的使用者。要產生一個隱藏的輸入欄位 _token 包含 CSRF token,你可以使用 csrf_field 輔助函式:



參考:https://laravel.tw/docs/5.1/routing



//檔案:routes/web.php
Route::resource('/posts','PostsController');


<?php
//檔案:app/http/controllers/postscontroller.php
namespace App\Http\Controllers;

use Illuminate\Http\Request;

class PostsController extends Controller
{
public function index(){
return "成功囉";
}
}


瀏覽器輸入:public/posts



呈現:成功囉



    //檔案:app/http/controllers/postscontroller.php
public function create(){
return view('posts.create');
}


瀏覽器輸入:public/posts/create



呈現出




 



    //檔案:app/http/controllers/postscontroller.php
public function store(Request $request){
return $request->all();
}


瀏覽器輸入:public/posts/create,並隨便輸入一個值



呈現出你剛剛輸入的資料所產生的物件(網址列:posts)



<?php
//檔案:app/http/controllers/postscontroller.php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Post;

class PostsController extends Controller
{
public function index(){
return "成功囉";
}
public function create(){
return view('posts.create');
}
public function store(Request $request){

//方法一
Post::create($request->all());

//方法二
// $input=$request->all();
// $input['title']=$request->title;
// Post::create($request->all());

//方法三
// $post=new Post;
// $post->title=$request->title;
// $post->save();

}
}


瀏覽器輸入:public/posts/create,並隨便輸入一個值



呈現空白(網址列:posts),資料寫入資料庫



(註:content和user_id要有預設值)



 



ALTER TABLE `posts` CHANGE `user_id` `user_id` INT(10) 
UNSIGNED NOT NULL DEFAULT '0';


<?php
//檔案:app/http/controllers/postscontroller.php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Post;

class PostsController extends Controller
{
public function index(){
return "成功囉";
}
public function create(){
return view('posts.create');
}
public function store(Request $request){

Post::create($request->all());
return redirect('posts');
}
}


輸入完後自動導回post的index



    //檔案:app/http/controllers/postscontroller.php
public function index(){
$posts=Post::all();
// dd($posts=Post::all());
return view('posts.index',compact('posts'));

}


<!-- 檔案:resources/views/posts/index.blade.php -->
@extends('layouts.app')
@section('content')

<ul>
@foreach($posts as $post)
<li>{{$post->title}}</li>
@endforeach
</ul>

@endsection


index呈現出文章標題列表



 compact 是 PHP 函數,將變數轉換成結合陣列的 key => value



參考:http://full-stack-engineering.blogspot.tw/2014/10/laravel-note--passing-data-to-views.html



    //檔案:app/http/controllers/postscontroller.php
public function show($id){
$post=Post::findOrFail($id);
return view('posts.show',compact('post'));
}


<!-- 檔案:resources/views/posts/show.blade.php -->
@extends('layouts.app')
@section('content')

<h1>{{$post->title}}</h1>

@endsection


瀏覽器輸入:posts/1



出現編號1文章的標題



<!-- 檔案:resources/views/posts/index.blade.php -->
@extends('layouts.app')
@section('content')

<ul>
@foreach($posts as $post)
<li>
<a href="{{route('posts.show',$post->id)}}">
{{$post->title}}
</a>
</li>
@endforeach
</ul>

@endsection


@endsection



瀏覽器輸入:posts



呈現





    //檔案:app/http/controllers/postscontroller.php
public function edit($id){
$post=Post::findOrFail($id);
return view('posts.edit',compact('post'));
}

public function update(Request $request,$id){
$post=Post::findOrFail($id);
$post->update($request->all());
return redirect('posts');
}


<!-- 檔案:resources/views/posts/edit.blade.php -->
@extends('layouts.app')
@section('content')

<h1>編輯文章</h1>

<form action="/posts/{{$post->id}}" method="post">
{{csrf_field()}} <!--建立一個token-->
<input type="hidden" name="_method" value="PUT">
<input type="text" name="title" placeholder="寫下標題" value="{{$post->title}}">
<!-- name要跟表格欄位名稱一樣 -->
<input type="submit" name="submit">
</form>

@endsection


<!-- 檔案:resources/views/posts/show.blade.php -->
@extends('layouts.app')
@section('content')

<h1><a href="{{route('posts.edit',$post->id)}}">{{$post->title}}</a></h1>

@endsection


瀏覽器輸入:posts→可連至列表



按列表細項→可連至文章



按標題→可連至編輯



編輯完成後→可連至列表



<!-- 檔案:resources/views/posts/edit.blade.php -->
@extends('layouts.app')
@section('content')

<h1>編輯文章</h1>

<form action="/posts/{{$post->id}}" method="post">
{{csrf_field()}} <!--建立一個token-->
<input type="hidden" name="_method" value="PUT">
<!-- 參考route-->
<input type="text" name="title" placeholder="寫下標題" value="{{$post->title}}">
<!-- name要跟表格欄位名稱一樣 -->
<input type="submit" name="submit" value="更新">
</form>

<form action="/posts/{{$post->id}}" method="post">
{{csrf_field()}}
<input type="hidden" name="_method" value="DELETE">
<!-- 參考route-->
<input type="submit" value="刪文">
</form>

@endsection


    //檔案:app/http/controllers/postscontroller.php
public function destroy($id){
//方法一
$post=Post::findOrFail($id);
$post->delete();

//方法二
// $post=Post::whereId($id)->delete();
return redirect('/posts');
}



瀏覽器輸入:posts→可連至列表



按列表細項→可連至文章



按標題→可連至編輯



編輯按刪除後→連至列表,並且文章被刪除



沒有留言:

張貼留言