This tutorial is the end of this series. What we need to do is the detailed management function of Pages, Form Verification and deep use of namespaces, and a better notification component, and a simple big job is arranged.
This tutorial is the end of this series. What we need to do is the detailed management function of Pages, Form Verification and deep use of namespaces, and a better notification component, and a simple big job is arranged.
1. Build Pages management functions
Run the following command:
Php artisan generate: controller admin/PagesController
Modify PagesController. php content:
<? Php
Namespace App \ Controllers \ Admin;
Use Page;
Use Input, Notification, Redirect, Sentry, Str;
Use App \ Services \ Validators \ PageValidator;
Class PagesController extends \ BaseController {
Public function index ()
{
Return \ View: make ('admin. pages. Index')-> with ('page', Page: all ());
}
Public function show ($ id)
{
Return \ View: make ('admin. pages. show ')-> with ('page', page: find ($ id)-> withAuthor (Sentry: findUserById (Page: find ($ id) -> user_id)-> name );
}
Public function create ()
{
Return \ View: make ('admin. pages. create ');
}
Public function store ()
{
$ Validation = new PageValidator;
If ($ validation-> passes ())
{
$ Page = new Page;
$ Page-> title = Input: get ('title ');
$ Page-> body = Input: get ('body ');
$ Page-> user_id = Sentry: getUser ()-> id;
$ Page-> save ();
Notification: success ('page added successfully! ');
Return Redirect: route ('admin. pages. edit', $ page-> id );
}
Return Redirect: back ()-> withInput ()-> withErrors ($ validation-> errors );
}
Public function edit ($ id)
{
Return \ View: make ('admin. pages. edit')-> with ('page', page: find ($ id ));
}
Public function update ($ id)
{
$ Validation = new PageValidator;
If ($ validation-> passes ())
{
$ Page = Page: find ($ id );
$ Page-> title = Input: get ('title ');
$ Page-> body = Input: get ('body ');
$ Page-> user_id = Sentry: getUser ()-> id;
$ Page-> save ();
Notification: success ('page updated successfully! ');
Return Redirect: route ('admin. pages. edit', $ page-> id );
}
Return Redirect: back ()-> withInput ()-> withErrors ($ validation-> errors );
}
Public function destroy ($ id)
{
$ Page = Page: find ($ id );
$ Page-> delete ();
Notification: success ('deleted successfully! ');
Return Redirect: route ('admin. pages. Index ');
}
}
Then, go to the: 8000/admin page and log on with the account password entered in the previous seed. We will get an error:
Class App \ Controllers \ Admin \ PagesController does not exist
This file already exists. Why does Laravel report an error ?! The reason is that in the second tutorial, I will talk about it here. Because this class is not in the top-level namespace, we didn't tell Laravel that we have added a new class in the sub-namespace. Let's tell it now:
Composer dump-autoload
OK, refresh, and we will get the following error:
View [admin. pages. index] not found.
Copy the entire pages folder in my view.
Refresh. You will get the following error:
Class 'notification' not found
This is because we have not installed this composer package, edvinaskrucas/notification. Please install version 3.0.1 On Your Own (4 is prepared for Laravel 5). This is the third small job. It must be placed in require. The package in require-dev is only used during development.
The Notification here is the better Notification component.
After packaging, run:
Composer dump-autoload
Add the following two lines in the appropriate position in config/app. php:
'Krucas \ Notification \ icationicationserviceprovider'
'Notification' => 'krucas \ Notification \ Facades \ notification'
The proper location many people do not understand, resulting in many people errors, the solution is also very simple: Please refer to my sample code: https://github.com/johnlui/Learn-Laravel-4/blob/master/app/config/app.php
Refresh. If you see the following interface:
Congratulations ~ Pages Management page complete!
2. Form Verification