本篇教程中,我們將利用 Laravel 5 自帶的開箱即用的 Auth 系統(tǒng)對我們的后臺(tái)進(jìn)行權(quán)限驗(yàn)證,并構(gòu)建出前臺(tái)頁面,對 Pages 進(jìn)行展示。
1. 權(quán)限驗(yàn)證
后臺(tái)地址為 http://localhost:88/admin ,我們的所有后臺(tái)操作都將在此頁面或其子頁面下進(jìn)行。利用 Laravel 5 提供的 Auth,我們只需要改動(dòng)很少部分的路由代碼便可以實(shí)現(xiàn)權(quán)限驗(yàn)證功能。
首先,將路由組的代碼改為:
Route::group(['prefix' => 'admin', 'namespace' => 'Admin', 'middleware' => 'auth'], function()
{
Route::get('/', 'AdminHomeComtroller@index');
Route::resource('pages', 'PagesController');
});
上面代碼中只有一處變化:給 `Route::group()` 的第一個(gè)參數(shù)(一個(gè)數(shù)組)增加了一項(xiàng) `'middleware' => 'auth'`。現(xiàn)在訪問 http://localhost:88/admin ,應(yīng)該會(huì)跳轉(zhuǎn)到登陸頁面。如果沒有跳轉(zhuǎn),也不要驚慌,從右上角退出,重新進(jìn)入即可。
我們的個(gè)人博客系統(tǒng)并不想讓人隨便注冊,下面我們將改動(dòng)部分路由代碼,只保留基本的登錄、注銷功能。
刪掉:
Route::controllers([
'auth' => 'Auth\AuthController',
'password' => 'Auth\PasswordController',
]);
增加:
Route::get('auth/login', 'Auth\AuthController@getLogin');
Route::post('auth/login', 'Auth\AuthController@postLogin');
Route::get('auth/logout', 'Auth\AuthController@getLogout');
帶有權(quán)限驗(yàn)證的最小化功能的后臺(tái)已經(jīng)完成,這個(gè)后臺(tái)目前只管理 Page(頁面)這一種資源。接下來我們將構(gòu)建前臺(tái)頁面,把 Pages 展示出來。
2. 構(gòu)建首頁
先整理路由代碼,將路由的最上面的兩行:
Route::get('/', 'WelcomeController@index');
Route::get('home', 'HomeController@index');
改成:
Route::get('/', 'HomeController@index');
我們將直接使用 HomeController 來支撐我們的前臺(tái)頁面展示。
此時(shí)可以刪除 learnlaravel5/app/Http/Controllers/WelcomeController.php 控制器文件和 learnlaravel5/resources/views/welcome.blade.php 視圖文件。
修改 learnlaravel5/app/Http/Controllers/HomeController.php 為:
1
2
3
4
5
6
7
8
9
10
11
12
|
<?php namespace App\Http\Controllers; use App\Page; class HomeController extends Controller { public function index() { return view( 'home' )->withPages(Page::all()); } } |
控制器構(gòu)造完成。
`view('home')->withPages(Page::all())` 這句話實(shí)現(xiàn)以下功能:
渲染 learnlaravel5/resources/views/home.blade.php 視圖文件
把變量 $pages 傳進(jìn)視圖,$pages = Page::all()
Page::all() 調(diào)用的是 Eloquent 中的 all() 方法,返回 pages 表中的所有數(shù)據(jù)。
接下來我們開始寫視圖文件:
首先,我們將創(chuàng)建一個(gè)前端頁面的統(tǒng)一的外殼,即 `<head>` 部分及 `#footer` 部分。新建 learnlaravel5/resources/views/_layouts/default.blade.php 文件(文件夾請自行創(chuàng)建):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
<!DOCTYPE html> <html lang= "zh-CN" > <head> <meta charset= "utf-8" > <meta http-equiv= "X-UA-Compatible" content= "IE=edge" > <meta name= "viewport" content= "width=device-width, initial-scale=1" > <title>Learn Laravel 5</title> <link href= "/css/app.css" rel= "stylesheet" > <!-- Fonts --> <link href= 'http://fonts.useso.com/css?family=Roboto:400,300' rel= 'stylesheet' type= 'text/css' > </head> <body> <div class = "container" style= "margin-top: 20px;" > @yield( 'content' ) <div id= "footer" style= "text-align: center; border-top: dashed 3px #eeeeee; margin: 50px 0; padding: 20px;" > ©2015 <a href= "http://lvwenhan.com" >JohnLui</a> </div> </div> </body> </html> |
修改 learnlaravel5/resources/views/home.blade.php 文件為:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
@ extends ( '_layouts.default' ) @section( 'content' ) <div id= "title" style= "text-align: center;" > <h1>Learn Laravel 5</h1> <div style= "padding: 5px; font-size: 16px;" >{{ Inspiring::quote() }}</div> </div> <hr> <div id= "content" > <ul> @ foreach ( $pages as $page ) <li style= "margin: 50px 0;" > <div class = "title" > <a href= "{{ URL('pages/'.$page->id) }}" > <h4>{{ $page ->title }}</h4> </a> </div> <div class = "body" > <p>{{ $page ->body }}</p> </div> </li> @ endforeach </ul> </div> @endsection |
第一行 `@extends('_layouts.default')` 代表這個(gè)頁面是 learnlaravel5/resources/views/_layouts/default.blade.php 的子視圖。此時(shí) Laravel 的 視圖渲染系統(tǒng)會(huì)首先載入父視圖,再將此視圖中的 @section('content') 里面的內(nèi)容放入到父視圖中的 @yield('content') 處進(jìn)行渲染。
訪問 http://localhost:88/ ,可以得到如下頁面:
2. 構(gòu)建 Page 展示頁
首先增加路由。在路由文件的第一行下面增加一行:
Route::get('pages/{id}', 'PagesController@show');
新建控制器 learnlaravel5/app/Http/Controllers/PagesController.php,負(fù)責(zé)單個(gè) page 的展示:
1
2
3
4
5
6
7
8
9
10
11
12
|
<?php namespace App\Http\Controllers; use App\Page; class PagesController extends Controller { public function show( $id ) { return view( 'pages.show' )->withPage(Page::find( $id )); } } |
新建視圖 learnlaravel5/resources/views/pages/show.blade.php 文件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
@ extends ( '_layouts.default' ) @section( 'content' ) <h4> <a href= "/" >??返回首頁</a> </h4> <h1 style= "text-align: center; margin-top: 50px;" >{{ $page ->title }}</h1> <hr> <div id= "date" style= "text-align: right;" > {{ $page ->updated_at }} </div> <div id= "content" style= "padding: 50px;" > <p> {{ $page ->body }} </p> </div> @endsection |
全部完成,檢驗(yàn)成果:點(diǎn)擊首頁之中任意一篇文章的標(biāo)題,進(jìn)入文章展示頁,你會(huì)看到以下頁面:
至此,前臺(tái)展示頁面全部完成,教程三結(jié)束。
以上所述就是本文的全部內(nèi)容了,希望能夠?qū)Υ蠹覍W(xué)習(xí)Laravel5框架有所幫助。