Using the Laravel 5 framework to build the management features of Pages

Source: Internet
Author: User
Tags button type

Using the Laravel 5 framework to build the management features of Pages

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 ':

Route::group ([' prefix ' = ' admin ', ' namespace ' = ' admin '], function ()

{

Route::get ('/', ' [email protected] ');

});

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 ' [email protected] ' is not in ' \app\http\controllers\[email protected] ' but in ' \app\http\con Trollers\admin\[email protected] ', 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:

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 {':

Use App\page;

The code to modify index () 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 ':

? 1234567891011121314151617181920212223242526272829303132333435363738 @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" > Backstage Home </div> & nbsp     <div class= "Panel-body" >       <a href= "{{URL (' Admin/pages/create ')}}" class= "btn BTN-LG btn-primary > New </a>         @foreach ($pages as $page)      

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

Route::group ([' prefix ' = ' admin ', ' namespace ' = ' admin '], function ()

{

Route::get ('/', ' [email protected] ');

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:

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 form for creating a new resource. &nbs p;*  * @return Response  */Public Function Create ()  { return view (' admin.pages.create ');  } &n Bsp  /**  * 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 {&NBSp Return Redirect::back ()->withinput ()->witherrors (' Save failed! ‘); &NBSP,}    }    /**  * Show the form for editing the specified resource.  *  * @param int $id  * @return Response  */Public Function edit ($id)  { return view (' Admi N.pages.edit ')->withpage (Page::find ($id));  }    /**  * Update The specified resource in storage.  *  * @param int $id  * @return Response  */Public Function Update (Request $request, $id)  {&nbs p; $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 ()-&G T;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 ();    return 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 ') <div class= "container" >  <div class= "Row" >   <div class= "col-md-10 col-md-offset-1" >    <div class= "Panel Panel-default" >     < Div class= "panel-heading" > New 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>     & nbsp  <ul>         @foreach ($errors->all () as $error)          < li>{{$error}}</li>         @endforeach       </ul>     &NBSP ; </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" > Add page</button>      </form>       </ div>    </div>   </div>  </div> </div> @endsection  

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

  @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" > Edit 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>     & nbsp  <ul>         @foreach ($errors->all () as $error)          < li>{{$error}}</li>         @endforeach       </ul>     &NBSP ; </div>       @endif       <form action= "{{URL (' admin/pages/'. $page->id)}}" met hod= "POST" >       &Lt;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" > Edit page</button>      </form> & nbsp     </div>    </div>   </div>  </div> </div> @endsection & nbsp

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!

Article Reference codego.net and Csdn.net


Using the Laravel 5 framework to build the management features of Pages

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.