Skip to content

Commit cd726ae

Browse files
committed
Inicializando
0 parents  commit cd726ae

File tree

15 files changed

+525
-0
lines changed

15 files changed

+525
-0
lines changed

application/Bootstrap.php

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
4+
{
5+
6+
protected function _initDatabase()
7+
{
8+
$this->bootstrap('db');
9+
$db = $this->getResource('db');
10+
Zend_Registry::set('db', $db);
11+
Zend_Db_Table::setDefaultAdapter($db);
12+
}
13+
14+
protected function _initRestRoute()
15+
{
16+
$this->bootstrap('frontController');
17+
$frontController = Zend_Controller_Front::getInstance();
18+
$restRoute = new Zend_Rest_Route($frontController);
19+
$frontController->getRouter()->addRoute('default', $restRoute);
20+
}
21+
22+
}
23+

application/configs/application.ini

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
[production]
2+
phpSettings.display_startup_errors = 0
3+
phpSettings.display_errors = 0
4+
includePaths.library = APPLICATION_PATH "/../library"
5+
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
6+
bootstrap.class = "Bootstrap"
7+
appnamespace = "Application"
8+
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
9+
resources.frontController.params.displayExceptions = 0
10+
11+
resources.db.adapter = "PDO_MYSQL"
12+
resources.db.params.host = "localhost"
13+
resources.db.params.username = "rest_user"
14+
resources.db.params.password = "restpass"
15+
resources.db.params.dbname = "kadabra"
16+
resources.db.params.charset = "utf8"
17+
18+
[staging : production]
19+
20+
[testing : production]
21+
phpSettings.display_startup_errors = 1
22+
phpSettings.display_errors = 1
23+
24+
[development : production]
25+
phpSettings.display_startup_errors = 1
26+
phpSettings.display_errors = 1
27+
resources.frontController.params.displayExceptions = 1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
class ErrorController extends Zend_Controller_Action
4+
{
5+
6+
public function errorAction()
7+
{
8+
$errors = $this->_getParam('error_handler');
9+
10+
if (!$errors || !$errors instanceof ArrayObject) {
11+
$this->view->message = 'You have reached the error page';
12+
return;
13+
}
14+
15+
switch ($errors->type) {
16+
case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ROUTE:
17+
case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
18+
case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:
19+
// 404 error -- controller or action not found
20+
$this->getResponse()->setHttpResponseCode(404);
21+
$priority = Zend_Log::NOTICE;
22+
$this->view->message = 'Page not found';
23+
break;
24+
default:
25+
// application error
26+
$this->getResponse()->setHttpResponseCode(500);
27+
$priority = Zend_Log::CRIT;
28+
$this->view->message = 'Application error';
29+
break;
30+
}
31+
32+
// Log exception, if logger available
33+
if ($log = $this->getLog()) {
34+
$log->log($this->view->message, $priority, $errors->exception);
35+
$log->log('Request Parameters', $priority, $errors->request->getParams());
36+
}
37+
38+
// conditionally display exceptions
39+
if ($this->getInvokeArg('displayExceptions') == true) {
40+
$this->view->exception = $errors->exception;
41+
}
42+
43+
$this->view->request = $errors->request;
44+
}
45+
46+
public function getLog()
47+
{
48+
$bootstrap = $this->getInvokeArg('bootstrap');
49+
if (!$bootstrap->hasResource('Log')) {
50+
return false;
51+
}
52+
$log = $bootstrap->getResource('Log');
53+
return $log;
54+
}
55+
56+
57+
}
58+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
class IndexController extends Zend_Controller_Action
4+
{
5+
6+
public function init()
7+
{
8+
/* Initialize action controller here */
9+
}
10+
11+
public function indexAction()
12+
{
13+
// action body
14+
}
15+
16+
17+
}
18+
+204
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
<?php
2+
3+
class UsersController extends Zend_Rest_Controller
4+
{
5+
/**
6+
* Model for current Controller
7+
* @var Zend_Db_Table
8+
*/
9+
protected $_model;
10+
11+
/**
12+
* Request "data" parameter JSON decoded
13+
* @var string
14+
*/
15+
protected $_requestData;
16+
17+
/**
18+
* Get requestData
19+
*/
20+
public function getRequestData()
21+
{
22+
if (empty($this->_requestData)) {
23+
$this->setRequestData(Zend_Json::decode($this->getRequest()->getParam("data"), Zend_Json::TYPE_OBJECT));
24+
}
25+
26+
return $this->_requestData;
27+
}
28+
29+
/**
30+
* Sets requestData
31+
* @param $requestData string
32+
* @return UsersController
33+
*/
34+
public function setRequestData($requestData)
35+
{
36+
$this->_requestData = $requestData;
37+
return $this;
38+
}
39+
40+
/**
41+
* Get model
42+
*
43+
* @return Zend_Db_Table
44+
*/
45+
public function getModel()
46+
{
47+
if (empty($this->_model)) {
48+
$this->setModel();
49+
}
50+
51+
return $this->_model;
52+
}
53+
54+
/**
55+
* Set model
56+
*
57+
* @param $model Zend_Db_Table
58+
* @return UsersController
59+
*/
60+
public function setModel($model)
61+
{
62+
if (empty($model)) {
63+
$model = new Application_Model_DbTable_Users();
64+
}
65+
66+
$this->_model = $model;
67+
return $this;
68+
}
69+
70+
/**
71+
* (non-PHPdoc)
72+
* @see library/Zend/Controller/Zend_Controller_Action::init()
73+
*/
74+
public function init()
75+
{
76+
$this->_helper->viewRenderer->setNoRender(true);
77+
}
78+
79+
/**
80+
* (non-PHPdoc)
81+
* @see library/Zend/Rest/Zend_Rest_Controller::indexAction()
82+
*/
83+
public function indexAction()
84+
{
85+
$response = new stdClass();
86+
$requestData = $this->getRequestData();
87+
88+
try {
89+
if (empty($requestData->start)) $requestData->start = 0;
90+
if (empty($requestData->count)) $requestData->count = 50;
91+
92+
$result = $this->getModel()->fetchAll(null, null, $requestData->count, $requestData->start);
93+
$response->data = $result->toArray();
94+
$response->success = true;
95+
}
96+
catch (Exception $e) {
97+
$response->success = false;
98+
$repsonse->messages = array($e->getMessage());
99+
}
100+
101+
$this->getResponse()->appendBody(Zend_Json::encode($response));
102+
}
103+
104+
/**
105+
* (non-PHPdoc)
106+
* @see library/Zend/Rest/Zend_Rest_Controller::getAction()
107+
*/
108+
public function getAction()
109+
{
110+
$response = new stdClass();
111+
$requestData = $this->getRequestData();
112+
113+
try {
114+
if (empty($requestData->id)) throw new Exception("msg_error_entry_id_must_be_provided");
115+
116+
$entry = $this->getModel()->find($requestData->id);
117+
118+
if (empty($entry)) throw new Exception("msg_error_entry_not_found");
119+
120+
$response->data = array($entry);
121+
$response->success = true;
122+
}
123+
catch (Exception $e) {
124+
$response->success = false;
125+
$response->messages = array($e->getMessage());
126+
}
127+
128+
$this->getResponse()->appendBody(Zend_Json::encode($response));
129+
}
130+
131+
/**
132+
* (non-PHPdoc)
133+
* @see library/Zend/Rest/Zend_Rest_Controller::postAction()
134+
*/
135+
public function postAction()
136+
{
137+
$response = new stdClass();
138+
$requestData = $this->getRequestData();
139+
140+
try {
141+
/* TODO: Check with staff what kind of validation must be done here */
142+
if (empty($requestData->entry)) throw new Exception("msg_error_entry_must_be_provided");
143+
$requestData->entry->id = $this->getModel()->insert(get_object_vars($requestData->entry));
144+
145+
$response->affectedRows = 1;
146+
$response->data = array($requestData->entry);
147+
$response->success = true;
148+
}
149+
catch (Exception $e) {
150+
$response->success = false;
151+
$response->messages = array($e->getMessage());
152+
}
153+
154+
$this->getResponse()->appendBody(Zend_Json::encode($response));
155+
}
156+
157+
/**
158+
* Update a record
159+
*/
160+
public function putAction()
161+
{
162+
$response = new stdClass();
163+
$requestData = $this->getRequestData();
164+
165+
try {
166+
/* TODO: Check with staff what kind of validation must be done here */
167+
if (empty($requestData->entry)) throw new Exception("msg_error_entry_must_be_provided");
168+
if (empty($requestData->entry->id)) throw new Exception("msg_error_entry_id_must_be_provided");
169+
170+
$response->affectedRows = $this->getModel()->update(get_object_vars($requestData->entry), array("id = ?" => $requestData->entry->id));
171+
$response->data = array($requestData->entry);
172+
$response->success = true;
173+
}
174+
catch (Exception $e) {
175+
$response->success = false;
176+
$response->messages = array($e->getMessage());
177+
}
178+
179+
$this->getResponse()->appendBody(Zend_Json::encode($response));
180+
}
181+
182+
/**
183+
* (non-PHPdoc)
184+
* @see library/Zend/Rest/Zend_Rest_Controller::deleteAction()
185+
*/
186+
public function deleteAction()
187+
{
188+
$response = new stdClass();
189+
$requestData = $this->getRequestData();
190+
191+
try {
192+
if (empty($requestData->id)) throw new Exception("msg_error_entry_id_must_be_provided");
193+
194+
$response->affectedRows = $this->getModel()->delete(array("id = ?" => $requestData->id));
195+
$response->success = true;
196+
}
197+
catch (Exception $e) {
198+
$response->success = false;
199+
$response->messages = array($e->getMessage());
200+
}
201+
202+
$this->getResponse()->appendBody(Zend_Json::encode($response));
203+
}
204+
}

application/models/DbTable/Users.php

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
class Application_Model_DbTable_Users extends Zend_Db_Table_Abstract
4+
{
5+
6+
protected $_name = 'users';
7+
8+
9+
}
10+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
2+
<html xmlns="http://www.w3.org/1999/xhtml">
3+
<head>
4+
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5+
<title>Zend Framework Default Application</title>
6+
</head>
7+
<body>
8+
<h1>An error occurred</h1>
9+
<h2><?php echo $this->message ?></h2>
10+
11+
<?php if (isset($this->exception)): ?>
12+
13+
<h3>Exception information:</h3>
14+
<p>
15+
<b>Message:</b> <?php echo $this->exception->getMessage() ?>
16+
</p>
17+
18+
<h3>Stack trace:</h3>
19+
<pre><?php echo $this->exception->getTraceAsString() ?>
20+
</pre>
21+
22+
<h3>Request Parameters:</h3>
23+
<pre><?php echo $this->escape(var_export($this->request->getParams(), true)) ?>
24+
</pre>
25+
26+
<?php endif ?>
27+
28+
</body>
29+
</html>

0 commit comments

Comments
 (0)