Laravel 5 框架入门(二)构建 Pages 的管理功能(2)

<?php namespace App\Http\Controllers\Admin; use App\Http\Requests; use App\Http\Controllers\Controller; use Illuminate\Http\Request; use App\Page; use Redirect, Input, Auth; class PagesController extends Controller { /** * Show the form for creating a new resource. * * @return Response */ public function create() { return view('admin.pages.create'); } /** * Store a newly created resource in storage. * * @return Response */ public function store(Request $request) { $this->validate($request, [ 'title' => 'required|unique:pages|max:255', 'body' => 'required', ]); $page = new Page; $page->title = Input::get('title'); $page->body = Input::get('body'); $page->user_id = 1;//Auth::user()->id; if ($page->save()) { return Redirect::to('admin'); } else { return Redirect::back()->withInput()->withErrors('保存失败!'); } } /** * Show the form for editing the specified resource. * * @param int $id * @return Response */ public function edit($id) { return view('admin.pages.edit')->withPage(Page::find($id)); } /** * Update the specified resource in storage. * * @param int $id * @return Response */ public function update(Request $request,$id) { $this->validate($request, [ 'title' => 'required|unique:pages,title,'.$id.'|max:255', 'body' => 'required', ]); $page = Page::find($id); $page->title = Input::get('title'); $page->body = Input::get('body'); $page->user_id = 1;//Auth::user()->id; if ($page->save()) { return Redirect::to('admin'); } else { return Redirect::back()->withInput()->withErrors('保存失败!'); } } /** * Remove the specified resource from storage. * * @param int $id * @return Response */ public function destroy($id) { $page = Page::find($id); $page->delete(); return Redirect::to('admin'); } }

4.4 创建视图文件

首先在 learnlaravel5/resources/views 下创建 admin/pages 两级文件夹。

然后创建 learnlaravel5/resources/views/admin/pages/create.blade.php:

@extends('app') @section('content') <div> <div> <div> <div> <div>新增 Page</div> <div> @if (count($errors) > 0) <div> <strong>Whoops!</strong> There were some problems with your input.<br><br> <ul> @foreach ($errors->all() as $error) <li>{{ $error }}</li> @endforeach </ul> </div> @endif <form action="{{ URL('admin/pages') }}" method="POST"> <input type="hidden" value="{{ csrf_token() }}"> <input type="text" required="required"> <br> <textarea rows="10" required="required"></textarea> <br> <button>新增 Page</button> </form> </div> </div> </div> </div> </div> @endsection

之后创建 learnlaravel5/resources/views/admin/pages/edit.blade.php:

@extends('app') @section('content') <div> <div> <div> <div> <div>编辑 Page</div> <div> @if (count($errors) > 0) <div> <strong>Whoops!</strong> There were some problems with your input.<br><br> <ul> @foreach ($errors->all() as $error) <li>{{ $error }}</li> @endforeach </ul> </div> @endif <form action="{{ URL('admin/pages/'.$page->id) }}" method="POST"> <input type="hidden" value="PUT"> <input type="hidden" value="{{ csrf_token() }}"> <input type="text" required="required" value="{{ $page->title }}"> <br> <textarea rows="10" required="required">{{ $page->body }}</textarea> <br> <button>编辑 Page</button> </form> </div> </div> </div> </div> </div> @endsection

4.5 查看结果

后台首页 :88/admin

新增 Page :88/admin/pages/create

编辑 Page :88/admin/pages/1/edit

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/08089cad56181c71fd0f9e916e78597e.html