ã¿ãªããããã«ã¡ã¯ïŒ
ãã®èšäºã§ã¯ã1ã€ã®ã¬ã¬ã·ãŒãããžã§ã¯ãã§ãã¢ãã³ãã¬ã³ãããã©ãããã©ãŒã ã«ç§»è¡ããç§ã®çµéšã«ã€ããŠã話ããããšæããŸãã
ããã¯ãã¹ãŠçŽ1幎åã«å§ãŸãã圌ãã¯ç§ããå€ããïŒç§ã«ãšã£ãŠã¯æ°ããïŒéšéã«æãå
¥ããŸããã
ãã以åã¯ãSymfony / Laravelã§åããŠããŸããã èªäœã®ãã¬ãŒã ã¯ãŒã¯ã䜿çšãããããžã§ã¯ãã«åãæ¿ãããããWTFã®éã¯ãããã«å¢ããŸãããããããŠããã»ã©æªãã¯ãªããªããŸããã
ãŸãããããžã§ã¯ãã¯æ©èœããŸããã 次ã«ãèšèšãã¿ãŒã³ã®äœ¿çšã远跡ãããŸãããäŸåé¢ä¿ã³ã³ãããActiveRecordãããã³QueryBuilderããããŸããã
ããã«ãã³ã³ããããã¬ãŒããã¥ãŒã®æäœãããã³ãµãŒãã¹ã¬ã€ã€ãŒã®éå§ã«å¯Ÿããè¿œå ã®æœè±¡åã¬ãã«ããããŸããïŒããžãã¹ããžãã¯ã¯HTTPã¬ã€ã€ãŒã«äŸåãããäžéšã®å Žæã§ã¯ããžãã¯ãã³ã³ãããŒã©ãŒããåãåºãããŠããŸããïŒã
次ã«ãææ ¢ããã®ãé£ããã£ããã®ã«ã€ããŠèª¬æããŸãã
1.ãã¬ãŒlog4php
ãã¬ãŒèªäœã¯ããŸãæ©èœããŸããã ãããããã¡ãªããã¯ãããŸããã
- ã€ã³ã¿ãŒãã§ã€ã¹ã®æ¬ åŠ
- ã¿ã¹ã¯ã®æ§æã®è€éãã¯ããæšæºçã§ãïŒããšãã°ããšã©ãŒã¬ãã«ãã°ãElastickSearchã«éä¿¡ããïŒã
- ãªãŒãã³ãœãŒã¹ã®äžçã®ã³ã³ããŒãã³ãã®å€§éšåã¯ã Psr \ Log \ LoggerInterfaceã€ã³ã¿ãŒãã§ãŒã¹ã«äŸåããŠããŸã ã ãããžã§ã¯ãã§ã¯ããŸã äž¡æ¹ã®ãã¬ãŒãä¿æããå¿ èŠããããŸããã
2-6ã ã³ã³ãããŒã©ãŒã®åœ¢åŒã¯æ¬¡ã®ãšããã§ãã
<?php class AwesomeController { public function actionUpdateCar($carId) { $this->checkUserIsAuthenticated(); if ($carId <= 0) { die(' '); } $car = Car::findById($carId); $name = @$_POST['name']; container::getCarService()->updateNameCar($car, $name); echo json_decode([ 'message' =>' ' ]); } }
- ã¢ããªã±ãŒã·ã§ã³ã³ãŒãã®å®è¡äžã«æ»ã¬
- ã³ã³ãããŒã©ãŒãçµäºããåã«ãšã³ãŒããŸãã
- ãŠãŒã¶ãŒèªèšŒã¯åã³ã³ãããŒã©ãŒã§åå¥ã«æ¥ç¶ãããŸãã
- åŸæ¥ã®$ _POSTããã³@
- ã³ã³ãããŒã©ã«ãµãŒãã¹ãå®è£ ããå¯èœæ§ã¯ãããŸããã§ããã
7.ã°ããŒãã«ã¢ããªã±ãŒã·ã§ã³ãšã©ãŒãã®ã³ã°ã®æ¬ åŠ
ãã°ã§èŠã€ããæ倧å€ã¯ãã¡ãã»ãŒãžããã¹ãã§ãã ãšã©ãŒã«ã€ããŠã®ãã詳现ãªæ å ±ã¯ãéçºã¹ã¿ã³ãã§ç¹°ãè¿ããåŸã«ååŸã§ããŸãã æŠéç°å¢ã§ããã«ãã°ãèšé²ããã«ã¯ãç®çã®ã³ã³ãããŒã©ãŒã¡ãœããã«try / catchãããã¯ãé 眮ããå¿ èŠããããŸããã
8.äŸåé¢ä¿ã³ã³ãããŒã®æ§æ
äŸåé¢ä¿ã³ã³ããã¯ãæ代2.4ã®Symfonyã³ã³ããã«äŒŒãŠããŸããã åãµãŒãã¹ã«ã¯ãç»é²ãšãã®çµã¿ç«ãŠæ¹æ³ã®èª¬æãå¿ èŠã§ããã èªåé ç·ãæ倧éã«äœ¿çšãããlaravelã³ã³ãããŒã®çµéšããããããæ¥åžžçãªã¢ã¯ã·ã§ã³ãåãé€ããããšæããŸããã ãŸããèªåé ç·ã®æ¬ åŠã«ãããããã°ã©ããŒãåå¥ã®ããžãã¹ã¿ã¹ã¯çšã«åå¥ã®ãµãŒãã¹ãäœæããïŒæ°ããã¯ã©ã¹ãäœæããïŒæ¬²æ±ãæžããŸããã ãã®å Žåãééããç¯ããŠããã«æéã倱ãå¯èœæ§ãåžžã«ãããŸãã
9.ã«ãŒãã£ã³ã°
Yii1ã«åºã¥ããã«ãŒãã£ã³ã°ã¯è«ççã§ã·ã³ãã«ã§ããã
www.carexchange.ru/awesome_controller/update_carã®ãããªã¢ãã¬ã¹ã¯ãAwesomeControllerã³ã³ãããŒã©ãŒãšactionUpdateCarã¡ãœããã®å®è¡ãæå³ããŠããŸããã
ããããæ®å¿µãªããããµã€ãã®ãã¹ãããããµããã£ã¬ã¯ããªãããããã©ãŒã ã®URLãäœæããå¿
èŠããããŸãã
www.carexchange.ru/awesome_controller_for_car_insite_settings_approve/update_car
ããã¯æ°ã«ããŸããããå¶éã¯å¥åŠã§ã
10. URLãããŒãã³ãŒããã
ã«ãŒãã£ã³ã°ã¯åçŽã ã£ãã®ã§ãURLãèªåçã«çæããæ¹æ³ã¯ãããŸããã§ããïŒãªãè€éãªã®ãïŒã ããã«ãããphpãšjsã®äž¡æ¹ã§ããŒãã³ãŒããããæ°åã®ãªã³ã¯ãäœæãããŸããã ãã¡ãããURLãå€æŽããããšã¯ãã£ãã«ãããŸããããæã èµ·ããããšããããŸãã ãããŠããããžã§ã¯ãã§ããããæ¢ãã®ã¯é£ããã§ãã
äœããå€ããæãæ¥ãŸããïŒ
å¥ã®ããã°ã©ããŒã®åºçŸã«ããããªãã¡ã¯ã¿ãªã³ã°ã®å¯èœæ§ã«ã€ããŠçåãæèµ·ããå§ãããããããã人éçãªããã®ã«ããããšããèŠæããããŸããã ããã人éçãªã-çŸä»£ã®éçºè ã«ãšã£ãŠãã身è¿ãªãã®ãèªãã§ãã ããã æ¢åã®ã³ãŒãã®èªã¿åããšä¿å®ã¯å°é£ã§ããã
çµå¶é£ãšã®ããã€ãã®è°è«ã®åŸãç·è²ã®æãåŸãããæŠå¿µå®èšŒã«é¢ããäœæ¥ãéå§ãããŸããã
åœåãããçŸä»£ââã®åºæºã«åŸãããšã決å®ãããŸããã ãããã¯PSRã®æšå¥šäºé ã§ãããä»ã®ãã¬ãŒã ã¯ãŒã¯ã®æšæºçãªåäœã«åŸããŸãã ææ°ã®ãã¬ãŒã ã¯ãŒã¯ã§äœæ¥ããæ°ããéçºè ã¯ãã³ã³ãããŒã©ãŒã®å ŽæããµãŒãã¹ã®çµã¿ç«ãŠæ¹æ³ãããžãã¹ããžãã¯ã®äœæå Žæãç解ããŠããå¿ èŠããããŸãã
é³å£°ã«ããã¯ã¬ãŒã ã詳ããèŠããšãã¢ããªã±ãŒã·ã§ã³ã¬ãã«ã®ã³ãŒãïŒã³ã³ãããŒã©ãŒïŒãšã€ã³ãã©ã¹ãã©ã¯ãã£ã¬ã€ã€ãŒïŒã³ã³ãããŒïŒã«åé¡ãããããšãããããŸãã
ããžãã¹ããžãã¯ã¯åå¥ã«èšè¿°ãããŠãããHTTPã¬ãã«ã«äŸåããŸããã§ããããã®ãŸãŸã«ããŠãããŸãã Active RecordãšQueryBuilderãåäœããŸããããåãæ矩/ dbalãšããŸãå€ãããªãããã觊ããŸããã
ãã¬ãŒã ã¯ãŒã¯ã®éžæ
å®éãããã§ã®éžæã¯å€§ãããããŸããã§ããã HTTPçµç±ã®ã¬ã€ã€ãŒã®ããã«ãã¹ãŠã®laravelãŸãã¯symfonyããã©ãã°ããŠãæå³ããããŸããã ãŸããå¿
èŠãªã³ã³ããŒãã³ãã¯ãã€ã§ãäœæ²å®¶ãä»ããŠæ¥ç¶ã§ããŸãã
çå£ãªéžæã¯ã2ã€ã®ãã€ã¯ããã¬ãŒã ã¯ãŒã¯ãã¹ãªã ãšZendã®éã§ããã
ãããã®ãã¬ãŒã ã¯ãŒã¯ã¯äž¡æ¹ãšããPSR-7ããã³PSR-11ãå®å
šã«ãµããŒãããŠããŸãã
ãªãã«ãŒã¡ã³ãããªãã®ïŒ ãã¡ãããäž»ãªçç±ã¯ãã«ãŒã¡ã³ãããã€ã¯ãããšåŒã°ããããšã¯ã»ãšãã©ãªãããã®ãã¹ãŠã®å©ç¹ãšçµã³ã€ããŠããããã§ãã æ¢åã®ãããžã§ã¯ãã«Lumenãçµã¿èŸŒãããšã¯å°é£ã§ãã äŸåé¢ä¿ã³ã³ãããç°¡åã«äº€æããããšã¯ã§ããŸããïŒç §æå¥çŽãžã®æºæ ãå¿ èŠã§ãïŒã PSR-7ã³ã³ãã©ã¯ãã¯ãã¬ãŒã ã¯ãŒã¯ããµããŒãããŸãããããã§ãsymfony / http-foundationã«äŸåããŠããŸãã
æåã¯ãZendãçå£ã«èããŸããã ããããããã¹ãŠã®ããã«ãŠã§ã¢ãã€ããªãã®ãŒã§ã¢ããªã±ãŒã·ã§ã³ã®å®è£ ãèŠãŠãã³ã³ãããŒæ§æãã©ã®ããã«åœ¢æãããããèŠãŠ2æ¥éãéãããåŸãç§ã¯çµéšã®æµ ãéçºè ã«åŒã³åºãå¯èœãå·¥å Žãšã©ã®ããã«ç°ãªãããšã€ãªã¢ã¹ãæžãææãã©ã®ããã«èª¬æããã®ãæ³åããã®ãæãã£ãã§ãã å®ç§äž»çŸ©è ãåŠè ã«ãšã£ãŠãZendã¯ããã奜ãã¹ãã§ãã ã¢ããªã±ãŒã·ã§ã³ã¯ããã€ãã©ã€ã³ãšããã«ãŠã§ã¢ãä»ããŠæ©èœããŸãã ããããç§ã¯å ¥å Žã®ããé«ãéŸå€ãæããŠããŸããããåãã¯ç°¡åã§ãçæ³çã«ã¯èŠããªãããã«ããããšã§ããã
ãã®åŸãã¹ãªã ã«åãæ¿ããŸããã ãããžã§ã¯ãã§ã®å®è£ ã«ã¯1æ¥ãããããŸããã§ããã ã³ã³ãããŒã©ãŒïŒå€ããã®ãšæ°ãããã®ïŒã®éžæã¯ãããã«ãŠã§ã¢ã«ãã£ãŠå®è£ ãããŸããã ã¹ãªã ã§åæ¢ããŸããã PSR-15ããã«ãŠã§ã¢ã§ãã€ãã©ã€ã³ã«åãæ¿ããé ãèšç»ã§ã
ã³ã³ããã®éžæ
ããã§ç§ã¯ãªãŒã°/ã³ã³ããã«èœã¡çãããšèšã£ãŠãèªåã®éžæã説æããããšããŸãã
- ããã¯PSR-11ãµããŒãã§ãã
çŸåšãã»ãšãã©ã®ã³ã³ããã¯ãã§ã«PSR-11ããµããŒãããŠããŸããã1幎åã«ã¯ã³ã³ãã/çžäºéçšã€ã³ã¿ãŒãã§ã€ã¹ããµããŒãããŠããã®ã¯ããäžéšã®ã¿ã§ãã - èªåé ç·ã
- åãzend-servicemanagerãšã¯å¯Ÿç §çã«ãSyntasisã¯éåžžã«åçŽã§ãã
- ã¢ãžã¥ãŒã«ãããã«åé¢ããŠèšè¿°ã§ãããµãŒãã¹ãããã€ããŒã
ç §æ/ã³ã³ããã§ã¯ããããã€ãã¯ã¢ããªã±ãŒã·ã§ã³ã¬ãã«ã§ç»é²ããããªãŒã°/ã³ã³ããã§ã¯ããããã€ãã¯ã³ã³ããã¬ãã«ã§ç»é²ãããŸãã ãããã£ãŠãã¢ããªã±ãŒã·ã§ã³ã¯ã³ã³ããã®ã¿ã«äŸåããã³ã³ããã¯ãµãŒãã¹ãããã€ããŒã«äŸåããŸãã - ã³ã³ããã®å§ä»»ã ãã®ãæ©èœãã¯ãã³ã³ãã亀æãã§ãŒãºã«ãšã£ãŠæ±ºå®çãªãã®ã§ããããšãå€æããããã詳现ãé瀺ããŸãã
å¿ èŠã«å¿ããŠããªãŒã°/ã³ã³ããå ã«è€æ°ã®PSR-11äºæã³ã³ãããããå ŽåããããŸãã
èããããã·ããªãªïŒå€ãã³ã³ãããŒãsymfony / dependency-injectionã«å€æŽããããšã«ããŸããã åŸã ã«ç§»åããã«ã¯ããªãŒã°/ã³ã³ãããŒãæ¥ç¶ããå€ãã³ã³ãããŒãšsymfonyã³ã³ãããŒã®äž¡æ¹ãããªã²ãŒãã«é 眮ããŸãã ãµãŒãã¹ãæ€çŽ¢ãããšããå€ãã³ã³ãããŒãæåã«ããŒãªã³ã°ãããsymfonyã³ã³ãããŒã§æ€çŽ¢ãè¡ãããŸãã 次ã®ã¹ãããã§ã¯ããã¹ãŠã®ãµãŒãã¹ã®èª¬æãsymfonyã³ã³ãããŒã«è»¢éããããã ããæ®ãããšãã§ããŸãã ã³ãŒãã¯PSR-11ã€ã³ã¿ãŒãã§ã€ã¹ã«äŸåãããããå€æŽã¯æå°éã§ãã
HTTPãä»ããæœè±¡åã®éžæ
次ã®3ã€ã®ãªãã·ã§ã³ã®ã¿ããããŸãã
ãšããã§ãSlimã¯HTTPå®è£
ãåå¥ã®ããã±ãŒãžïŒãã©ã³ã4.0ã§äºå®ïŒã«åé¢ããããã«åããŠããŸãã
äžèŠãªã³ãŒããšäžèŠãªäŸåé¢ä¿ã®ãããSymfonyããªããžã䜿çšããããããŸããã§ããã Slimã¯ç§ãã¡ãäœãå¶éããªãã®ã§ãZendã®å®è£
ãåªå
ãããŸããã ããã¯ãHTTPã³ãŒãå±€ããã®ã¢ããªã±ãŒã·ã§ã³ã³ãŒãã®ç¬ç«æ§ãé«ããã ãã§ãã
ãã®ã³ã°
ããã§ã¯ãã¢ããã°ã ããæãæµ®ãã³ãŸãã 圌ãã¯ããããã蟌ãã ã PHPConsoleHandlerãšChromePHPHandlerã¯éçº äžã«äŸ¿å©ã§ã
ã«ãŒãã£ã³ã°
ã¹ãªã ã§ããã«äœ¿ããFastRouteããããŸãã ããã«åºã¥ããŠãååä»ãã«ãŒããç»å ŽããŸããã URLçæã¯ãã°ããŒãã«ãã«ããŒãä»ããŠå®è£ ãããŸãïŒ ãã¡ãã®ããã« ïŒ
ããã§ãäœãå€ãã£ãã®ã§ããããïŒ
ã³ã³ãããŒã©ãŒã¯æ¬¡ã®ããã«ãªããŸãã
<?php namespace Controllers; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ResponseInterface; use Zend\Diactoros\Response\JsonResponse; use Domain\Car\Services\CarService; class AwesomeController { /** * @var CarService */ private $carService; public function __construct(CarService $carService) { $this->carService = $carService; } public function actionUpdateNameCar(ServerRequestInterface $request, $carId): ResponseInterface { if ($carId <= 0) { throw new BadRequestException(' '); } $car = $this->carService->getCar($carId); $name = $request->getParsedBody()['name']; $this->carService->updateNameCar($car, $name); return new JsonResponse([ 'message' => 'updateNameCar ' ]); } }
ãã¡ãããå®éã®ã³ãŒãã§ã¯ã $request->getParsedBody()['name']
ãnew JsonResponse
ãè¿œå ã®ãã§ãã¯ã§å¥ã®æœè±¡åã¬ãã«ã«ç§»åããŸãã
ããŸããšããŠïŒç°å¢ã«ãã£ãŠã¯ãã³ã³ããå ã®ãµãŒãã¹ã眮ãæããŠæ©èœãã¹ããå®è¡ããããšãã§ããŸãã
çµè«ãšããŠ
ã芧ã®ãšããããéåžžã«ç°¡åããšããã€ããªãã®ãŒãæã€å€ãã®ãã©ã¯ãã£ã¹ãLaravelããåçšãããŠããŸãã 圌ã¯æ¬åœã«ãã¬ã³ããèšå®ããŸãã
ã¢ããªã±ãŒã·ã§ã³ã¯ã7幎éåäœããŠããåŸãæ°ãããã¬ãŒã ã¯ãŒã¯ãåãåããŸããã ç§ã®ç¥ãéããå€ãèªå·±èšè¿°ãã¬ãŒã ã¯ãŒã¯ãããã«ã¯çŸããŸããã§ããã ãããŠã5幎åŸã«ãã¬ãŒã ã¯ãŒã¯ãå€æŽããããªããšããä¿èšŒã¯ãããŸããã ãããã£ãŠãã³ãŒãã¯éžæãããã¬ãŒã ã¯ãŒã¯ããå¯èœãªéãç¬ç«ããŠèšè¿°ãããŸããã ããžãã¹ããžãã¯ã¯ã以åã¯ã¢ããªã±ãŒã·ã§ã³ã«äŸåããŠããŸããã§ããããã³ã³ãããŒã©ãŒã¯ãã¬ãŒã ã¯ãŒã¯ã«äŸåããªããªããŸããã ã³ã³ãããŒã©ãŒã¯PSR-7äºæèŠæ±ã«äŸåããPSR-7å¿çãè¿ããŸãã ãŸããã³ã³ãããŒã©ãŒã¯PSR-11äºæã³ã³ãããŒã«å¿ããŠã¢ããªã±ãŒã·ã§ã³ã«ãã£ãŠçµã¿ç«ãŠãããŸãã
Slimã¯ããã«ãŠã§ã¢ãä»ããŠåäœããå ±éããžãã¯ã®è¿œå ãããç°¡åã«ãªããŸããïŒã¢ããªã±ãŒã·ã§ã³ãšã©ãŒã®ãã°èšé²ããŠãŒã¶ãŒå ¥åãšã©ãŒã®åŠçïŒã èªåé ç·ã³ã³ãããŒã©ãŒã¯éåžžã«æ©èœããŸããå®éãã³ã³ãããŒã©ãŒã¯ãµãŒãã¹ã«ãªããŸããã
ã¡ãªã¿ã«ã ããã§ã¯ã¹ãªã ã§èªåé ç·ãæå¹ã«ããäŸãèŠãããšãã§ããŸãã
ãã¡ãããã¢ããªã±ãŒã·ã§ã³ã¯é²åãç¶ããŠããããã§ã«æšæºã®ãã£ãã·ã¥ãšã€ãã³ãã®ã€ã³ã¿ãŒãã§ãŒã¹ã«åãæ¿ããŠããŸããããããã®å®è£ ã¯å°ãè¡ãŸã¿ãã§ãã:)ã