public function __construct(\Silex\Application $app)
 {
     $app->register(new \Silex\Provider\UrlGeneratorServiceProvider());
     $conn = new DataBaseHelper();
     $db = $conn->autoConnect();
     $view = new ViewHelper($app);
     $view->setPath(dirname(__DIR__) . "/Views");
     // A query está sendo feita aqui porque é utilizadas em todos os métodos
     $headings = $db::table("exemplos")->get();
     // Essa variavel é adicionada aqui pois ela será usada em todos os métodos
     $view->assign(array("headings" => $headings));
     // Aqui ficarão as rotas/controllers desse módulo
     //=========== Home ===========//
     $app->get('/exemplo', function () use($app, $view) {
         $view->render("exemplo");
         return PHP_EOL;
     })->bind("exemplo_home");
     //=========== Heading ===========//
     $app->get('/exemplo/heading/{id}', function (Request $request) use($app, $view, $db) {
         // Pega o Heading atual
         $actual = $db::table("exemplos")->find($request->get("id"));
         $view->assign(array("actual" => $actual));
         $view->render("exemplo");
         return PHP_EOL;
     })->bind("exemplo_heading");
     //=========== Login (GET) ===========//
     $app->get('/exemplo/login', function (Request $request) use($app, $view) {
         return $app->redirect($app['url_generator']->generate('exemplo_home'));
     })->bind("exemplo_login_get");
     //=========== Login (POST) ===========//
     $app->post('/exemplo/login', function (Request $request) use($app, $view) {
         $view->assign(array("nome" => $request->get("name")));
         $mail = new SwiftMailerHelper($app);
         $mail->autoConfig();
         $mail->setSubject("Teste de Helper [Silex]");
         $mail->setTo(array("*****@*****.**"));
         $mail->setTitle("Testando 1, 2, 3...");
         $mail->setBody("Ola mundo :)");
         $mail->setAttach(__DIR__ . "/Exemplo.php");
         $mail->send();
         $view->render("exemplo");
         return PHP_EOL;
     })->bind("exemplo_login");
 }
Example #2
0
 /**
  * @param \Silex\Application $app
  */
 protected function run(\Silex\Application $app)
 {
     $app->register(new \Silex\Provider\UrlGeneratorServiceProvider());
     $this->startRequestTime = $this->getStartRequestTime();
     $conn = new DataBaseHelper();
     $db = $conn->autoConnect();
     $query = $db::table($this->dbTable);
     // Obtem todos os registros no banco
     // Opicional: Passa paramentro "page" para obter informações paginadas
     $app->get("/" . $this->uri, function (\Silex\Application $app, Request $request) use($db, $query) {
         if (!is_null($request->get('page'))) {
             $query->limit($this->perPage)->offset($request->get('page'));
         }
         $fetch = $query->get();
         $total = $db::table($this->dbTable)->count();
         $res = array();
         $res['data'] = $fetch;
         $res['total'] = $total;
         if (!is_null($request->get('page'))) {
             if (count($fetch) == $this->perPage && ($request->get('page') + 1) * $this->perPage < $total) {
                 $res['next_page'] = sprintf("%s://%s%s?page=%s", isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off' ? 'https' : 'http', $_SERVER['SERVER_NAME'], $app['url_generator']->generate($this->uri . "_fetch_all"), (int) $request->get('page') + 1);
             }
             if ((int) $request->get('page') > 0) {
                 $res['prev_page'] = sprintf("%s://%s%s?page=%s", isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off' ? 'https' : 'http', $_SERVER['SERVER_NAME'], $app['url_generator']->generate($this->uri . "_fetch_all"), (int) $request->get('page') - 1);
             }
             $res['first_page'] = sprintf("%s://%s%s?page=0", isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off' ? 'https' : 'http', $_SERVER['SERVER_NAME'], $app['url_generator']->generate($this->uri . "_fetch_all"));
             $res['last_page'] = sprintf("%s://%s%s?page=%s", isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off' ? 'https' : 'http', $_SERVER['SERVER_NAME'], $app['url_generator']->generate($this->uri . "_fetch_all"), $total / $this->perPage - 1);
         }
         $res['time_request'] = $this->getFinishRequestTime($this->startRequestTime);
         return $app->json($res);
     })->bind($this->uri . "_fetch_all");
     // Obtem registro via ID
     // Obs: A chave primaria da tabela deve ter o nome "id"
     $app->get("/" . $this->uri . "/{id}", function (\Silex\Application $app, $id) use($db, $query) {
         $fetch = $query->find($id);
         foreach ($this->foreignKey as $field => $table) {
             if (isset($fetch->{$field})) {
                 $fetch->{$field} = $db::table($table)->find($fetch->{$field});
             }
         }
         $res = array();
         $res['data'] = $fetch;
         $res['time_request'] = $this->getFinishRequestTime($this->startRequestTime);
         return $app->json($res);
     })->bind($this->uri . "_fetch_one");
     // Insere os dados enviados no banco de dados
     $app->post("/" . $this->uri, function (\Silex\Application $app, Request $request) use($db, $query) {
         $post = $request->request->all();
         $val = $this->runValidation($app, $post);
         if ($val['error']) {
             return $app->json($val);
         }
         $insert = $query->insert($val);
         $res = array();
         $res['data'] = $db::table($this->dbTable)->find($insert);
         $res['insertID'] = $insert;
         $res['time_request'] = $this->getFinishRequestTime($this->startRequestTime);
         return $app->json($res);
     })->bind($this->uri . "_insert");
     // Atualiza os dados do banco
     $app->put("/" . $this->uri . "/{id}", function (\Silex\Application $app, Request $request, $id) use($db, $query) {
         $post = $request->request->all();
         $val = $this->runValidation($app, $post);
         if ($val['error']) {
             return $app->json($val);
         }
         $query->where("id", $id)->update($val);
         $res = array();
         $res['data'] = $db::table($this->dbTable)->find($id);
         $res['time_request'] = $this->getFinishRequestTime($this->startRequestTime);
         return $app->json($res);
     })->bind($this->uri . "_update");
     // Apaga um registro do banco de dados
     $app->delete("/" . $this->uri . "/{id}", function (\Silex\Application $app, $id) use($query) {
         $query->where('id', $id)->delete();
         $res = array();
         $res['time_request'] = $this->getFinishRequestTime($this->startRequestTime);
         return $app->json($res);
     })->bind($this->uri . "_delete");
     // Realiza uma busca no banco de dados na tabela especifica utilizando o método LIKE.
     // Os parametros para busca devem ser passado via Query String. Ex: email=gmail&nome=kayo
     $app->get("/search/" . $this->uri, function (\Silex\Application $app, Request $request) use($db, $query) {
         $params = $request->query->all();
         foreach ($params as $field => $value) {
             if ($field == "limit") {
                 $query->limit($value);
             } else {
                 $exp = StringHelper::extractKeyWords($value);
                 foreach ($exp as $val) {
                     $query->orWhere($field, 'LIKE', "%" . $val . "%");
                 }
             }
         }
         $fetch = $query->get();
         $total = $db::table($this->dbTable)->count();
         $res = array();
         $res['data'] = $fetch;
         $res['total'] = $total;
         $res['time_request'] = $this->getFinishRequestTime($this->startRequestTime);
         return $app->json($res);
     })->bind($this->uri . "_search");
 }