controllers changes

This commit is contained in:
Yuri Kuznetsov
2013-11-27 13:07:09 +02:00
parent 1bb2ef6f36
commit 4692e4b447
5 changed files with 48 additions and 34 deletions
+18 -11
View File
@@ -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 = <<<EOT
<h1>EspoCRM REST API!!!</h1>
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',
+10 -7
View File
@@ -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'");
}
+3 -1
View File
@@ -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;
}
}
+16 -13
View File
@@ -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();
}
}
@@ -1,7 +1,6 @@
<?php
namespace Espo\Controllers;
namespace Espo\Modules\Crm\Controllers;
class Account extends \Espo\Core\Controllers\Record
{