Home > Backend Development > PHP Tutorial > Getting started with the Laravel 5 framework (2) Building the management function of Pages, laravelpages_PHP tutorial

Getting started with the Laravel 5 framework (2) Building the management function of Pages, laravelpages_PHP tutorial

WBOY
Release: 2016-07-13 09:57:33
Original
789 people have browsed it

Getting started with the Laravel 5 framework (2) Building the management function of Pages, laravelpages

We will change the learning route and no longer build the login system first like the Laravel 4 tutorial. In this tutorial, we'll build out the management capabilities of Pages, experimenting with Laravel's routing and PHP's namespaces.

1. Routing
Routing in Laravel, like other PHP frameworks, is used to distribute various requests to various controllers.

Add the following code at the end of `learnlaravel5/app/Http/routes.php`:

Copy code The code is as follows:
Route::group(['prefix' => 'admin', 'namespace' => 'Admin'], function()
{
Route::get('/', 'AdminHomeController@index');
});

This indicates that a routing group has been created.

1. `'prefix' => 'admin'` means that the url prefix of this routing group is /admin, which means that the link corresponding to the middle line of code `Route::get('/'` is not http: //fuck.io:88/ but http://fuck.io:88/admin. If this code is `Route::get('fuck'`, then the URL should be http://fuck. io:88/admin/fuck .

2. `'namespace' => 'Admin'` means that the following `AdminHomeController@index` is not in `AppHttpControllersAdminHomeController@index` but in `AppHttpControllersAdminAdminHomeController@index`, plus a namespace prefix.

If you have used Laravel 4, you will find that the namespace planning of Laravel 5 is weird, which is actually a very big improvement. Laravel 4 has actually fully introduced the powerful feature of namespaces. However, in order to "reduce learning costs", the default namespaces of routes, controllers, and models are all set to top-level namespaces. This move makes it easier for many people. I "got started" with Laravel, but after using it for a while, I still need to climb a high wall, which is the namespace. And with the previous impression of "easy to get started" as a foreshadowing, later learning will be more difficult. Laravel 5 separates all namespaces. The controller is in `AppHttpControllers` and the model is in `App`. This allows us to experience the feeling of namespace separation when we first get started. Overall, it will actually reduce the learning cost.

2. Controller

We can build controllers very easily using Artisan:

Copy code The code is as follows: php artisan make:controller Admin/AdminHomeController

Get the `learnlaravel5/app/Http/Controllers/Admin/AdminHomeController.php` file.

Add a line above `class AdminHomeController extends Controller {`:

Copy code The code is as follows: use AppPage;

Modify the code of index() as follows:

Copy code The code is as follows: public function index()
{
Return view('AdminHome')->withPages(Page::all());
}

Controller Chinese documentation: http://laravel-china.org/docs/5.0/controllers

The controller involves a lot of namespace knowledge, you can refer to PHP namespace to solve your doubts.

3. View

New `learnlaravel5/resources/views/AdminHome.blade.php`:

@extends('app')

@section('content')
<div class="container">
 <div class="row">
  <div class="col-md-10 col-md-offset-1">
   <div class="panel panel-default">
    <div class="panel-heading">后台首页</div>

    <div class="panel-body">

    <a href="{{ URL('admin/pages/create') }}" class="btn btn-lg btn-primary">新增</a>

     @foreach ($pages as $page)
      <hr>
      <div class="page">
       <h4>{{ $page->title }}</h4>
       <div class="content">
        <p>
         {{ $page->body }}
        </p>
       </div>
      </div>
      <a href="{{ URL('admin/pages/'.$page->id.'/edit') }}" class="btn btn-success">编辑</a>

      <form action="{{ URL('admin/pages/'.$page->id) }}" method="POST" style="display: inline;">
       <input name="_method" type="hidden" value="DELETE">
       <input type="hidden" name="_token" value="{{ csrf_token() }}">
       <button type="submit" class="btn btn-danger">删除</button>
      </form>
     @endforeach

    </div>
   </div>
  </div>
 </div>
</div>
@endsection

Copy after login
The basic usage of views will not be described here. Please read the Chinese documentation: http://laravel-china.org/docs/5.0/views

Visit http://fuck.io:88/admin and get the following page:

At this point, the entire process including Routing >> Controller >> Model >> View has been completed.

4. Complete Pages management function

Next, I will record my process of implementing the Pages management function without going into too much elaboration. If you have any questions, you can leave a message directly below this article and I will reply in time.

4.1 Modify routing learnlaravel5/app/Http/routes.php

Copy code The code is as follows: Route::group(['prefix' => 'admin', 'namespace' => 'Admin'], function()
{
Route::get('/', 'AdminHomeController@index');
Route::resource('pages', 'PagesController');
});

A "resource controller" has been added here, Chinese document address: http://laravel-china.org/docs/5.0/controllers#restful-resource-controllers

4.2 Create learnlaravel5/app/Http/Controllers/Admin/PagesController.php

Run:

Copy code The code is as follows: php artisan make:controller Admin/PagesController

4.3 Modify learnlaravel5/app/Http/Controllers/Admin/PagesController.php to:

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

use App\Http\Requests;
use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

use App\Page;

use Redirect, Input, Auth;

class PagesController extends Controller {

 /**
 * Show the form for creating a new resource.
 *
 * @return Response
 */
 public function create()
 {
 return view('admin.pages.create');
 }

 /**
 * Store a newly created resource in storage.
 *
 * @return Response
 */
 public function store(Request $request)
 {
 $this->validate($request, [
  'title' => 'required|unique:pages|max:255',
  'body' => 'required',
 ]);

 $page = new Page;
 $page->title = Input::get('title');
 $page->body = Input::get('body');
 $page->user_id = 1;//Auth::user()->id;

 if ($page->save()) {
  return Redirect::to('admin');
 } else {
  return Redirect::back()->withInput()->withErrors('保存失败!');
 }

 }

 /**
 * Show the form for editing the specified resource.
 *
 * @param int $id
 * @return Response
 */
 public function edit($id)
 {
 return view('admin.pages.edit')->withPage(Page::find($id));
 }

 /**
 * Update the specified resource in storage.
 *
 * @param int $id
 * @return Response
 */
 public function update(Request $request,$id)
 {
 $this->validate($request, [
  'title' => 'required|unique:pages,title,'.$id.'|max:255',
  'body' => 'required',
 ]);

 $page = Page::find($id);
 $page->title = Input::get('title');
 $page->body = Input::get('body');
 $page->user_id = 1;//Auth::user()->id;

 if ($page->save()) {
  return Redirect::to('admin');
 } else {
  return Redirect::back()->withInput()->withErrors('保存失败!');
 }
 }

 /**
 * Remove the specified resource from storage.
 *
 * @param int $id
 * @return Response
 */
 public function destroy($id)
 {
 $page = Page::find($id);
 $page->delete();

 return Redirect::to('admin');
 }

}

Copy after login
4.4 Create view file

First create the admin/pages two-level folder under learnlaravel5/resources/views.

Then create learnlaravel5/resources/views/admin/pages/create.blade.php:

@extends('app')

@section('content')
<div class="container">
 <div class="row">
  <div class="col-md-10 col-md-offset-1">
   <div class="panel panel-default">
    <div class="panel-heading">新增 Page</div>

    <div class="panel-body">

     @if (count($errors) > 0)
      <div class="alert alert-danger">
       <strong>Whoops!</strong> There were some problems with your input.<br><br>
       <ul>
        @foreach ($errors->all() as $error)
         <li>{{ $error }}</li>
        @endforeach
       </ul>
      </div>
     @endif

     <form action="{{ URL('admin/pages') }}" method="POST">
      <input type="hidden" name="_token" value="{{ csrf_token() }}">
      <input type="text" name="title" class="form-control" required="required">
      <br>
      <textarea name="body" rows="10" class="form-control" required="required"></textarea>
      <br>
      <button class="btn btn-lg btn-info">新增 Page</button>
     </form>

    </div>
   </div>
  </div>
 </div>
</div>
@endsection

Copy after login
Create learnlaravel5/resources/views/admin/pages/edit.blade.php afterwards:

@extends('app')

@section('content')
<div class="container">
 <div class="row">
  <div class="col-md-10 col-md-offset-1">
   <div class="panel panel-default">
    <div class="panel-heading">编辑 Page</div>

    <div class="panel-body">

     @if (count($errors) > 0)
      <div class="alert alert-danger">
       <strong>Whoops!</strong> There were some problems with your input.<br><br>
       <ul>
        @foreach ($errors->all() as $error)
         <li>{{ $error }}</li>
        @endforeach
       </ul>
      </div>
     @endif

     <form action="{{ URL('admin/pages/'.$page->id) }}" method="POST">
      <input name="_method" type="hidden" value="PUT">
      <input type="hidden" name="_token" value="{{ csrf_token() }}">
      <input type="text" name="title" class="form-control" required="required" value="{{ $page->title }}">
      <br>
      <textarea name="body" rows="10" class="form-control" required="required">{{ $page->body }}</textarea>
      <br>
      <button class="btn btn-lg btn-info">编辑 Page</button>
     </form>

    </div>
   </div>
  </div>
 </div>
</div>
@endsection

Copy after login
4.5 View results

Backstage home page http://fuck.io:88/admin:

New Page http://fuck.io:88/admin/pages/create:

Edit Page http://fuck.io:88/admin/pages/1/edit:

The functions of adding, editing, and deleting on the page have been completed, and form verification has been added, and the Pages management function is completed!

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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/981348.htmlTechArticleGetting started with Laravel 5 framework (2) Building the management function of Pages, laravelpages We will change the learning route and no longer be like Laravel 4. Build the login system first as in the tutorial. In this tutorial...
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