private function __construct() { // building data source name from config $dsn = 'mysql:host=' . Config::read('db.host') . ';dbname=' . Config::read('db.basename') . ';port=' . Config::read('db.port') . ';connect_timeout=15'; // getting DB user from config $user = Config::read('db.user'); // getting DB password from config $password = Config::read('db.password'); $this->dbh = new PDO($dsn, $user, $password); }
private function __construct() { // building data source name from config $dsn = 'mysql:host=' . Config::read('db.host') . ';dbname=' . Config::read('db.basename') . ';charset=' . Config::read("db.charset"); // getting DB user from config $user = Config::read('db.user'); // getting DB password from config $password = Config::read('db.password'); $this->dbh = new PDO($dsn, $user, $password); $this->dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); }
/** * コンストラクタ * 設定ファイルはrootに配置されているconfig.php */ private function __construct() { // building data source name from config $dsn = 'mysql:host=' . Config::read('db.host') . ';dbname=' . Config::read('db.basename') . ';port=' . Config::read('db.port') . ';connect_timeout=15'; // getting DB user from config $user = Config::read('db.user'); // getting DB password from config $password = Config::read('db.password'); $options = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES ' . Config::read('db.encode')); $this->dbh = new PDO($dsn, $user, $password, $options); }
public function request($method, $path, $options = array()) { // Capture STDOUT ob_start(); // Prepare a mock environment \Slim\Environment::mock(array_merge(array('REQUEST_METHOD' => $method, 'SERVER_PORT' => Config::read('port_project'), 'PATH_INFO' => $path, 'SERVER_NAME' => 'http://localhost:9076'), $options)); // Run the application require __DIR__ . '/../../www/bootstrap.php'; $this->app = $app; $this->request = $app->request(); $this->response = $app->response(); // We fire the routes $this->app->run(); // Return STDOUT return ob_get_clean(); }
// Now we know $key is valid, we can assign it // We use a helper to assist us in the different use cases we can // encounter where the type of input we want to create gives as the value in a format // different than we want to store (e.g. Checkbox gives 'on' and we want to store 1) $value_inserted = DbConversor::convert($form_field->getTypeString(), $var); $response->{$key} = $value_inserted; // We also add it to our parameters array for it to be send to the contact by // e-mail $parameters['fields'][$key] = $value_inserted; // And the field names $parameters['field_names'][$key] = $form_field->field_name; } $response->save(); // Aaaand, we send emails to all the contacts of that form $twig = $app->view()->getEnvironment(); // twig environment $transport = Swift_MailTransport::newInstance(); // Create the Mailer using your created Transport $mailer = Swift_Mailer::newInstance($transport); $parameters['form'] = $form; foreach ($contacts as $contact) { // Contact to be accessible from the template $parameters['contact'] = $contact; $generator = new Email($twig); $message = $generator->getMessage(Config::read('email_template'), $parameters); $message->setTo($contact->contact_email); $message->setFrom(Config::read('email_from')); $mailer->send($message); } $app->redirect($form->redirect); });
<?php use lib\Config; use Illuminate\Database\Capsule\Manager as DB; // /render/:id controller // Render a form // When accessing /render/:id via GET, // the form will be rendered $app->get('/render/:id', function ($id) use($app) { $c = array(); // We grab the form $form = models\Form::find($id); // We grab its fields $fields = $form->fields; // Base path $c['base_path'] = Config::read('base_path'); $c['form'] = $form; $c['fields'] = $fields; $app->render('pages/render.html', $c); })->name('render');
continue; } // We edit the field $contact = models\Contact::find($id_contact); $contact->{$column_name} = $var; $contact->save(); } $app->redirect($app->urlFor('getFormEdit', array('id' => $id))); })->name('postFormContactEdit'); // /form/edit/:id/fields controller // Edit Fields controller // Name and placeholders for the current fields are updated // type or number of fields cannot be updated for performance reasons $app->post('/form/edit/:id/fields', function ($id) use($app) { $c = array(); $allowed_field_names = Config::read('field_form_elements'); $post_vars = $app->request()->post(); // We iterate through id foreach ($post_vars as $key => $var) { // as keys are name_id $explode = explode('_', $key); $id_field = array_pop($explode); // column name can be placeholder or field_name $column_name = implode('_', $explode); // We try to avoid as possible possible Database mess if (!in_array($column_name, $allowed_field_names)) { continue; } // We edit the field $field = models\Field::find($id_field); $field->{$column_name} = $var;
<?php // This file is isolated in order for PHPUnit to work require_once __DIR__ . '/session_start.php'; use lib\Config; require_once __DIR__ . '/../vendor/autoload.php'; require_once __DIR__ . '/../config.php'; $twigView = new \Slim\Views\Twig(); //$twigView = new \Slim\Extras\Views\Twig(); $app = new \Slim\Slim(array('debug' => true, 'view' => $twigView, 'templates.path' => __DIR__ . '/../app/templates/')); // We set the content-type $app->contentType('text/html; charset=utf-8'); // We add the Auth middleware if (Config::read('auth_required')) { $app->add(new \MyMiddleware\Auth()); } // We extend TWIG defining generators for the dynamic form fields $twig = $app->view()->getEnvironment(); $twig->addGlobal('field_generator', new \generators\Field()); // We pass the base path to our templates $twig->addGlobal('base_path', Config::read('base_path')); // Automatically load router files $routers = glob(__DIR__ . '/../app/routers/*.router.php'); foreach ($routers as $router) { require $router; }
public function hash($password) { return Bcrypt::hashPassword($password . Config::read('salt')); }
require '../vendor/autoload.php'; require '../config.php'; use lib\Config; date_default_timezone_set('America/Sao_Paulo'); session_cache_limiter(false); session_start(); // Setup custom Twig view $twigView = new \Slim\Views\Twig(); $app = new \Slim\Slim(array('debug' => true, 'view' => $twigView, 'templates.path' => '../templates/')); // Automatically load router files $routers = glob('../routers/*.router.php'); foreach ($routers as $router) { require $router; } $app->url = Config::read('path'); $app->salt = Config::read('salt'); $app->hook('slim.before.dispatch', function () use($app) { $url = Config::read('path'); $app->view()->setData('url', $url); $app->view()->setData('session', $_SESSION); }); //MidleWare function auth() { if (!isset($_SESSION['user']['id'])) { $app = \Slim\Slim::getInstance(); $app->redirect($app->url . '/login'); } } $app->run();