Laravel 4 Introductory tutorial pages, form validation _php instances

Source: Internet
Author: User
Tags autoload

1. Building the Pages management function

To run the 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 (' pages ', 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 (' new page success! ');
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 (' Update page success! ');
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 (' Delete succeeded! ');
Return Redirect::route (' Admin.pages.index ');
}
}

Then, open the Http://localhost:8000/admin page and log in with the account password entered in the previous seed, and we'll get an error:

Class App\controllers\admin\pagescontroller does not exist

This document clearly already has Ah, why laravel error say no?! The reason is in the second tutorial, I just say it here. Because this class is not under the top-level namespace, we have not told Laravel that we have added a new class under the child namespace. We're going to tell it now:

Composer Dump-autoload

OK, refresh, and we'll get the following error:

View [Admin.pages.index] not found.

At this point, copy the entire pages folder in my view to the past.

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 the 3.0.1 Version (4 is for Laravel 5), this is the third small job. Be sure to put it inside the require, Require-dev inside the bag is only used when developing.

The notification here is the better notification component.

After this package is ready, run:

Composer Dump-autoload

Then add the following two lines to the appropriate location in the config/app.php:

' Krucas\notification\notificationserviceprovider '
' Notification ' => ' krucas\notification\facades\notification '

The right place many people do not understand, causing many people to be wrong, the solution is also very simple: Please refer to my sample code directly: https://github.com/johnlui/Learn-Laravel-4/blob/master/app/config/app.php

Refresh if you see the following interface:


Congratulations to you ~ pages of the management page completed!

2. Form Validation

Laravel provides native, very handy form validation, but sometimes validation rules need to be reused, so we'll use a powerful namespace to implement code reuse, and will showcase the powerful component functions and module decoupling of PHP's namespaces outside of Laravel, Hmvc something has fallen behind.

Create a new app/services/validators level two folder and add it to the AutoLoad > classmap at Composer.json:

"App/services"

This is telling composer: Come and merge all of the files and subfolders below me into your namespace tree! This allows the class under App/services to declare its own namespace, and the files in the subfolder can also declare themselves to belong to the child namespace. This folder will host our form validation classes and, of course, can host many other components and modules to achieve complete decoupling.

When you have finished adding, create a new 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;
}
}

New app/services/validators/pagevalidator.php File:

<?php
namespace App\services\validators;
Class Pagevalidator extends Validator {
public static $rules = Array (
' title ' => ' Required ',
' Body ' => ' required ',
);
}

And then run:

Composer Dump-autoload

At this point, you can try all the actions on the entire page! New, edit, view, delete, to this end, the Pages Management section is complete!

Big job: At present the pages management has all been completed, but the articles management part or nothing, try to imitate the pages of the code, complete with the same management system as the pages. Tip: Include controller, view, and form validation oh. When you finish the Articles Management Section, Laravel is really getting 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.