Introduction to Laravel 5 Framework (ii) building the management functions of Pages _php

Source: Internet
Author: User
Keywords PHP Framework Laravel laravel Getting Started tutorial
We will change our learning route and no longer build a login system like the Laravel 4 tutorial. In this tutorial, we will build the management capabilities of Pages together, trying to Laravel the routing and PHP namespaces.

1. Routing
The route in Laravel, like other PHP frameworks, is to offload various requests to each controller.

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 means that a routing group has been created.

1. ' prefix ' + ' admin ' indicates that the URL prefix for this routing group is/admin, which means that the middle line code ' Route::get ('/' corresponds to a link that 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 the following ' Adminhomecontroller@index ' is not in ' \app\http\controllers\adminhomecontroller@index ' ' But in ' \app\http\controllers\admin\adminhomecontroller@index ', plus a namespace prefix.

If you have used Laravel 4, you will find that Laravel 5 's namespace planning is more bizarre, which is actually a very big improvement. Laravel 4 has fully introduced the namespace of this powerful feature, but in order to "reduce learning costs", the routing, controller, model of the default namespace all set to a top-level namespace, this move instead makes many people relatively easy to "get Started" Laravel, But after a period of time, but also need to climb a high wall, that is the name space, and with the front of the "easy-to-use" impression as a cushion, later learning will be more difficult. Laravel 5 to separate the namespace, the controller in the ' \app\http\controllers ', the model in ' \app ', let us at the beginning of the time to experience the separation of the sense of the name space, in general, will actually reduce the cost of learning.

2. Controller

We can use Artisan to build controllers very easily:

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 the ' class Adminhomecontroller extends Controller {':

The code is as follows:


Use App\page;

The code to modify 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, and you can refer to the PHP namespace for doubts.

3. View

New ' learnlaravel5/resources/views/adminhome.blade.php ':

@extends (' app ') @section (' content ')          background homepage        added     @foreach ($pages as $page)                   

{{$page->title}}

{{$page->body}}

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

The basic usage of the view is not mentioned here, please read the Chinese document: Http://laravel-china.org/docs/5.0/views

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

At this point, the entire process that contains the routing controller Model view has been completed.

4. Complete the Pages management function

Next, I'll take a note of the process of implementing the Pages management feature, no more elaboration. We have a problem can be directly under the text message, I will promptly reply.

4.1 Modifying 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" has been added here, Chinese document address: Http://laravel-china.org/docs/5.0/controllers#restful-resource-controllers

4.2 Creating 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 to:

<?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 F ORM for creating a new resource. * * @return Response */Public Function Create () {return view (' Admin.pages.create ');}/** * Store a newly created resour CE 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 (' Save failed! '); }}/** * 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, [' t Itle ' = ' 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 (' Save failed! '); }}/** * Remove the specified resource from storage. * * @param int $id * @return Response */Public Function destroy ($id) {$page = Page::find ($id); $page->delete (); retur N redirect::to (' admin '); }}

4.4 Creating a View File

First create the Admin/pages level two folder under Learnlaravel5/resources/views.

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

@extends (' app ') @section (' content ')          added Page         @if (Count ($errors) > 0)             whoops! There were some problems with your input.

    @foreach ($errors->all () as $error)
  • {{$error}}
  • @endforeach
@endif @endsection

Then create the 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 Viewing 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 features on the page have been completed, and forms validation has been added, and the Pages management feature is complete!

The above is the whole content of this article, I hope that we are familiar with the LARAVEL5 framework can be helpful.

  • 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.