Home > Backend Development > PHP Tutorial > Getting Started with Laravel 5 Framework (3), Getting Started with Laravel Framework_PHP Tutorial

Getting Started with Laravel 5 Framework (3), Getting Started with Laravel Framework_PHP Tutorial

WBOY
Release: 2016-07-13 09:57:35
Original
843 people have browsed it

Getting started with the Laravel 5 framework (3), getting started with the laravel framework

In this tutorial, we will use the out-of-the-box Auth system that comes with Laravel 5 to run our backend Perform permission verification and build a front-end page to display Pages.

1. Permission verification

The backend address is http://localhost:88/admin, and all our backend operations will be performed under this page or its subpages. Using the Auth provided by Laravel 5, we only need to change a small part of the routing code to implement the permission verification function.

First, change the code of the routing group to:

Copy code The 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 above code: adding `'middleware' => 'auth'` to the first parameter (an array) of `Route::group()`. Now visit http://localhost:88/admin and you should be redirected to the login page. If there is no jump, don't panic, just exit from the upper right corner and re-enter.

Our personal blog system does not allow people to register casually. Below we will change part of the routing code and only retain the basic login and logout functions.

Delete:

Copy code The code is as follows:
Route::controllers([
'auth' => 'AuthAuthController',
'password' => 'AuthPasswordController',
]);

Added:

Copy code The code is as follows:
Route::get('auth/login', 'AuthAuthController@getLogin');
Route::post('auth/login', 'AuthAuthController@postLogin');
Route::get('auth/logout', 'AuthAuthController@getLogout');

The backend with the minimization function of permission verification has been completed. This backend currently only manages the Page resource. Next we will build the front page and display Pages.

2. Build the homepage

First organize the routing code and change the top two lines of the routing:

Copy code The code is as follows:
Route::get('/', 'WelcomeController@index');
Route::get('home', 'HomeController@index');

Change to:

Copy code The code is as follows:
Route::get('/', 'HomeController@index');

We will use HomeController directly to support our front page display.

You can delete the learnlaravel5/app/Http/Controllers/WelcomeController.php controller file and learnlaravel5/resources/views/welcome.blade.php view file at this time.

Modify learnlaravel5/app/Http/Controllers/HomeController.php to:

<&#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

The controller construction is completed.

`view('home')->withPages(Page::all())` This sentence implements the following functions:

Render 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 the data in the pages table.
Next we start writing the view file:

First, we will create a unified shell of the front-end page, namely the `` part and the `#footer` part. Create a new learnlaravel5/resources/views/_layouts/default.blade.php file (please create the folder yourself):

<!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

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

@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

Copy after login

The first line `@extends('_layouts.default')` means that this page is a subview of learnlaravel5/resources/views/_layouts/default.blade.php. At this time, Laravel's view rendering system will first load the parent view, and then put the content in @section('content') in this view into @yield('content') in the parent view for rendering.

Visit http://localhost:88/ and you will get the following page:

2. Build Page display page

First add routing. Add a line below the first line of the routing file:

Copy code The code is as follows:
Route::get('pages/{id}', 'PagesController@show');

Create a new controller learnlaravel5/app/Http/Controllers/PagesController.php, responsible for the display of a single 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

New view learnlaravel5/resources/views/pages/show.blade.php file:

@extends('_layouts.default')

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

Copy after login

All completed, check the results: click on the title of any article on the homepage to enter the article display page, you will see the following page:

At this point, the front-end display page is completed, and tutorial three is over.

The above is the entire content of this article. I hope it will be helpful to everyone learning the Laravel5 framework.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/981347.htmlTechArticleGetting Started with Laravel 5 Framework (3), Getting Started with Laravel Framework In this tutorial, we will use Laravel 5’s own The out-of-the-box Auth system performs permission verification on our backend and builds...
Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template