We will change our learning route and not build the login system like the Laravel 4 tutorial. In this tutorial, we will build the management features of the Pages together, trying to laravel the routing and the PHP namespace.
1. Routing
The routing in Laravel, like other PHP frameworks, is designed to divert various requests to each controller.
Add the following code at the end of ' learnlaravel5/app/http/routes.php ':
Copy Code code 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 of code ' Route::get ('/' the corresponding link is not http://fuck.io:88/but Http://fuck Io:88/admin, if the 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 ' Instead of ' \app\http\controllers\admin\adminhomecontroller@index ', add a prefix to the namespace.
If you've used Laravel 4, you'll find that Laravel 5 's namespace planning is weird, which is actually a big step forward. Laravel 4 has actually introduced the powerful feature of the namespace, but in order to "reduce learning costs", the routing, controller, model of the default namespace all set to the top-level namespace, this move makes many people more easily "get Started" laravel, But after a period of time, but also need to climb a wall, that is the namespace, and with the front of the "easy to use" as a cushion, the later study will be more difficult. Laravel 5 The namespace is all separated, the controller in ' \app\http\controllers ', the model in ' \app ', let us experience the sense of namespace separation in the beginning, in fact, will reduce the cost of learning.
2. Controller
We can use Artisan to build the controller very easily:
Copy Code code as follows:
PHP Artisan Make:controller Admin/adminhomecontroller
Get the ' learnlaravel5/app/http/controllers/admin/adminhomecontroller.php ' file.
Add a line to the ' class Adminhomecontroller extends Controller {':
Copy Code code as follows:
The code for modifying index () is as follows:
Copy Code code 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 the PHP namespace FAQ.
3. View
New ' learnlaravel5/resources/views/adminhome.blade.php ':
@extends (' app ') @section (' content ') <div class= "container" > <div class= "Row" > <div class= "col-md-10 Co L-md-offset-1 "> <div class=" Panel Panel-default "> <div class=" panel-heading "> Background home </div> & Lt;div class= "Panel-body" > <a href= "{{URL (' Admin/pages/create ')}}" class= "btn btn-lg btn-primary" > Add </a > @foreach ($pages as $page)
Basic usage of views no longer repeat 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 will record my implementation of the Pages management functions of the process, no longer do too much elaboration. We have a problem can be directly under this article message, I will promptly reply.
4.1 Modifying routing learnlaravel5/app/http/routes.php
Copy Code code 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 Creating learnlaravel5/app/http/controllers/admin/pagescontroller.php
Run:
Copy Code code as follows:
PHP Artisan Make:controller Admin/pagescontroller
4.3 Modify learnlaravel5/app/http/controllers/admin/pagescontroller.php for:
<?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 ' => ' req
uired|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, [' 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 () ' 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 Co
L-md-offset-1 "> <div class=" Panel Panel-default "> <div class=" panel-heading "> Add 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= "hid Den "name=" _token "value=" {{Csrf_token ()}} "> <input type=" text "name=" title "class=" Form-control "required=" Required "> <br> <textarea name=" Body "rows=" "class=" Form-control "required=" required "></t" Extarea> <br> <button class= "btn btn-lg btn-info" > Add page</button> </form> </div> </d
iv> </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 Co
L-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> <ul>
@foreach ($errors->all () as $error) <li>{{$error}}</li> @endforeach </ul> </div> @endif <form action= "{{URL (' admin/pages/'. $page->id)}}" method= "POST" > <i Nput name= "_method" type= "hidden" value= "put" > <input type= "hidden" name= "_token" value= "{{Csrf_token ()}}" &G
T <input type= "text" name= "title" class= "Form-control" required= "required" value= "{{$page->title}}" > <br > <TextArea name= "Body" rows= "ten" class= "Form-control" required= "required" >{{$page->body}}</textarea> ;br> <button class= "btn btn-lg btn-info" > Edit page</button> </form> </div>
;/div> </div> </div> </div> @endsection
4.5 View Results
Background 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 the form verification is added, and the pages management function is complete!
The above is the entire content of this article, I hope to be familiar with the LARAVEL5 framework can help.