Laravel 4: Pages, form verification, laravelpages
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!
What is Laravel?
It can free you from the messy code like a noodle; it can help you build a perfect web APP, and each line of code can be concise and expressive. 1. Bundle is the extended package organization form or name of Laravel. Laravel's extension package repository is quite mature and can easily help you install the extension package (bundle) into your application. You can download an extension package (bundle) and copy it to the bundles directory, or use the command line tool "Artisan" for automatic installation. 2. Laravel already has an advanced PHP ActiveRecord implementation-Eloquent ORM. It can easily apply constraints to the two sides of the relationship, so that you have full control over the data and enjoy all the conveniences of ActiveRecord. Eloquent native supports all query-builder methods in Fluent. 3. Application Logic can be implemented in the Controller or directly integrated into the route declaration. The syntax is similar to that of the Sinatra framework. Laravel's design philosophy is to provide developers with the maximum flexibility, both creating very small websites and building large-scale enterprise applications. 4. Reverse Routing gives you the ability to create a link (URI) using a routes name. You only need to use the route name. Laravel will automatically create the correct URI for you. In this way, you can change your routes at any time. Laravel will automatically update all related links. 5. Restful controller (Restful Controllers) is an optional method to differentiate the GET and POST request logic. For example, in a user login logic, you declare a get_login () action to process the service that obtains the login page, and also declare a post_login () action (action) to verify the data in the form POST, and after the verification, make the decision to redirect (redirect) to the login page or to the console. 6. The automatic loading Class simplifies the loading of the class. In the future, you do not need to maintain the automatic loading of configuration tables and non-essential components. When you want to load any library or model, use it immediately. Laravel will automatically help you load the required files. 7. View Composers is essentially a piece of code, which is automatically executed when the View is loaded. The best example is the recommendation of side random articles in the blog. The "view assembler" contains the logic for loading the recommendation of random articles. In this way, you only need to load the view of the content area) that's it. Laravel will automatically complete other tasks. 8. IoC container provides a convenient way to generate new objects, instantiate objects at any time, and access singleton objects. IoC means that you can access these objects from any location in the code without having to specifically Load External libraries, it does not need to endure complicated and redundant code structures. 9. Migration is like version control. However, it manages the database paradigm and is directly integrated into Laravel. You can use the "Artisan" command line tool to generate and execute the "Migration" command. When your team members change the database paradigm, you can easily update the current project using a version control tool, and then execute the "Migration command, your database is up to date! 11. The Automatic Pagination function prevents a large number of irrelevant page configuration code from being incorporated into your business logic. It is convenient that you do not need to remember the current page... the remaining full text>
What is Laravel?
It can free you from the messy code like a noodle; it can help you build a perfect web APP, and each line of code can be concise and expressive. 1. Bundle is the extended package organization form or name of Laravel. Laravel's extension package repository is quite mature and can easily help you install the extension package (bundle) into your application. You can download an extension package (bundle) and copy it to the bundles directory, or use the command line tool "Artisan" for automatic installation. 2. Laravel already has an advanced PHP ActiveRecord implementation-Eloquent ORM. It can easily apply constraints to the two sides of the relationship, so that you have full control over the data and enjoy all the conveniences of ActiveRecord. Eloquent native supports all query-builder methods in Fluent. 3. Application Logic can be implemented in the Controller or directly integrated into the route declaration. The syntax is similar to that of the Sinatra framework. Laravel's design philosophy is to provide developers with the maximum flexibility, both creating very small websites and building large-scale enterprise applications. 4. Reverse Routing gives you the ability to create a link (URI) using a routes name. You only need to use the route name. Laravel will automatically create the correct URI for you. In this way, you can change your routes at any time. Laravel will automatically update all related links. 5. Restful controller (Restful Controllers) is an optional method to differentiate the GET and POST request logic. For example, in a user login logic, you declare a get_login () action to process the service that obtains the login page, and also declare a post_login () action (action) to verify the data in the form POST, and after the verification, make the decision to redirect (redirect) to the login page or to the console. 6. The automatic loading Class simplifies the loading of the class. In the future, you do not need to maintain the automatic loading of configuration tables and non-essential components. When you want to load any library or model, use it immediately. Laravel will automatically help you load the required files. 7. View Composers is essentially a piece of code, which is automatically executed when the View is loaded. The best example is the recommendation of side random articles in the blog. The "view assembler" contains the logic for loading the recommendation of random articles. In this way, you only need to load the view of the content area) that's it. Laravel will automatically complete other tasks. 8. IoC container provides a convenient way to generate new objects, instantiate objects at any time, and access singleton objects. IoC means that you can access these objects from any location in the code without having to specifically Load External libraries, it does not need to endure complicated and redundant code structures. 9. Migration is like version control. However, it manages the database paradigm and is directly integrated into Laravel. You can use the "Artisan" command line tool to generate and execute the "Migration" command. When your team members change the database paradigm, you can easily update the current project using a version control tool, and then execute the "Migration command, your database is up to date! 11. The Automatic Pagination function prevents a large number of irrelevant page configuration code from being incorporated into your business logic. It is convenient that you do not need to remember the current page... the remaining full text>