A comprehensive explanation of the popular PHP development framework Laravel

Source: Internet
Author: User
Tags form post
This article mainly introduces Laravel, a popular PHP development framework, and provides examples of some major technical features. For more information, see Major technical features of Laravel:

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!
10. Unit-Testing is an important part of Laravel. Laravel itself contains hundreds of test cases to ensure that any modification in one place will not affect other functions. this is one of the reasons why Laravel is considered the most stable version in the industry. Laravel also provides convenient functions, allowing your code to easily perform unit tests. All test cases can be run using the Artisan command line tool.
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. you only need to retrieve the total number of entries from the database, then use limit/offset to obtain the selected data, and finally call the 'paginate' method, let Laravel output the links of each page to the specified View. Laravel will automatically complete all the work for you. Laravel's automatic paging system is designed to be easy to implement and easy to modify. Although Laravel can automatically handle these tasks, do not forget to call the corresponding method or manually configure the paging system!

Below are some small examples to explain:
Microservice and program interface
Lumen is a lightweight microframework derived from laravel. Its high-performance program interface allows you to develop micro-projects more easily and quickly. Lumen integrates all the important features of laravel with the smallest configuration. you can migrate the complete framework by copying the code to the laravel project.

<?php$app->get('/', function() {  return view('lumen');});$app->post('framework/{id}', function($framework) {  $this->dispatch(new Energy($framework));});

HTTP path
Laravel has a fast and efficient routing system similar to Ruby on Rails. It allows users to join all parts of an application by entering the path in the browser.

HTTP middleware

Route::get('/', function () {   return 'Hello World'; });

Applications can be protected by middleware-middleware processes and filters HTTP requests on the server. You can install middleware to verify registered users and avoid cross-site scripting (XSS) or other security issues.

<?php namespace App\Http\Middleware; use Closure; class OldMiddleware {  public function handle($request, Closure $next) {   if ($request->input('age') <= 200) {      return redirect('home');   }   return $next($request); }}

Cache
Your application can obtain a robust cache system. by adjusting it, you can load your application more quickly, which provides you with the best user experience.

Cache::extend('mongo', function($app) {   return Cache::repository(new MongoStore);});

Authentication
Security is crucial. Laravel provides local user authentication and can use the "remember" option to remember the user. It also allows you to display additional parameters, such as whether the user is active.

if (Auth::attempt(['email' => $email, 'password' => $password, 'active' => 1 ], $remember)) {   // The user is being remembered... }

Various integrations
Laravel Cashier can meet all the requirements you need to develop a payment system. In addition, it also synchronizes and integrates the user authentication system. Therefore, you no longer need to worry about how to integrate the billing system into development.

$user = User::find(1);$user->subscription('monthly')->create($creditCardToken);

Task Automation
Elixir is a Laravel program interface that allows us to use Gulp to define tasks. we can use Elixir to define a preprocessor that can streamline CSS and JavaScript.

elixir(function(mix) {   mix.browserify('main.js'); });


Encryption
A secure application can encrypt data. With Laravel, you can enable the OpenSSL security encryption algorithm AES-256-CBC to meet all your needs. In addition, all encryption values are signed by the verification code that checks whether the encrypted information has been changed.

use Illuminate\Contracts\Encryption\DecryptException; try {   $decrypted = Crypt::decrypt($encryptedValue);} catch (DecryptException $e) {   // }

Event Processing
The definition, recording, and listening of events in applications are fast. The listen in the EventServiceProvider event contains a list of all events recorded in your application.

protected $listen = [ 'App\Events\PodcastWasPurchased' => [    'App\Listeners\EmailPurchaseConfirmation', ],];

Paging
In Laravel, paging is very easy because it can generate a series of links based on the current page of the user's browser.

<?php namespace App\Http\Controllers; use DB; use App\Http\Controllers\Controller; class UserController extends Controller {  public function index() {   $users = DB::table('users')->paginate(15);  return view('user.index', ['users' => $users]); }}

Object Relational ing (ORM)
Laravel contains a layer for processing databases. Its Object Relational ING is called Eloquent. This also applies to PostgreSQL.

$users = User::where('votes', '>', 100)->take(10)->get();foreach ($users as $user) {  var_dump($user->name);}

Unit test
Unit test development is a time-consuming task, but it is the key to ensure that our applications work normally. In Laravel, PHPUnit can be used to perform unit tests.

 
  visit('/')->see('Laravel 5')->dontSee('Rails'); }}
 

To-do list
Laravel provides the option to use the to do list in the background to process complex and long processes. It allows us to process some processes asynchronously without the need for continuous navigation.

Queue :: push ( new SendEmail ( $ message ));

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.