本篇教程中,我们将利用 Laravel 5 自带的开箱即用的 Auth 系统对我们的后台进行权限验证,并构建出前台页面,对 Pages 进行展示。
1. 权限验证
后台地址为 http://localhost:88/admin ,我们的所有后台操作都将在此页面或其子页面下进行。利用 Laravel 5 提供的 Auth,我们只需要改动很少部分的路由代码便可以实现权限验证功能。
首先,将路由组的代码改为:
Route::group(['prefix' => 'admin', 'namespace' => 'Admin', 'middleware' => 'auth'], function()
{
Route::get('/', 'AdminHomeComtroller@index');
Route::resource('pages', 'PagesController');
});
上面代码中只有一处变化:给 `Route::group()` 的第一个参数(一个数组)增加了一项 `'middleware' => 'auth'`。现在访问 http://localhost:88/admin ,应该会跳转到登陆页面。如果没有跳转,也不要惊慌,从右上角退出,重新进入即可。
我们的个人博客系统并不想让人随便注册,下面我们将改动部分路由代码,只保留基本的登录、注销功能。
删掉:
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');
带有权限验证的最小化功能的后台已经完成,这个后台目前只管理 Page(页面)这一种资源。接下来我们将构建前台页面,把 Pages 展示出来。
2. 构建首页
先整理路由代码,将路由的最上面的两行:
改成:
我们将直接使用 HomeController 来支撑我们的前台页面展示。
此时可以删除 learnlaravel5/app/Http/Controllers/WelcomeController.php 控制器文件和 learnlaravel5/resources/views/welcome.blade.php 视图文件。
修改 learnlaravel5/app/Http/Controllers/HomeController.php 为:
<?php namespace App\Http\Controllers; use App\Page; class HomeController extends Controller { public function index() { return view('home')->withPages(Page::all()); } }
控制器构造完成。
`view('home')->withPages(Page::all())` 这句话实现以下功能:
渲染 learnlaravel5/resources/views/home.blade.php 视图文件
把变量 $pages 传进视图,$pages = Page::all()
Page::all() 调用的是 Eloquent 中的 all() 方法,返回 pages 表中的所有数据。
接下来我们开始写视图文件:
首先,我们将创建一个前端页面的统一的外壳,即 `
` 部分及 `#footer` 部分。新建 learnlaravel5/resources/views/_layouts/default.blade.php 文件(文件夹请自行创建):<!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 文件为:
@extends('_layouts.default') @section('content') <div id="title" style="text-align: center;"> <h1 id="Learn-Laravel">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 id="page-title">{{ $page->title }}</h4> </a> </div> <div class="body"> <p>{{ $page->body }}</p> </div> </li> @endforeach </ul> </div> @endsection
第一行 `@extends('_layouts.default')` 代表这个页面是 learnlaravel5/resources/views/_layouts/default.blade.php 的子视图。此时 Laravel 的 视图渲染系统会首先载入父视图,再将此视图中的 @section('content') 里面的内容放入到父视图中的 @yield('content') 处进行渲染。
访问 http://localhost:88/ ,可以得到如下页面:
2. 构建 Page 展示页
首先增加路由。在路由文件的第一行下面增加一行:
新建控制器 learnlaravel5/app/Http/Controllers/PagesController.php,负责单个 page 的展示:
<?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 文件:
@extends('_layouts.default') @section('content') <h4> <a href="/">⬅️返回首页</a> </h4> <h1 id="page-title">{{ $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
全部完成,检验成果:点击首页之中任意一篇文章的标题,进入文章展示页,你会看到以下页面:
至此,前台展示页面全部完成,教程三结束。
以上所述就是本文的全部内容了,希望能够对大家学习Laravel5框架有所帮助。

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 English version
Recommended: Win version, supports code prompts!

SublimeText3 Chinese version
Chinese version, very easy to use

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software