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
Copy code The code is as follows:
php artisan make:controller Admin/AdminHomeController
Add a line above `class AdminHomeController extends Controller {`:
Copy code The code is as follows:
use AppPage;
Copy code The code is as follows:
public function index()
{
Return view('AdminHome')->withPages(Page::all());
}
The controller involves a lot of namespace knowledge, you can refer to PHP namespace to solve your doubts.
3. View
@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
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
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');
});
4.2 Create learnlaravel5/app/Http/Controllers/Admin/PagesController.php
Run:
Copy code The code is as follows:
php artisan make:controller Admin/PagesController
<?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'); } }
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
@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
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.