diff --git a/application/Espo/Core/Application.php b/application/Espo/Core/Application.php index 81c944b19e..b1a5412f34 100644 --- a/application/Espo/Core/Application.php +++ b/application/Espo/Core/Application.php @@ -116,14 +116,14 @@ class Application $controllerParams[$key] = $value; } - $controllerName = ucfirst($controllerParams['controller']); + $controllerName = ucfirst($controllerParams['controller']); if (!empty($controllerParams['action'])) { $actionName = $controllerParams['action']; } else { $httpMethod = strtolower($slim->request()->getMethod()); $actionName = $container->get('config')->get('crud')->$httpMethod; - } + } try { $controllerManager = new \Espo\Core\ControllerManager($container, $serviceFactory); @@ -164,9 +164,7 @@ class Application ); $this->getSlim()->get('/', function() { - return $template = <<EspoCRM REST API!!! -EOT; + return "EspoCRM REST API"; }); $this->getSlim()->get('/app/user/', function() { @@ -209,14 +207,13 @@ EOT; 'scope' => ':controller', ); })->via('PATCH'); - - - - /*$this->getSlim()->get('/:controller/:id', function() { + + + + $this->getSlim()->get('/:controller', function() { return array( 'controller' => ':controller', - 'action' => 'read', - 'id' => ':id' + 'action' => 'index', ); }); @@ -227,6 +224,16 @@ EOT; ); }); + /*$this->getSlim()->get('/:controller/:id', function() { + return array( + 'controller' => ':controller', + 'action' => 'read', + 'id' => ':id' + ); + }); + + + $this->getSlim()->put('/:controller/:id', function() { return array( 'controller' => ':controller', diff --git a/application/Espo/Core/ControllerManager.php b/application/Espo/Core/ControllerManager.php index 9f77ce2040..5b325dbdc8 100644 --- a/application/Espo/Core/ControllerManager.php +++ b/application/Espo/Core/ControllerManager.php @@ -46,13 +46,18 @@ class ControllerManager } else { $controllerClassName = '\\Espo\\Controllers\\' . $controllerName; } - } + } + if (!class_exists($controllerClassName)) { throw new NotFound("Controller '$controllerName' is not found"); - } + } - $controller = new $controllerClassName($this->container, $this->serviceFactory); + $controller = new $controllerClassName($this->container, $this->serviceFactory); + + if ($actionName == 'index') { + $actionName = $controller->defaultAction; + } $actionNameUcfirst = ucfirst($actionName); @@ -60,11 +65,9 @@ class ControllerManager if (method_exists($controller, $beforeMethodName)) { $controller->$beforeMethodName($params, $data); } - $actionMethodName = 'action' . $actionNameUcfirst; + $actionMethodName = 'action' . $actionNameUcfirst; - - if (!method_exists($controller, $actionMethodName)) { - + if (!method_exists($controller, $actionMethodName)) { throw new NotFound("Action '$actionMethodName' does not exist in controller '$controller'"); } diff --git a/application/Espo/Core/Controllers/Base.php b/application/Espo/Core/Controllers/Base.php index d94d38ffe5..0ded5fe233 100644 --- a/application/Espo/Core/Controllers/Base.php +++ b/application/Espo/Core/Controllers/Base.php @@ -16,6 +16,8 @@ abstract class Base protected $serviceClassName = null; private $service = null; + + public $defaultAction = 'index'; public function __construct(Container $container, ServiceFactory $serviceFactory) { @@ -84,5 +86,5 @@ abstract class Base $this->loadService(); return $this->service; } - + } diff --git a/application/Espo/Core/Controllers/Record.php b/application/Espo/Core/Controllers/Record.php index ca6b927016..e1fae551ed 100644 --- a/application/Espo/Core/Controllers/Record.php +++ b/application/Espo/Core/Controllers/Record.php @@ -9,13 +9,15 @@ abstract class Record extends Base { protected $serviceClassName = '\\Espo\\Services\\Record'; + public $defaultAction = 'list'; + protected function loadService() { parent::loadService(); $this->service->setEntityName($this->name); } - protected function actionRead($params) + public function actionRead($params) { $id = $params['id']; $service = $this->getService(); @@ -28,7 +30,7 @@ abstract class Record extends Base return $entity; } - protected function actionUpdate($params, $data) + public function actionUpdate($params, $data) { $id = $params['id']; $service = $this->getService(); @@ -45,7 +47,7 @@ abstract class Record extends Base throw new Error(); } - protected function actionPost($params, $data) + public function actionPost($params, $data) { if (!$this->getAcl()->check($this->name, 'edit')) { throw new Forbidden(); @@ -60,7 +62,7 @@ abstract class Record extends Base throw new Error(); } - protected function actionList($params, $where) + public function actionList($params, $where) { if (!$this->getAcl()->check($this->name, 'read')) { throw new Forbidden(); @@ -73,18 +75,18 @@ abstract class Record extends Base $asc = $data['asc']; $sortBy = $data['sortBy']; - $entityList = $service->findEntities({ + $entityList = $service->findEntities(array( 'where' => $where, 'offset' => $offset, 'limit' => $limit, 'asc' => $asc, 'sortBy' => $sortBy, - }); + )); return $entityList; } - protected function actionDelete($params) + public function actionDelete($params) { $id = $params['id']; @@ -101,7 +103,7 @@ abstract class Record extends Base throw new Error(); } - protected function actionMassUpdate($params, $data) + public function actionMassUpdate($params, $data) { if (!$this->getAcl()->check($this->name, 'edit')) { throw new Forbidden(); @@ -116,7 +118,7 @@ abstract class Record extends Base return $idsUpdated; } - protected function actionMassDelete($params, $data) + public function actionMassDelete($params, $data) { if (!$this->getAcl()->check($this->name, 'delete')) { throw new Forbidden(); @@ -131,7 +133,7 @@ abstract class Record extends Base return $idsDeleted; } - protected function actionListLinked($params, $data) + public function actionListLinked($params, $data) { $id = $params['id']; $link = $params['link']; @@ -153,18 +155,18 @@ abstract class Record extends Base $asc = $data['asc']; $sortBy = $data['sortBy']; - $entityList = $service->findLinkedEntities($entity, $link, { + $entityList = $service->findLinkedEntities($entity, $link, array( 'where' => $where, 'offset' => $offset, 'limit' => $limit, 'asc' => $asc, 'sortBy' => $sortBy, - }); + )); return $entityList; } - protected function actionCreateLink($params) + public function actionCreateLink($params) { $id = $params['id']; $link = $params['link']; @@ -184,4 +186,5 @@ abstract class Record extends Base throw new Error(); } +} diff --git a/application/Espo/Controllers/Account.php b/application/Espo/Modules/Crm/Controllers/Account.php similarity index 61% rename from application/Espo/Controllers/Account.php rename to application/Espo/Modules/Crm/Controllers/Account.php index 73d6acbc73..e9260e7132 100644 --- a/application/Espo/Controllers/Account.php +++ b/application/Espo/Modules/Crm/Controllers/Account.php @@ -1,7 +1,6 @@