Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

以 Laravel 經驗開發 Hyperf 應用

1,093 views

Published on

受邀至 PHP 也有 Day #51 分享如何用您已經會的 Laravel 知織,無痛轉移到效能更好的 Hyperf 框架上。示範程式碼:https://github.com/shengyou/hyperf-mvc-introduction

Published in: Internet
  • Be the first to comment

以 Laravel 經驗開發 Hyperf 應用

  1. 1. Laravel Hyperf — (Shengyou Fan) PHP Day #51 2019/12/17
  2. 2. — • Developer Advocate • Backend Developer • Open Source Community Organizer TEAM
  3. 3. Laravel
  4. 4. — </>
  5. 5.
  6. 6. — https://www.techempower.com/benchmarks/
  7. 7. — https://github.com/kenjis/php-framework-benchmark
  8. 8. PHP — https://www.slideshare.net/chtalbert/the-integration-of-laravel-with-swoole https://www.youtube.com/watch?v=wTrUOWuJBl0
  9. 9. Laravel — https://www.slideshare.net/chtalbert/the-integration-of-laravel-with-swoole https://www.youtube.com/watch?v=wTrUOWuJBl0
  10. 10. Request — count(get_included_files()); 331
  11. 11.
  12. 12.
  13. 13. C —
  14. 14. Swoole — https://www.swoole.com/
  15. 15. Hyperf — https://hyperf.io/
  16. 16. Hyperf JSON — HyperfLaravel
  17. 17. Hyperf — HyperfLaravel
  18. 18. QPS … — 60~1200
  19. 19.
  20. 20. — Todo MVC
  21. 21. — PHP Composer Extension • Swoole >= 4.4 • OpenSSL • JSON • PDO • PHP >= 7.2
  22. 22. — Vagrant Docker Homestead• macOS: Homebrew • Windows: WSL • Linux: apt hyperf/hyperf:7.2-alpine-cli
  23. 23. — // hyperf $ composer create-project hyperf/hyperf-skeleton // laravel $ composer create-project laravel/laravel --prefer-dist • composer • package •
  24. 24. Route — // hyperf Router::get('...', function () { return '...'; }); Router::get('...', [Controller::class, '...']); // laravel Route::get('...', function () { return '...'; }); Route::get('...', [Controller::class, '...']); • • Class • Laravel
  25. 25. CLI Tool — // hyperf $ php bin/hyperf.php command [options] [arguments] // laravel $ php artisan command [options] [arguments] • Symfony Console • Command • • 100%
  26. 26. 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
  27. 27. View Packages — // view component $ composer require hyperf/view // blade engine $ composer require duncan3dc/blade // task component $ composer require hyperf/task
  28. 28. 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/', ], ];
  29. 29. 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' ], ], ];
  30. 30. Serve Assets — // config/autoload/server.php return [ // ... 'settings' => [ // ... 'document_root' => BASE_PATH.'/public', 'static_handler_locations' => ['/'], 'enable_static_handler' => true, ], ];• Laravel assets
  31. 31. Views — // app/Controller/*Controller.php public function index(RenderInterface $render) { // ... return $render->render('...', []); } • Blade • • Task
  32. 32. 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('...'); } } • •
  33. 33. 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 •
  34. 34. 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 • •
  35. 35. 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([ ... ]); } • •
  36. 36. CRUD — // read Model::all(); // create Model::create([ ... ]); // update $task = Model::find($id); $task->update([ ... ]); // delete $task = Model::find($id); $task->delete(); •
  37. 37. — • PHP • Laravel • Swoole Hyperf • Hyperf • Laravel Hyperf
  38. 38. — • PHP • Swoole • Hyperf • Hyperf
  39. 39. — SwooleHyperf Hyperf Hyperf
  40. 40. — https://github.com/shengyou/hyperf-mvc-introduction
  41. 41. (Shengyou) shengyou.fan@jetbrains.com Q&A —

×