Upgrade to Pro — share decks privately, control downloads, hide ads and more …

PHP 也有 Day #51:高效能框架的曙光 - 以 Laravel 經驗開發 Hyperf 應用

Star Rocket
December 17, 2019

PHP 也有 Day #51:高效能框架的曙光 - 以 Laravel 經驗開發 Hyperf 應用

我們都喜歡 Laravel,對吧?但 PHP 天生的限制及 Laravel 的肥大讓我們在高併發的情境下遇到了瓶頸。有沒有一種可能是融合 Swoole 的威力與 Laravel 的優美,為高效能框架立下新的標竿?很幸運的,我們在 Hyperf 身上看到了這樣的潛力!在這場分享裡,將從效能的評比開場,說明 Laravel 的瓶頸及 Hyperf 的突破。接著會以實作一個網路應用程式為例,比較兩者間在開發上的異同。最後會分享一些開發技巧,讓大家在適應 Hyperf 時可以更加無痛。透過這場分享,期能讓更多 PHP/Laravel 開發者走出舒適圈、看到更多可能。

Star Rocket

December 17, 2019
Tweet

More Decks by Star Rocket

Other Decks in Programming

Transcript

  1. 絑ह襑穩 — PHP Composer Extension • Swoole >= 4.4 •

    OpenSSL • JSON • PDO • PHP >= 7.2
  2. 絑ह礍戔 — ๜秚礍戔 Vagrant Docker Homestead • macOS: Homebrew •

    Windows: WSL • Linux: apt hyperf/hyperf:7.2-alpine-cli
  3. 䌕礯ୌ缏 — // hyperf $ composer create-project hyperf/hyperf-skeleton // laravel

    $ composer create-project laravel/laravel \ --prefer-dist ݶ物 • ᮷ฎֵአ composer 瞲犤 吖物 • package ጱݷ圸犋ݶ • ग़ԧ԰㵕㺔ᒼ螡殻
  4. Route — // hyperf Router::get('...', function () { return '...';

    }); Router::get('...', [Controller::class, '...']); // laravel Route::get('...', function () { return '...'; }); Route::get('...', [Controller::class, '...']); ݶ物 • 承ဩ奾䯤觊犲 吖物 • Class ݷ圸犋ݶ • ䷱磪 Laravel 蝡讕ग़箸ದ
  5. CLI Tool — // hyperf $ php bin/hyperf.php command [options]

    [arguments] // laravel $ php artisan command [options] [arguments] ݶ物 • ᮷ฎ䟖አ Symfony Console 吖物 • Command ݷ圸犋ݶ • ֖ᗝ犋ݶ • 犋ฎ 100% ጱ瞲犤᮷磪ᑏ༙
  6. Controller — // hyperf $ php bin/hyperf.php gen:controller TaskController declare(strict_types=1);

    class TaskController extends AbstractController { public function index() { // ... } } // laravel $ php artisan make:controller TaskController class TaskController extends Controller { public function index() { // ... } } ݶ物 • 皃ԒӞ䰬 吖物 • 䟖አ PHP 7 ጱ承ဩ
  7. View Packages — // view component $ composer require hyperf/view

    // blade engine $ composer require duncan3dc/blade // task component $ composer require hyperf/task
  8. Setup View Component — // config/autoload/view.php return [ 'engine' =>

    BladeEngine::class, 'mode' => Mode::TASK, 'config' => [ 'view_path' => BASE_PATH.'/storage/view/', 'cache_path' => BASE_PATH.'/runtime/view/', ], ];
  9. Setup Task Component — // config/autoload/server.php return [ // ...

    'settings' => [ // ... 'task_worker_num' => 8, 'task_enable_coroutine' => false, ], 'callbacks' => [ SwooleEvent::ON_TASK => [ TaskCallback::class, 'onTask' ], SwooleEvent::ON_FINISH => [ FinishCallback::class, 'onFinish' ], ], ];
  10. Serve Assets — // config/autoload/server.php return [ // ... 'settings'

    => [ // ... 'document_root' => BASE_PATH.'/public', 'static_handler_locations' => ['/'], 'enable_static_handler' => true, ], ]; 吖物 • Laravel 犋蒂ቘ assets
  11. Views — // app/Controller/*Controller.php public function index(RenderInterface $render) { //

    ... return $render->render('...', []); } ݶ物 • ᮷ݢ犥አ Blade 吖物 • ๅ䕃௔ጱ䰬礂螡殻 • 襑ᥝग़蕕 Task زկ
  12. Migration — // hyperf $ php bin/hyperf.php gen:migration migration_name //

    laravel $ php artisan make:migration migration_name class Create...Table extends Migration { public function up(): void { Schema::create('.', function (Blueprint $table) { $table->bigIncrements('id'); // ... $table->timestamps(); }); } public function down(): void { Schema::dropIfExists('...'); } } ݶ物 • 皃ԒӞ䰬 吖物 • 䲆礯硯ᗝ֖ᗝ犋ݶ
  13. Model — // hyperf $ php bin/hyperf.php gen:model Name //

    laravel $ php artisan make:model Name class Task extends Model { protected $table = '...'; protected $fillable = [...]; protected $casts = [...]; } ݶ物 • 皃Ԓ蚤 Eloquent Ӟ䰬 吖物 • 毆戔叨ኞጱ䰬礂犚盏癩吖
  14. Seeder — // hyperf $ php bin/hyperf.php gen:seeder TableSeeder $

    php bin/hyperf.php db:seed // laravel $ php artisan make:seeder TableSeeder $ php artisan db:seed class TableSeeder extends Seeder { public function run() { // ... } } ݶ物 • 皃ԒӞ䰬 吖物 • ䷱磪 DatabaseSeeder ጱ戔懯 • 䲆ݷ䙼ֺ犋ݶ • 䲆礯硯ᗝ֖ᗝ犋ݶ
  15. Request & Response — // hyperf public function store( RequestInterface

    $request, ResponseInterface $response ) { return $response->redirect('...'); return $response->json([ ... ]); } // laravel public function index(Request $request) { return redirect('...'); return response()->json([ ... ]); } ݶ物 • 禊盢觊犲 吖物 • 襑ᥝဳ獈ጱزկ犋ݶ
  16. CRUD — // read Model::all(); // create Model::create([ ... ]);

    // update $task = Model::find($id); $task->update([ ... ]); // delete $task = Model::find($id); $task->delete(); ݶ物 • Ӟ䰬
  17. ࢧ觎 — • PHP ጱ螀ᤈ粬௔ • Laravel ጱ硳胼絵毮 • Swoole

    现 Hyperf ጱݢ胼薹ဩ • Hyperf 墋Օ现硳胼箛ێ疻纈 • አ૪妿䨝ጱ Laravel 妿涢旉ᑏک Hyperf