trafficmonitor init...2

This commit is contained in:
최준흠 2025-11-06 01:30:14 +09:00
parent 2faa54edbb
commit f430183704
6 changed files with 34 additions and 27 deletions

View File

@ -13,6 +13,6 @@ abstract class AdminController extends CommonController
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger) public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
{ {
parent::initController($request, $response, $logger); parent::initController($request, $response, $logger);
$this->_paths[] = self::PATH; $this->addActionPaths(self::PATH);
} }
} }

View File

@ -21,10 +21,11 @@ class UserController extends AdminController
public const PATH = 'user'; public const PATH = 'user';
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger) public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
{ {
parent::initController($request, $response, $logger);
if ($this->service === null) { if ($this->service === null) {
$this->service = service('localauth'); $this->service = service('localauth');
} }
$this->_paths[] = self::PATH; $this->addActionPaths(self::PATH);
} }
protected function getFormRule(string $action, string $field, string $rule): array protected function getFormRule(string $action, string $field, string $rule): array
{ {

View File

@ -16,7 +16,7 @@ abstract class AuthController extends CommonController
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger) public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
{ {
parent::initController($request, $response, $logger); parent::initController($request, $response, $logger);
$this->_paths[] = self::PATH; $this->addActionPaths(self::PATH);
} }
abstract protected function login_process(): UserEntity; abstract protected function login_process(): UserEntity;
abstract protected function logout_process(): void; abstract protected function logout_process(): void;
@ -34,8 +34,8 @@ abstract class AuthController extends CommonController
$viewDatas = $this->login_form_process($viewDatas); $viewDatas = $this->login_form_process($viewDatas);
return $this->action_post_process($action, $viewDatas); return $this->action_post_process($action, $viewDatas);
} catch (\Exception $e) { } catch (\Exception $e) {
$viewDatas['error'] = $e->getMessage(); $viewDatas[self::ACTION_RESULT] = 'error';
//리디렉션 대신 폼 뷰를 다시 렌더링하도록 form_post_process 호출 $viewDatas[self::ACTION_MESSAGE] = $e->getMessage();
return $this->action_post_process($action, $viewDatas); return $this->action_post_process($action, $viewDatas);
} }
} }
@ -44,6 +44,8 @@ abstract class AuthController extends CommonController
public function login(): RedirectResponse public function login(): RedirectResponse
{ {
try { try {
$action = __FUNCTION__;
$viewDatas = $this->action_init_process($action);
$this->login_process(); $this->login_process();
return redirect()->to($this->authService->popPreviousUrl())->with('success', '로그인이 완료되었습니다.'); return redirect()->to($this->authService->popPreviousUrl())->with('success', '로그인이 완료되었습니다.');
} catch (\Exception $e) { } catch (\Exception $e) {

View File

@ -20,8 +20,9 @@ class GoogleController extends AuthController
if ($this->service === null) { if ($this->service === null) {
$this->service = service('googleauth'); $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용 //구글 로그인 BUTTON용
$viewDatas['SNSButton'] = anchor($this->service->socket->createAuthUrl(), ICONS['GOOGLE'] . 'Google 로그인', ["class" => "btn-google"]); $viewDatas['SNSButton'] = anchor($this->service->socket->createAuthUrl(), ICONS['GOOGLE'] . 'Google 로그인', ["class" => "btn-google"]);

View File

@ -21,7 +21,7 @@ class LocalController extends AuthController
if ($this->service === null) { if ($this->service === null) {
$this->service = service('localauth'); $this->service = service('localauth');
} }
$this->_paths[] = self::PATH; $this->addActionPaths(self::PATH);
} }
//로그인처리 //로그인처리
protected function login_process(): UserEntity protected function login_process(): UserEntity

View File

@ -19,11 +19,14 @@ use Psr\Log\LoggerInterface;
abstract class CommonController extends BaseController abstract class CommonController extends BaseController
{ {
use LogTrait; 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 $authService = null;
protected $service = null; protected $service = null;
protected $myAuth = null; protected $myAuth = null;
protected $action = null;
protected array $_paths = [];
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger) public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
{ {
parent::initController($request, $response, $logger); parent::initController($request, $response, $logger);
@ -31,9 +34,13 @@ abstract class CommonController extends BaseController
$this->authService = service('myauth'); $this->authService = service('myauth');
$this->myAuth = AuthDTO::fromByAuthService($this->authService); $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 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 protected function action_init_process(string $action, array $viewDatas = []): array
{ {
$viewDatas['action'] = $action; $viewDatas['action'] = $action;
$viewDatas['view_paths'] = $this->getPaths(); $viewDatas[self::ACTION_PATH] = $this->getActionPaths();
$viewDatas['view_file'] = $this->request->getVar('ActionTemplate') ?? $viewDatas['action']; $viewDatas[self::ACTION_VIEW_FILE] = $this->request->getVar('ActionTemplate') ?? $viewDatas['action'];
$viewDatas['formFields'] = $this->service->getFormFields(); $viewDatas['formFields'] = $this->service->getFormFields();
$viewDatas['formFilters'] = $this->service->getFormFilters(); $viewDatas['formFilters'] = $this->service->getFormFilters();
$viewDatas['formRules'] = $this->service->getFormRules($viewDatas['action']); $viewDatas['formRules'] = $this->service->getFormRules($viewDatas['action']);
$viewDatas['formOptions'] = $this->service->getFormOptions($viewDatas['action']); $viewDatas['formOptions'] = $this->service->getFormOptions($viewDatas['action']);
if (array_key_exists('formDatas', $viewDatas)) {
$viewDatas['formDatas'] = [];
}
return $viewDatas; return $viewDatas;
} }
protected function action_post_process(string $action, array $viewDatas): string|RedirectResponse 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는 마지막을 뺀 앞단까지만 남음 $lastest_path = array_pop($view_paths); //paths는 마지막을 뺀 앞단까지만 남음
switch ($action) { switch ($action) {
case 'create_form': case 'create_form':
case 'modify_form': case 'modify_form':
case 'login_form':
// ✅ 중간 안내 화면으로 // ✅ 중간 안내 화면으로
// return view('posts/success_redirect', [ // return view('posts/success_redirect', [
// 'message' => '게시글이 성공적으로 등록되었습니다.', // 'message' => '게시글이 성공적으로 등록되었습니다.',
@ -90,7 +95,7 @@ abstract class CommonController extends BaseController
$full_path = implode(DIRECTORY_SEPARATOR, [ $full_path = implode(DIRECTORY_SEPARATOR, [
...$view_paths, ...$view_paths,
$this->request->getVar('ActionTemplate') ?? $lastest_path, $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 = [ $view_datas = [
'control' => $viewDatas, 'control' => $viewDatas,
@ -102,10 +107,9 @@ abstract class CommonController extends BaseController
default: default:
// ✅ Flashdata로 성공 메시지 저장 // ✅ Flashdata로 성공 메시지 저장
// 일반 CRUD (create/update/delete) → Flashdata + redirect() // 일반 CRUD (create/update/delete) → Flashdata + redirect()
session()->setFlashdata('success', array_key_exists('message', $viewDatas) ?: static::class . "/{$action}이 완료되었습니다."); $message = array_key_exists(self::ACTION_MESSAGE, $viewDatas) ?: static::class . "/{$action}이 완료되었습니다.";
$result = redirect()->route( session()->setFlashdata($viewDatas[self::ACTION_RESULT], $message);
implode(DIRECTORY_SEPARATOR, $view_paths) $result = redirect()->route(implode(DIRECTORY_SEPARATOR, $view_paths))->with('message', $message);
);
break; break;
} }
return $result; return $result;
@ -119,12 +123,12 @@ abstract class CommonController extends BaseController
try { try {
//초기화 //초기화
$action = __FUNCTION__; $action = __FUNCTION__;
$viewDatas = ['formDatas' => []]; $viewDatas = $this->action_init_process($action);
$viewDatas = $this->action_init_process($action, $viewDatas);
$viewDatas = $this->create_form_process($viewDatas); $viewDatas = $this->create_form_process($viewDatas);
return $this->action_post_process($action, $viewDatas); return $this->action_post_process($action, $viewDatas);
} catch (\Exception $e) { } catch (\Exception $e) {
$viewDatas['error'] = $e->getMessage(); $viewDatas[self::ACTION_RESULT] = 'error';
$viewDatas[self::ACTION_MESSAGE] = $e->getMessage();
//리디렉션 대신 폼 뷰를 다시 렌더링하도록 form_post_process 호출 //리디렉션 대신 폼 뷰를 다시 렌더링하도록 form_post_process 호출
return $this->action_post_process($action, $viewDatas); return $this->action_post_process($action, $viewDatas);
} }
@ -139,8 +143,7 @@ abstract class CommonController extends BaseController
try { try {
//초기화 //초기화
$action = __FUNCTION__; $action = __FUNCTION__;
$viewDatas = ['formDatas' => $this->request->getPost()]; $viewDatas = $this->action_init_process($action);
$viewDatas = $this->action_init_process($action, $viewDatas);
return $this->create_process($action, $viewDatas); return $this->create_process($action, $viewDatas);
} catch (\Exception $e) { } catch (\Exception $e) {
return redirect()->back()->withInput()->with('error', $e->getMessage()); return redirect()->back()->withInput()->with('error', $e->getMessage());