Pages and Form Verification _ php instances in Laravel4 basic tutorial

Source: Internet
Author: User
Tags autoload
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, open the http: // localhost: 8000/admin page and log on with the account and 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\NotificationServiceProvider'
'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

Laravel provides native form verification functions, but sometimes verification rules need to be reused. Therefore, we will use a powerful namespace to reuse code and demonstrate what is not included in Laravel, PHP namespaces bring about powerful componentization functions and module decoupling. HMVC is lagging behind.

Create an app/services/validators folder and add it to composer. json's autoload> classmap:

"app/services"

This is telling composer: to merge all my files and all the files in the subfolders into your namespace tree! In this way, the classes under the app/services can declare their own namespaces, and files in subfolders can also declare their own sub-namespaces. This folder will host our form verification group, and of course it can also host many other components and modules for full decoupling.

After adding, create the app/services/validators/Validator. php file:

<?php
namespace App\Services\Validators;
abstract class Validator {
protected $data;
public $errors;
public static $rules;
public function __construct($data = null)
{
$this->data = $data ?: \Input::all();
}
public function passes()
{
$validation = \Validator::make($this->data, static::$rules);
if ($validation->passes()) return true;
$this->errors = $validation->messages();
return false;
}
}

Create an app/services/validators/PageValidator. php file:

<?php
namespace App\Services\Validators;
class PageValidator extends Validator {
public static $rules = array(
'title' => 'required',
'body' => 'required',
);
}

Then run:

composer dump-autoload

At this time, you can try all the operations on the entire page! Create, edit, view, and delete pages. Now, pages management is complete!

Big job: at present, the Pages management part has been completed, but the Articles management part is still nothing. Try to imitate the Pages code to complete a management system like Pages. TIPS: includes controller, view, and form verification. After you have completed the Articles management, Laravel will get started!

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.