diff --git a/app/Controllers/Admin/AdminController.php b/app/Controllers/Admin/AdminController.php index aa42151..426c418 100644 --- a/app/Controllers/Admin/AdminController.php +++ b/app/Controllers/Admin/AdminController.php @@ -13,6 +13,6 @@ abstract class AdminController extends CommonController public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger) { parent::initController($request, $response, $logger); - $this->_paths[] = self::PATH; + $this->addActionPaths(self::PATH); } } diff --git a/app/Controllers/Admin/UserController.php b/app/Controllers/Admin/UserController.php index b04d9e4..71ee2e1 100644 --- a/app/Controllers/Admin/UserController.php +++ b/app/Controllers/Admin/UserController.php @@ -21,10 +21,11 @@ class UserController extends AdminController public const PATH = 'user'; public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger) { + parent::initController($request, $response, $logger); if ($this->service === null) { $this->service = service('localauth'); } - $this->_paths[] = self::PATH; + $this->addActionPaths(self::PATH); } protected function getFormRule(string $action, string $field, string $rule): array { diff --git a/app/Controllers/Auth/AuthController.php b/app/Controllers/Auth/AuthController.php index 9486a55..76f4197 100644 --- a/app/Controllers/Auth/AuthController.php +++ b/app/Controllers/Auth/AuthController.php @@ -16,7 +16,7 @@ abstract class AuthController extends CommonController public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger) { parent::initController($request, $response, $logger); - $this->_paths[] = self::PATH; + $this->addActionPaths(self::PATH); } abstract protected function login_process(): UserEntity; abstract protected function logout_process(): void; @@ -34,8 +34,8 @@ abstract class AuthController extends CommonController $viewDatas = $this->login_form_process($viewDatas); return $this->action_post_process($action, $viewDatas); } catch (\Exception $e) { - $viewDatas['error'] = $e->getMessage(); - //리디렉션 대신 폼 뷰를 다시 렌더링하도록 form_post_process 호출 + $viewDatas[self::ACTION_RESULT] = 'error'; + $viewDatas[self::ACTION_MESSAGE] = $e->getMessage(); return $this->action_post_process($action, $viewDatas); } } @@ -44,6 +44,8 @@ abstract class AuthController extends CommonController public function login(): RedirectResponse { try { + $action = __FUNCTION__; + $viewDatas = $this->action_init_process($action); $this->login_process(); return redirect()->to($this->authService->popPreviousUrl())->with('success', '로그인이 완료되었습니다.'); } catch (\Exception $e) { diff --git a/app/Controllers/Auth/GoogleController.php b/app/Controllers/Auth/GoogleController.php index cc5cd68..ade5554 100644 --- a/app/Controllers/Auth/GoogleController.php +++ b/app/Controllers/Auth/GoogleController.php @@ -20,8 +20,9 @@ class GoogleController extends AuthController if ($this->service === null) { $this->service = service('googleauth'); } + $this->addActionPaths(self::PATH); } - public function login_form_process(array $viewDatas): array + public function login_form_process(array $viewDatas = []): array { //구글 로그인 BUTTON용 $viewDatas['SNSButton'] = anchor($this->service->socket->createAuthUrl(), ICONS['GOOGLE'] . 'Google 로그인', ["class" => "btn-google"]); diff --git a/app/Controllers/Auth/LocalController.php b/app/Controllers/Auth/LocalController.php index 8994957..4721503 100644 --- a/app/Controllers/Auth/LocalController.php +++ b/app/Controllers/Auth/LocalController.php @@ -21,7 +21,7 @@ class LocalController extends AuthController if ($this->service === null) { $this->service = service('localauth'); } - $this->_paths[] = self::PATH; + $this->addActionPaths(self::PATH); } //로그인처리 protected function login_process(): UserEntity diff --git a/app/Controllers/CommonController.php b/app/Controllers/CommonController.php index 7af7e56..d98def7 100644 --- a/app/Controllers/CommonController.php +++ b/app/Controllers/CommonController.php @@ -19,11 +19,14 @@ use Psr\Log\LoggerInterface; abstract class CommonController extends BaseController { use LogTrait; + protected const ACTION_PATH = "action_path"; + protected const ACTION_VIEW_FILE = "action_view_file"; + protected const ACTION_RESULT = "action_result"; + protected const ACTION_MESSAGE = "action_message"; + private array $_action_paths = []; protected $authService = null; protected $service = null; protected $myAuth = null; - protected $action = null; - protected array $_paths = []; public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger) { parent::initController($request, $response, $logger); @@ -31,9 +34,13 @@ abstract class CommonController extends BaseController $this->authService = service('myauth'); $this->myAuth = AuthDTO::fromByAuthService($this->authService); } - final public function getPaths(): array + final public function addActionPaths(string $path) { - return $this->_paths; + $this->_action_paths[] = $path; + } + final public function getActionPaths(): array + { + return $this->_action_paths; } final protected function doValidation(string $action): array { @@ -63,24 +70,22 @@ abstract class CommonController extends BaseController protected function action_init_process(string $action, array $viewDatas = []): array { $viewDatas['action'] = $action; - $viewDatas['view_paths'] = $this->getPaths(); - $viewDatas['view_file'] = $this->request->getVar('ActionTemplate') ?? $viewDatas['action']; + $viewDatas[self::ACTION_PATH] = $this->getActionPaths(); + $viewDatas[self::ACTION_VIEW_FILE] = $this->request->getVar('ActionTemplate') ?? $viewDatas['action']; $viewDatas['formFields'] = $this->service->getFormFields(); $viewDatas['formFilters'] = $this->service->getFormFilters(); $viewDatas['formRules'] = $this->service->getFormRules($viewDatas['action']); $viewDatas['formOptions'] = $this->service->getFormOptions($viewDatas['action']); - if (array_key_exists('formDatas', $viewDatas)) { - $viewDatas['formDatas'] = []; - } return $viewDatas; } protected function action_post_process(string $action, array $viewDatas): string|RedirectResponse { - $view_paths = array_key_exists('view_paths', $viewDatas) ? $viewDatas['view_paths'] : $this->getPaths(); + $view_paths = array_key_exists(self::ACTION_PATH, $viewDatas) ? $viewDatas[self::ACTION_PATH] : $this->getActionPaths(); $lastest_path = array_pop($view_paths); //paths는 마지막을 뺀 앞단까지만 남음 switch ($action) { case 'create_form': case 'modify_form': + case 'login_form': // ✅ 중간 안내 화면으로 // return view('posts/success_redirect', [ // 'message' => '게시글이 성공적으로 등록되었습니다.', @@ -90,7 +95,7 @@ abstract class CommonController extends BaseController $full_path = implode(DIRECTORY_SEPARATOR, [ ...$view_paths, $this->request->getVar('ActionTemplate') ?? $lastest_path, - array_key_exists('view_file', $viewDatas) ? $viewDatas['view_file'] : $action + array_key_exists(self::ACTION_VIEW_FILE, $viewDatas) ? $viewDatas[self::ACTION_VIEW_FILE] : $action ]); $view_datas = [ 'control' => $viewDatas, @@ -102,10 +107,9 @@ abstract class CommonController extends BaseController default: // ✅ Flashdata로 성공 메시지 저장 // 일반 CRUD (create/update/delete) → Flashdata + redirect() - session()->setFlashdata('success', array_key_exists('message', $viewDatas) ?: static::class . "/{$action}이 완료되었습니다."); - $result = redirect()->route( - implode(DIRECTORY_SEPARATOR, $view_paths) - ); + $message = array_key_exists(self::ACTION_MESSAGE, $viewDatas) ?: static::class . "/{$action}이 완료되었습니다."; + session()->setFlashdata($viewDatas[self::ACTION_RESULT], $message); + $result = redirect()->route(implode(DIRECTORY_SEPARATOR, $view_paths))->with('message', $message); break; } return $result; @@ -119,12 +123,12 @@ abstract class CommonController extends BaseController try { //초기화 $action = __FUNCTION__; - $viewDatas = ['formDatas' => []]; - $viewDatas = $this->action_init_process($action, $viewDatas); + $viewDatas = $this->action_init_process($action); $viewDatas = $this->create_form_process($viewDatas); return $this->action_post_process($action, $viewDatas); } catch (\Exception $e) { - $viewDatas['error'] = $e->getMessage(); + $viewDatas[self::ACTION_RESULT] = 'error'; + $viewDatas[self::ACTION_MESSAGE] = $e->getMessage(); //리디렉션 대신 폼 뷰를 다시 렌더링하도록 form_post_process 호출 return $this->action_post_process($action, $viewDatas); } @@ -139,8 +143,7 @@ abstract class CommonController extends BaseController try { //초기화 $action = __FUNCTION__; - $viewDatas = ['formDatas' => $this->request->getPost()]; - $viewDatas = $this->action_init_process($action, $viewDatas); + $viewDatas = $this->action_init_process($action); return $this->create_process($action, $viewDatas); } catch (\Exception $e) { return redirect()->back()->withInput()->with('error', $e->getMessage());