Laravel 4 Primary tutorials pages, forms validation, laravelpages_php tutorials

Source: Internet
Author: User
Tags autoload

Laravel 4 Primary tutorials pages, forms validation, laravelpages


1. Building pages Management features

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 succeeded! ');
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 your previous seed password and we'll get an error:

Class App\controllers\admin\pagescontroller does not exist

This file has already had Ah, why Laravel error said no?! The reason in the second tutorial, I said here directly. Because this class is not in the top-level namespace, we have not told Laravel that we have added a new class under a sub-namespace. Let's 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 bag, edvinaskrucas/notification, please install 3.0. Version 1 (4 is for Laravel 5), this is the third small job. Must be placed in the require inside, require-dev inside the bag only in the development of the use of the time.

The notification here is the better-used notification component.

After this package is ready, run:

Composer Dump-autoload

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

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

The right position many people do not understand, causing many people to make mistakes, the solution is very simple: please refer directly 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, the admin page of pages is complete!

2. Form Validation

Laravel provides native, very good form validation functionality, but sometimes validation rules need to be reused, so we'll use a powerful namespace to implement code reuse, while demonstrating the power of the modular functionality and module decoupling of PHP's namespaces beyond Laravel, Hmvc something has fallen behind.

Create a new app/services/validators level two folder and add it to Composer.json autoload > Classmap Last:

"App/services"

This is telling composer: to 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 be declared as belonging to the child namespace. This folder will host our forms validation class and, of course, can also host many other components and modules for complete decoupling.

After the add is complete, 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 ',
);
}

Then run:

Composer Dump-autoload

At this point, you can try all the actions on the entire page! New, edit, view, delete, now, Pages admin section all done!

Big job: The Pages Management section is now complete, but the articles management part is nothing, try to imitate the code of pages, complete a management system like pages. Tip: Include controller, view, and form validation oh. When you finish the Articles Management Section, Laravel is really getting started!


What is Laravel?

It frees you from the messy code of noodles, and it helps you build a perfect web app, and every line of code can be concise and expressive. 1, Bundle is Laravel expansion package organization form or salutation. Laravel's expansion pack warehouse is quite mature and can easily be used to install expansion packs (bundles) into your application. You can choose to download an expansion pack (bundle) and then copy it to the bundles directory, or install it automatically via the command line tool "Artisan". 2, in Laravel already has a set of advanced PHP ActiveRecord implementation-eloquent ORM. It makes it easy to apply "constraints" to both sides of the relationship, so you have full control over the data and enjoy all the conveniences of ActiveRecord. Eloquent native supports all methods of the query constructor (Query-builder) in fluent. 3. Application logic (application logic) can be implemented either in the controller (controllers) or directly into the route declaration, and the syntax is similar to the Sinatra framework. Laravel's design philosophy is to give developers maximum flexibility to create very small websites and build large enterprise applications. 4. Reverse Routing (Reverse Routing) gives you the ability to create a link (URI) by routing (routes) name. Simply use the route name, Laravel will automatically help you create the correct URI. This way you can change your routing (routes) at any time, and Laravel will automatically update all relevant links for you. 5. RESTful controller (restful Controllers) is an optional way to differentiate between get and post request logic. For example, in a user login logic, you declare a get_login () action to handle the service that gets the landing page, and also declare a post_login () action to verify the data that the form is post, and after verification, Make a decision to re-turn (redirect) to the landing page or to the console. 6. The auto-load class (class auto-loading) simplifies the loading of classes, so that it is not necessary to maintain automatic loading of configuration tables and non-essential component loading work. When you want to load any library or model, use it immediately, and Laravel will automatically load the required files for you. 7. View composers is essentially a piece of code, whichThe segment code is automatically executed when the view is loaded. The best example is the blog side of the random article recommendation, "View Assembler" contains the logic to load the recommended random article, so that you only need to load the content area of the Views (view) on the line, the other things laravel will help you to do it automatically. 8. The Reverse control container (IoC container) provides a convenient way to generate new objects, instantiate objects at any time, and Access Singleton (singleton) objects. Reverse control (IoC) means that you hardly need to intentionally load external libraries (libraries) to access them anywhere in your code, and you don't have to tolerate cumbersome, redundant code structures. 9. Migration (migrations) is like a version control tool, but it manages the database paradigm and is integrated directly into the laravel. You can use the "Artisan" command-line tool to generate and execute "migration" instructions. When your team member changes the database paradigm, you can easily update the current project through the version Control tool and then execute the "migration instructions," OK, your database is up to date! 11. The automatic paging (Automatic pagination) feature avoids the mixing of a large number of unrelated paging configuration codes in your business logic. Convenient is no need to remember the current page ... Remaining full text >>

What is Laravel?

It frees you from the messy code of noodles, and it helps you build a perfect web app, and every line of code can be concise and expressive. 1, Bundle is Laravel expansion package organization form or salutation. Laravel's expansion pack warehouse is quite mature and can easily be used to install expansion packs (bundles) into your application. You can choose to download an expansion pack (bundle) and then copy it to the bundles directory, or install it automatically via the command line tool "Artisan". 2, in Laravel already has a set of advanced PHP ActiveRecord implementation-eloquent ORM. It makes it easy to apply "constraints" to both sides of the relationship, so you have full control over the data and enjoy all the conveniences of ActiveRecord. Eloquent native supports all methods of the query constructor (Query-builder) in fluent. 3. Application logic (application logic) can be implemented either in the controller (controllers) or directly into the route declaration, and the syntax is similar to the Sinatra framework. Laravel's design philosophy is to give developers maximum flexibility to create very small websites and build large enterprise applications. 4. Reverse Routing (Reverse Routing) gives you the ability to create a link (URI) by routing (routes) name. Simply use the route name, Laravel will automatically help you create the correct URI. This way you can change your routing (routes) at any time, and Laravel will automatically update all relevant links for you. 5. RESTful controller (restful Controllers) is an optional way to differentiate between get and post request logic. For example, in a user login logic, you declare a get_login () action to handle the service that gets the landing page, and also declare a post_login () action to verify the data that the form is post, and after verification, Make a decision to re-turn (redirect) to the landing page or to the console. 6. The auto-load class (class auto-loading) simplifies the loading of classes, so that it is not necessary to maintain automatic loading of configuration tables and non-essential component loading work. When you want to load any library or model, use it immediately, and Laravel will automatically load the required files for you. 7. View composers is essentially a piece of code, whichThe segment code is automatically executed when the view is loaded. The best example is the blog side of the random article recommendation, "View Assembler" contains the logic to load the recommended random article, so that you only need to load the content area of the Views (view) on the line, the other things laravel will help you to do it automatically. 8. The Reverse control container (IoC container) provides a convenient way to generate new objects, instantiate objects at any time, and Access Singleton (singleton) objects. Reverse control (IoC) means that you hardly need to intentionally load external libraries (libraries) to access them anywhere in your code, and you don't have to tolerate cumbersome, redundant code structures. 9. Migration (migrations) is like a version control tool, but it manages the database paradigm and is integrated directly into the laravel. You can use the "Artisan" command-line tool to generate and execute "migration" instructions. When your team member changes the database paradigm, you can easily update the current project through the version Control tool and then execute the "migration instructions," OK, your database is up to date! 11. The automatic paging (Automatic pagination) feature avoids the mixing of a large number of unrelated paging configuration codes in your business logic. Convenient is no need to remember the current page ... Remaining full text >>

http://www.bkjia.com/PHPjc/903471.html www.bkjia.com true http://www.bkjia.com/PHPjc/903471.html techarticle Laravel 4 Primary tutorials pages, forms validation, laravelpages 1. Build pages management function Run command: PHP artisan generate:controller Admin/pagescontroller Modify Pagescontroller ....

  • Related Article

    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.