Laravel’s main technical features:
1. Bundle is the organization form or name of Laravel’s expansion package. Laravel's extension package repository is quite mature and can easily help you install extension packages (bundles) into your application. You can choose to download an extension package (bundle) and copy it to the bundles directory, or install it automatically through the command line tool "Artisan".
2. Laravel already has a set of advanced PHP ActiveRecord implementation - Eloquent ORM. It can easily apply "constraints" to both sides of the relationship, so that you have complete control over the data and enjoy all the conveniences of ActiveRecord. Eloquent natively supports all methods of the query builder (query-builder) in Fluent.
3. Application logic can be implemented in controllers or directly integrated into route statements, and the syntax is similar to the Sinatra framework. Laravel's design philosophy is to give developers maximum flexibility, allowing them to create very small websites and build large-scale enterprise applications.
4. Reverse Routing gives you the ability to create links (URIs) through route names. Just use the route name and Laravel will automatically create the correct URI for you. This way you can change your routes at any time, and Laravel will automatically update all related links for you.
5. Restful Controllers are an optional way to distinguish GET and POST request logic. For example, in a user login logic, you declare a get_login() action to process the service of obtaining the login page; you also declare a post_login() action to verify the data POSTed from the form, and After validation, a decision is made to redirect to the login page or to the console.
6. Class Auto-loading simplifies the loading of classes. In the future, you no longer need to maintain the auto-loading configuration table and unnecessary component loading. When you want to load any library or model, just use it immediately, and Laravel will automatically load the required files for you.
7. View Composers are essentially a piece of code that is automatically executed when the View is loaded. The best example is the random article recommendation on the side of the blog. The "view assembler" contains the logic for loading the random article recommendation. In this way, you only need to load the view of the content area, and Laravel will do the other things. Complete it automatically for you.
8. The reverse control container (IoC container) provides a convenient way to generate new objects, instantiate objects at any time, and access singleton objects. Inverse control (IoC) means that you almost don't need to load external libraries (libraries), you can access these objects anywhere in the code, and you don't need to endure complicated and redundant code structures.
9. Migrations is like a version control tool, but it manages the database paradigm and is directly integrated into Laravel. You can use the "Artisan" command line tool to generate and execute "migration" instructions. When your team members change the database paradigm, you can easily update the current project through the version control tool, and then execute the "migrate" command. Well, your database is already up to date!
10. Unit-Testing is a very important part of Laravel. Laravel itself contains hundreds of test cases to ensure that any modification will not affect the functionality of other parts. This is one of the reasons why Laravel is considered the most stable version in the industry. Laravel also provides convenient functions to make unit testing your own code easy. All test cases can be run through the Artisan command line tool.
11. The Automatic Pagination function avoids mixing a large amount of irrelevant paging configuration code into your business logic. The convenience is that you don't need to remember the current page, just get the total number of entries from the database, then use limit/offset to get the selected data, and finally call the 'paginate' method to let Laravel output the links of each page to the specified view ( 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 handle these tasks automatically, don't forget to call the corresponding methods and manually configure the paging system!
Let’s use some small examples to explain:
Microservices and programming interfaces
Lumen is a micro-framework derived from laravel that focuses on simplicity. Its high-performance programming interface allows you to develop micro-projects more easily and quickly. Lumen integrates all the important features of laravel with minimal 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路径
Laravel拥有类似于Ruby on Rails的,快速、高效的路由系统。它可以让用户通过在浏览器上输入路径的方式让应用程序的各部分相关联。
HTTP中间件
Route::get('/', function () { return 'Hello World'; });
应用程序可受到中间件的保护——中间件会处理分析和过滤服务器上的HTTP请求。你可以安装中间件,用于验证注册用户,并避免如跨站脚本(XSS)或其它的安全状况的问题。
<?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::extend('mongo', function($app) { return Cache::repository(new MongoStore); });
身份验证
安全是至关重要的。Laravel自带对本地用户的身份验证,并可以使用“remember” 选项来记住用户。它还可以让你例如一些额外参数,例如显示是否为活跃的用户。
if (Auth::attempt(['email' => $email, 'password' => $password, 'active' => 1 ], $remember)) { // The user is being remembered... }
各种集成
Laravel Cashier可以满足你要开发支付系统所需要的一切需求。除此之外,它还同步并集成了用户身份验证系统。所以,你不再需要担心如何将计费系统集成到开发当中了。
$user = User::find(1); $user->subscription('monthly')->create($creditCardToken);
任务自动化
Elixir是一个可让我们使用Gulp定义任务的Laravel程序接口,我们可以使用Elixir定义可精简CSS 和JavaScript的预处理器。
elixir(function(mix) { mix.browserify('main.js'); });
加密
一个安全的应用程序应该做到可把数据进行加密。使用Laravel,可以启用OpenSSL安全加密算法AES-256-CBC来满足你所有的需求。另外,所有的加密值都是由检测加密信息是否被改变的验证码所签署的。
use Illuminate\Contracts\Encryption\DecryptException; try { $decrypted = Crypt::decrypt($encryptedValue); } catch (DecryptException $e) { // }
事件处理
应用程序中事件的定义、记录和聆听都非常迅速。EventServiceProvider事件中的listen包含记录在你应用程序上所有事件的列表。
protected $listen = [ 'App\Events\PodcastWasPurchased' => [ 'App\Listeners\EmailPurchaseConfirmation', ], ];
分页
在Laravel中分页是非常容易的因为它能够根据用户的浏览器当前页面生成一系列链接。
<?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]); } }
对象关系化映射(ORM)
Laravel包含一个处理数据库的层,它的对象关系化映射被称为Eloquent。另外这个也适用于PostgreSQL。
$users = User::where('votes', '>', 100)->take(10)->get(); foreach ($users as $user) { var_dump($user->name); }
单元测试
单元测试的开发是一个耗费大量时间的任务,但是它却是保证我们的应用程序保持正常工作的关键。Laravel中可使用PHPUnit执行单元测试。
<php use Illuminate\Foundation\Testing\WithoutMiddleware; use Illuminate\Foundation\Testing\DatabaseTransactions; class ExampleTest extends TestCase { public function testBasicExample() { $this->visit('/')->see('Laravel 5')->dontSee('Rails'); } }
待办事项清单
Laravel提供在后台使用待办事项清单(to do list)处理复杂、漫长流程的选择。它可以让我们异步处理某些流程而不需要用户的持续导航。
Queue :: push ( new SendEmail ( $ message ));