Laravel5 framework entry (2) build Pages management function, laravelpages_PHP tutorial

Source: Internet
Author: User
Laravel5 framework (2) build the management function of Pages, laravelpages. Laravel5 framework entry (2) build the Pages management function. laravelpages will change the learning path, instead of building a login system as in the Laravel4 tutorial. In this tutorial, Laravel 5 framework entry (2) builds the Pages management function, laravelpages

We will change our Learning Path, instead of building a login system as we did in Laravel 4. In this tutorial, we will build the Pages management function and try Laravel routing and PHP namespace.

1. routing
Like other PHP frameworks, Laravel routes distribute requests to various controllers.

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

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

This indicates that a route group is created.

1. ''prefix' => 'admin'' indicates that the url prefix of the Route Group is/admin, that is, the line of code in the middle is 'Route :: get ('/''corresponding to the link is not http://fuck.io: 88/but http://fuck.io: 88/admin, if this code is 'Route: get ('fuck, the URL should be http://fuck.io: 88/admin/fuck.

2. ''namespace '=> 'admin'' indicates that the following 'adminhomecontroller @ index' is not in' \ App \ Http \ Controllers \ AdminHomeController @ Index', but in '\ App \ Http \ controllers \ Admin \ AdminHomeController @ Index ', prefix of a namespace.

If you have used Laravel 4, you will find that the namespace planning of Laravel 5 is odd, which is actually a great improvement. Laravel 4 has actually fully introduced the powerful namespace feature, but to "reduce learning costs", it sets all the default namespaces of routes, controllers, and models as top-level namespaces, this behavior made Laravel easy for many people to get started. However, after a while, we still need to move over a wall, that is, namespace, in addition, with the previous "easy to use" impression as a foreshadowing, later learning will be more difficult. Laravel 5 separates all namespaces. the controller is in '\ App \ Http \ controllers', and the model is in' \ app ', this allows us to experience namespace separation when we get started. In general, it will actually reduce the learning cost.

2. Controller

We can use Artisan to easily build controllers:

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

Obtain the 'learnlaravel5/app/Http/Controllers/Admin/AdminHomeController. php' file.

Add a line above 'class AdminHomeController extends Controller:

The code is as follows:
Use App \ Page;

The code for modifying index () is as follows:

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

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

The controller involves a lot of namespace knowledge. you can refer to PHP namespace to learn more.

3. View

Create 'learnlaravel5/resources/views/AdminHome. blade. php ':

@ Extends ('app') @ section ('content ')

Background homepage

Add @ foreach ($ pages as $ page)

{{$ Page-> title }}

{$ Page-> body }}

Id. '/edit')} "class =" btn-success "> edit @ endforeach

@ Endsection

The basic usage of the view will not be repeated here, please read Chinese documents: http://laravel-china.org/docs/5.0/views

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

So far, the entire process including the routing, controller, model, and view has been completed.

4. complete Pages management

Next, I will record the process of implementing the Pages management function and will not elaborate too much. If you have any questions, please leave a message below this article and I will reply in time.

4.1 modify the route learnlaravel5/app/Http/routes. php

The code is as follows:
Route: group (['prefix' => 'admin', 'namespace '=> 'admin'], function ()
{
Route: get ('/', 'adminhomecontroller @ index ');
Route: resource ('Pages ', 'pagescontroller ');
});

A resource controller is 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:

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

4.3 modify learnlaravel5/app/Http/Controllers/Admin/PagesController. php:

<? 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. ** @ r Eturn Response */public function store (Request $ request) {$ this-> validate ($ request, ['title' => 'required | unique: pages | max: 255 ', 'Body' => 'requestred',]); $ 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 ('failed to save! ');}/*** 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 ('failed to save! ');}/*** 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 ');}}

4.4 create a view file

First, create admin/pages folders under learnlaravel5/resources/views.

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

@ Extends ('app') @ section ('content ')

Add Page

@ If (count ($ errors)> 0)

Whoops!There were some problems with your input.

    @ Foreach ($ errors-> all () as $ error)
  • {$ Error }}
  • @ Endforeach

@ Endif

@ Endsection

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

@ Extends ('app') @ section ('content ')

Edit Page

@ If (count ($ errors)> 0)

Whoops!There were some problems with your input.

    @ Foreach ($ errors-> all () as $ error)
  • {$ Error }}
  • @ Endforeach

@ Endif

@ Endsection

4.5 View Results

Back home http://fuck.io: 88/admin:

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

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

The new, edited, and deleted functions on the page have been completed, and form verification is added. The Pages management function is complete!

The above is all the content of this article. I hope you will be familiar with the Laravel5 framework.

Http://www.bkjia.com/PHPjc/981348.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/981348.htmlTechArticleLaravel 5 framework getting started (II) building management of Pages, laravelpages we'll change the learning path and no longer build a login system first like Laravel 4 tutorial. In this tutorial ,...

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.