墨香年少 32 发布于 3月20日 // src/Service/PaymentLogService.php namespace App\Service; class PaymentLogService { private $logDirectory; public function __construct(string $logDirectory) { $this->logDirectory = $logDirectory; } public function appendToPaymentLog($pno, $content): void { $filePath = $this->logDirectory . '/' . $pno; // 确保目录存在 if (!file_exists(dirname($filePath))) { mkdir(dirname($filePath), 0777, true); } // 追加内容到文件,如果文件不存在则创建 file_put_contents($filePath, $content, FILE_APPEND); } } # config/services.yaml services: App\Service\PaymentLogService: arguments: $logDirectory: '%kernel.project_dir%/log' // src/Controller/SomeController.php namespace App\Controller; use Symfony\Component\HttpFoundation\Response; use App\Service\PaymentLogService; class SomeController { private $paymentLogService; public function __construct(PaymentLogService $paymentLogService) { $this->paymentLogService = $paymentLogService; } public function appendLogAction($pno): Response { $content = date('Y-m-d H:i:s') . "\n"; $this->paymentLogService->appendToPaymentLog($pno, $content); return new Response('Log updated.'); } } 目之所及,皆是回忆,心之所想,皆是过往 分享这篇帖子 链接帖子 分享到其他站点