Table of Contents
Learn Laravel 5
{{ $page->title }}
Home Backend Development PHP Tutorial Laravel 5 框架入门(三)_php实例

Laravel 5 框架入门(三)_php实例

May 16, 2016 pm 08:17 PM
php framework laravel

本篇教程中,我们将利用 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. 构建首页

先整理路由代码,将路由的最上面的两行:

复制代码 代码如下:

Route::get('/', 'WelcomeController@index');
Route::get('home', 'HomeController@index');

改成:

复制代码 代码如下:

Route::get('/', 'HomeController@index');

我们将直接使用 HomeController 来支撑我们的前台页面展示。

此时可以删除 learnlaravel5/app/Http/Controllers/WelcomeController.php 控制器文件和 learnlaravel5/resources/views/welcome.blade.php 视图文件。

修改 learnlaravel5/app/Http/Controllers/HomeController.php 为:

<&#63;php namespace App\Http\Controllers;

use App\Page;

class HomeController extends Controller {

 public function index()
 {
 return view('home')->withPages(Page::all());
 }

}

Copy after login

控制器构造完成。

`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&#63;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;">
   &copy;2015 <a href="http://lvwenhan.com">JohnLui</a>
  </div>
 </div>


</body>
</html>

Copy after login

修改 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

Copy after login

第一行 `@extends('_layouts.default')` 代表这个页面是 learnlaravel5/resources/views/_layouts/default.blade.php 的子视图。此时 Laravel 的 视图渲染系统会首先载入父视图,再将此视图中的 @section('content') 里面的内容放入到父视图中的 @yield('content') 处进行渲染。

访问 http://localhost:88/ ,可以得到如下页面:

2. 构建 Page 展示页

首先增加路由。在路由文件的第一行下面增加一行:

复制代码 代码如下:

Route::get('pages/{id}', 'PagesController@show');

新建控制器 learnlaravel5/app/Http/Controllers/PagesController.php,负责单个 page 的展示:

<&#63;php namespace App\Http\Controllers;

use App\Page;

class PagesController extends Controller {

 public function show($id)
 {
  return view('pages.show')->withPage(Page::find($id));
 }

}

Copy after login

新建视图 learnlaravel5/resources/views/pages/show.blade.php 文件:

@extends('_layouts.default')

@section('content')
 <h4>
  <a href="/">&#11013;&#65039;返回首页</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

Copy after login

全部完成,检验成果:点击首页之中任意一篇文章的标题,进入文章展示页,你会看到以下页面:

至此,前台展示页面全部完成,教程三结束。

以上所述就是本文的全部内容了,希望能够对大家学习Laravel5框架有所帮助。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1252
24
Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Apr 17, 2025 am 12:06 AM

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values ​​to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

How does PHP type hinting work, including scalar types, return types, union types, and nullable types? How does PHP type hinting work, including scalar types, return types, union types, and nullable types? Apr 17, 2025 am 12:25 AM

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.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

See all articles