[FastRoute] 파일 분리

2020. 1. 7. 01:08FastRoute (PHP)

1. FastRoute 파일 분리 (index.php)

FastRoute 구조에서 파일들을 세분화하여 분리하기 위해서는 index 파일에서 URI의 정보를 추가하여 분리한다. (bigCategory 폴더와 smallCategory 폴더내에 각각의 용도에 맞게 다르게 수행하는 controller파일이 있다고 가정)

//bigCategory내의 mainController의 test
$r->addRoute('POST', '/test', ['bigCategory','mainController','test']);
switch ($routeInfo[0]) {
	case FastRoute\Dispatcher::NOT_FOUND:
		// ... 404 Not Found
		echo "404 Not Found";
		break;
	case FastRoute\Dispatcher::METHOD_NOT_ALLOWED:
		$allowedMethods = $routeInfo[1];
		// ... 405 Method Not Allowed
		echo "405 Method Not Allowed";
		break;
	case FastRoute\Dispatcher::FOUND:
		$handler = $routeInfo[1];
		$vars = $routeInfo[2];
        
		if($routeInfo[1][0] == 'bigCategory') {
			switch ($routeInfo[1][1]) {
				case 'mainController':
					$handler = $routeInfo[1][2]; $vars = $routeInfo[2];
					require './controller/mainController.php';
 					break;
 			}
		}
		require './controller/mainController.php';

		break;
}

index.php
Postman (POST 방식)

728x90

'FastRoute (PHP)' 카테고리의 다른 글