Introduction to Laravel 5 Framework (III) and laravel framework

Source: Internet
Author: User

Introduction to Laravel 5 Framework (III) and laravel framework

In this tutorial, we will use the out-of-the-box Auth system provided by Laravel 5 to verify the permissions on Our backend, build a front-end page, and display Pages.

1. Permission Verification

The background address is http: // localhost: 88/admin. All background operations will be performed on this page or its subpages. With the Auth provided by Laravel 5, we only need to modify a few Routing codes to implement the permission verification function.

First, change the routing group code:

Copy codeThe Code is as follows:
Route: group (['prefix' => 'admin', 'namespace '=> 'admin', 'middleware' => 'auth'], function ()
{
Route: get ('/', 'adminhomecomtroller @ Index ');
Route: resource ('pages ', 'pagescontroller ');
});

There is only one change in the Code above: Add a ''middleware => 'auth ''To the first parameter (an array) of 'route: group ''. Access http: // localhost: 88/admin now. The logon page is displayed. If no jump is made, do not panic. Exit from the upper right corner and enter again.

Our personal blog system does not want to be registered at will. Below we will change some routing code, only retaining the basic login and logout functions.

Delete:

Copy codeThe Code is as follows:
Route: controllers ([
'Auth' => 'auth \ AuthController ',
'Password' => 'auth \ passwordcontroller ',
]);

Added:

Copy codeThe Code is as follows:
Route: get ('auth/login', 'auth \ AuthController @ getlogin ');
Route: post ('auth/login', 'auth \ AuthController @ postlogin ');
Route: get ('auth/logout', 'auth \ AuthController @ getlogout ');

The background with the minimal function of permission verification has been completed. Currently, this background only manages Page resources. Next we will build a front-end page to display Pages.

2. Build the Home Page

First, sort out the routing code to display the top two lines of the route:

Copy codeThe Code is as follows:
Route: get ('/', 'welcomecontroller @ Index ');
Route: get ('home', 'homecontroller @ Index ');

Changed:

Copy codeThe Code is as follows:
Route: get ('/', 'homecontroller @ Index ');

We will directly use HomeController to support front-end page display.

You can delete the learnlaravel5/app/Http/Controllers/WelcomeController. php controller file and the learnlaravel5/resources/views/welcome. blade. php View File.

Modify 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()); }}

The Controller is constructed.

'View ('home')-> withPages (Page: all () 'implements the following functions:

Render the learnlaravel5/resources/views/home. blade. php View File
Pass the variable $ pages into the view, $ pages = Page: all ()
Page: all () calls the all () method in Eloquent and returns all data in the pages table.
Next, we will write the View File:

First, we will create a uniform shell for the front-end page, that is, the '

<!DOCTYPE html>

Modify the learnlaravel5/resources/views/home. blade. php file:

@extends('_layouts.default')@section('content') <div id="title" style="text-align: center;"> 

The first line '@ extends (' _ layouts. default') 'indicates that this page is a subview of learnlaravel5/resources/views/_ layouts/default. blade. php. In this case, Laravel's view rendering system first loads the parent view, and then places the content in the @ section ('content') in this view into the parent view @ yield ('content ').

Visit http: // localhost: 88/to obtain the following page:

2. Build a Page display Page

Add a route first. Add a line under the first line of the route file:

Copy codeThe Code is as follows:
Route: get ('pages/{id} ', 'pagescontroller @ show ');

Create the learnlaravel5/app/Http/Controllers/PagesController. php controller to display a single 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)); }}

Create the learnlaravel5/resources/views/pages/show. blade. php View File:

@ Extends ('_ layouts. default') @ section ('content') 

Finish all. Test results: Click the title of any article on the homepage to go to the article display page. You will see the following page:

Now, the front-end display page is complete, and tutorial 3 is complete.

The above is all the content of this article, hoping to help you learn the Laravel5 framework.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.