为什么 Laravel 会成为最成功的 PHP 框架?

HoeWang 2015-10-13

Laravel是一个有着美好前景的年轻框架,它的社区充满着活力,相关的文档和教程完整而清晰,并为快速、安全地开发现代应用程序提供了必要的功能。在近几年对PHP框架流行度的统计中,Laravel始终遥遥领先。那么是什么让Laravel成为最成功的PHP框架?

为什么 Laravel 会成为最成功的 PHP 框架?

Laravel

2011年,Taylor Otwell将Laravel作为一种包含全新现代方法的框架介绍给大家。Laravel最初的设计是为了面向MVC架构的,它可以满足如事件处理、用户身份验证等各种需求。另外它还有一个由管理数据库强力支持,用于管理模块化和可扩展性代码的软件包管理器。

Laravel以其简洁、优雅的特性赢得了大家的广泛关注,无论是专家还是新手,在开发PHP项目的时候,都会第一时间的想到Laravel。本文我们将讨论为什么Laravel会成为最成功的PHP框架。

模块化和可扩展性

Laravel注重代码的模块化和可扩展性。你可以在包含超过5500个程序包的Packalyst目录中找到你想要添加的任何文件。Laravel的目标是让你能够找到任何想要的文件。

微服务和程序接口

Lumen是一个由laravel衍生的专注于精简的微框架。它高性能的程序接口可让你更加简单快速的开发微型项目。Lumen使用最小的配置集成了所有laravel的重要特性,你可以通过将代码复制到laravel项目的方式将完整的框架迁移过来。

  1. <span class="pun"><?</span><span class="pln">php</span><br><span class="pln">$app</span><span class="pun">-></span><span class="kwd">get</span><span class="pun">(</span><span class="str">'/'</span><span class="pun">,</span><span class="kwd">function</span><span class="pun">()</span><span class="pun">{</span>
  2. <span class="kwd">return</span><span class="pln"> view</span><span class="pun">(</span><span class="str">'lumen'</span><span class="pun">);</span>
  3. <span class="pun">});</span>
  4. <span class="pln">$app</span><span class="pun">-></span><span class="pln">post</span><span class="pun">(</span><span class="str">'framework/{id}'</span><span class="pun">,</span><span class="kwd">function</span><span class="pun">(</span><span class="pln">$framework</span><span class="pun">)</span><span class="pun">{</span>
  5. <span class="pln">$this</span><span class="pun">-></span><span class="pln">dispatch</span><span class="pun">(</span><span class="kwd">new</span><span class="typ">Energy</span><span class="pun">(</span><span class="pln">$framework</span><span class="pun">));</span>
  6. <span class="pun">});</span>

HTTP路径

Laravel拥有类似于Ruby on Rails的,快速、高效的路由系统。它可以让用户通过在浏览器上输入路径的方式让应用程序的各部分相关联。

HTTP中间件

  1. <span class="typ">Route</span><span class="pun">::</span><span class="kwd">get</span><span class="pun">(</span><span class="str">'/'</span><span class="pun">,</span><span class="kwd">function</span><span class="pun">()</span><span class="pun">{</span>
  2. <span class="kwd">return</span><span class="str">'Hello World'</span><span class="pun">;</span>
  3. <span class="pun">});</span>

应用程序可受到中间件的保护——中间件会处理分析和过滤服务器上的HTTP请求。你可以安装中间件,用于验证注册用户,并避免如跨站脚本(XSS)或其它的安全状况的问题。

  1. <span class="pun"><?</span><span class="pln">php </span>
  2. <span class="kwd">namespace</span><span class="typ">App</span><span class="pln">\Http\Middleware</span><span class="pun">;</span>
  3. <span class="kwd">use</span><span class="typ">Closure</span><span class="pun">;</span>
  4. <span class="kwd">class</span><span class="typ">OldMiddleware</span><span class="pun">{</span>
  5. <span class="kwd">public</span><span class="kwd">function</span><span class="pln"> handle</span><span class="pun">(</span><span class="pln">$request</span><span class="pun">,</span><span class="typ">Closure</span><span class="pln"> $next</span><span class="pun">)</span><span class="pun">{</span>
  6. <span class="kwd">if</span><span class="pun">(</span><span class="pln">$request</span><span class="pun">-></span><span class="pln">input</span><span class="pun">(</span><span class="str">'age'</span><span class="pun">)</span><span class="pun"><=</span><span class="lit">200</span><span class="pun">)</span><span class="pun">{</span>
  7. <span class="kwd">return</span><span class="pln"> redirect</span><span class="pun">(</span><span class="str">'home'</span><span class="pun">);</span>
  8. <span class="pun">}</span>
  9. <span class="kwd">return</span><span class="pln"> $next</span><span class="pun">(</span><span class="pln">$request</span><span class="pun">);</span>
  10. <span class="pun">}</span>
  11. <span class="pun">}</span>

缓存

你的应用程序可得到一个健壮的缓存系统,通过对其进行调整,可以让应用程序的加载更加快速,这可以给你的用户提供最好的使用体验。

  1. <span class="typ">Cache</span><span class="pun">::</span><span class="pln">extend</span><span class="pun">(</span><span class="str">'mongo'</span><span class="pun">,</span><span class="kwd">function</span><span class="pun">(</span><span class="pln">$app</span><span class="pun">)</span><span class="pun">{</span>
  2. <span class="kwd">return</span><span class="typ">Cache</span><span class="pun">::</span><span class="pln">repository</span><span class="pun">(</span><span class="kwd">new</span><span class="typ">MongoStore</span><span class="pun">);</span>
  3. <span class="pun">});</span>

身份验证

安全是至关重要的。Laravel自带对本地用户的身份验证,并可以使用“remember” 选项来记住用户。它还可以让你例如一些额外参数,例如显示是否为活跃的用户。

  1. <span class="kwd">if</span><span class="pun">(</span><span class="typ">Auth</span><span class="pun">::</span><span class="pln">attempt</span><span class="pun">([</span><span class="str">'email'</span><span class="pun">=></span><span class="pln"> $email</span><span class="pun">,</span><span class="str">'password'</span><span class="pun">=></span><span class="pln"> $password</span><span class="pun">,</span><span class="str">'active'</span><span class="pun">=></span><span class="lit">1</span><span class="pun">],</span><span class="pln"> $remember</span><span class="pun">))</span><span class="pun">{</span>
  2. <span class="com">// The user is being remembered... </span>
  3. <span class="pun">}</span>

各种集成

Laravel Cashier可以满足你要开发支付系统所需要的一切需求。除此之外,它还同步并集成了用户身份验证系统。所以,你不再需要担心如何将计费系统集成到开发当中了。

  1. <span class="pln">$user </span><span class="pun">=</span><span class="typ">User</span><span class="pun">::</span><span class="kwd">find</span><span class="pun">(</span><span class="lit">1</span><span class="pun">);</span>
  2. <span class="pln">$user</span><span class="pun">-></span><span class="pln">subscription</span><span class="pun">(</span><span class="str">'monthly'</span><span class="pun">)-></span><span class="pln">create</span><span class="pun">(</span><span class="pln">$creditCardToken</span><span class="pun">);</span>

任务自动化

Elixir是一个可让我们使用Gulp定义任务的Laravel程序接口,我们可以使用Elixir定义可精简CSS 和JavaScript的预处理器。 

  1. <span class="pln">elixir</span><span class="pun">(</span><span class="kwd">function</span><span class="pun">(</span><span class="pln">mix</span><span class="pun">)</span><span class="pun">{</span>
  2. <span class="pln">mix</span><span class="pun">.</span><span class="pln">browserify</span><span class="pun">(</span><span class="str">'main.js'</span><span class="pun">);</span>
  3. <span class="pun">});</span>

加密

一个安全的应用程序应该做到可把数据进行加密。使用Laravel,可以启用OpenSSL安全加密算法AES-256-CBC来满足你所有的需求。另外,所有的加密值都是由检测加密信息是否被改变的验证码所签署的。

  1. <span class="kwd">use</span><span class="typ">Illuminate</span><span class="pln">\Contracts\Encryption\DecryptException</span><span class="pun">;</span>
  2. <span class="kwd">try</span><span class="pun">{</span>
  3. <span class="pln">$decrypted </span><span class="pun">=</span><span class="typ">Crypt</span><span class="pun">::</span><span class="pln">decrypt</span><span class="pun">(</span><span class="pln">$encryptedValue</span><span class="pun">);</span>
  4. <span class="pun">}</span><span class="kwd">catch</span><span class="pun">(</span><span class="typ">DecryptException</span><span class="pln"> $e</span><span class="pun">)</span><span class="pun">{</span>
  5. <span class="com">// </span>
  6. <span class="pun">}</span>

事件处理

应用程序中事件的定义、记录和聆听都非常迅速。EventServiceProvider事件中的listen包含记录在你应用程序上所有事件的列表。

  1. <span class="kwd">protected</span><span class="pln"> $listen </span><span class="pun">=</span><span class="pun">[</span>
  2. <span class="str">'App\Events\PodcastWasPurchased'</span><span class="pun">=></span><span class="pun">[</span>
  3. <span class="str">'App\Listeners\EmailPurchaseConfirmation'</span><span class="pun">,</span>
  4. <span class="pun">],</span>
  5. <span class="pun">];</span>

分页

在Laravel中分页是非常容易的因为它能够根据用户的浏览器当前页面生成一系列链接。 

  1. <span class="pun"><?</span><span class="pln">php </span>
  2. <span class="kwd">namespace</span><span class="typ">App</span><span class="pln">\Http\Controllers</span><span class="pun">;</span>
  3. <span class="kwd">use</span><span class="pln"> DB</span><span class="pun">;</span>
  4. <span class="kwd">use</span><span class="typ">App</span><span class="pln">\Http\Controllers\Controller</span><span class="pun">;</span>
  5. <span class="kwd">class</span><span class="typ">UserController</span><span class="kwd">extends</span><span class="typ">Controller</span><span class="pun">{</span>
  6. <span class="kwd">public</span><span class="kwd">function</span><span class="pln"> index</span><span class="pun">()</span><span class="pun">{</span>
  7. <span class="pln">$users </span><span class="pun">=</span><span class="pln"> DB</span><span class="pun">::</span><span class="pln">table</span><span class="pun">(</span><span class="str">'users'</span><span class="pun">)-></span><span class="pln">paginate</span><span class="pun">(</span><span class="lit">15</span><span class="pun">);</span>
  8. <span class="kwd">return</span><span class="pln"> view</span><span class="pun">(</span><span class="str">'user.index'</span><span class="pun">,</span><span class="pun">[</span><span class="str">'users'</span><span class="pun">=></span><span class="pln"> $users</span><span class="pun">]);</span>
  9. <span class="pun">}</span>
  10. <span class="pun">}</span>

对象关系化映射(ORM)

Laravel包含一个处理数据库的层,它的对象关系化映射被称为Eloquent。另外这个也适用于PostgreSQL

  1. <span class="pln">$users </span><span class="pun">=</span><span class="typ">User</span><span class="pun">::</span><span class="kwd">where</span><span class="pun">(</span><span class="str">'votes'</span><span class="pun">,</span><span class="str">'>'</span><span class="pun">,</span><span class="lit">100</span><span class="pun">)-></span><span class="pln">take</span><span class="pun">(</span><span class="lit">10</span><span class="pun">)-></span><span class="kwd">get</span><span class="pun">();</span>
  2. <span class="kwd">foreach</span><span class="pun">(</span><span class="pln">$users </span><span class="kwd">as</span><span class="pln"> $user</span><span class="pun">)</span><span class="pun">{</span>
  3. <span class="pln">var_dump</span><span class="pun">(</span><span class="pln">$user</span><span class="pun">-></span><span class="pln">name</span><span class="pun">);</span>
  4. <span class="pun">}</span>

单元测试

单元测试的开发是一个耗费大量时间的任务,但是它却是保证我们的应用程序保持正常工作的关键。Laravel中可使用PHPUnit执行单元测试。

  1. <span class="tag"><php</span>
  2. <span class="atn">use</span><span class="atn">Illuminate</span><span class="pln">\</span><span class="atn">Foundation</span><span class="pln">\</span><span class="atn">Testing</span><span class="pln">\</span><span class="atn">WithoutMiddleware</span><span class="pln">; </span>
  3. <span class="atn">use</span><span class="atn">Illuminate</span><span class="pln">\</span><span class="atn">Foundation</span><span class="pln">\</span><span class="atn">Testing</span><span class="pln">\</span><span class="atn">DatabaseTransactions</span><span class="pln">; </span>
  4. <span class="atn">class</span><span class="atn">ExampleTest</span><span class="atn">extends</span><span class="atn">TestCase</span><span class="pln"> { </span>
  5. <span class="atn">public</span><span class="atn">function</span><span class="atn">testBasicExample</span><span class="pln">() { </span>
  6. <span class="pln">$</span><span class="atn">this</span><span class="pln">-</span><span class="tag">></span><span class="pln">visit('/')->see('Laravel 5')->dontSee('Rails');</span>
  7. <span class="pln">}</span>
  8. <span class="pln">}</span>

待办事项清单

Laravel提供在后台使用待办事项清单(to do list)处理复杂、漫长流程的选择。它可以让我们异步处理某些流程而不需要用户的持续导航。

  1. <span class="typ">Queue</span><span class="pun">::</span><span class="pln"> push </span><span class="pun">(</span><span class="kwd">new</span><span class="typ">SendEmail</span><span class="pun">(</span><span class="pln"> $ message </span><span class="pun">));</span>

相关推荐