lithium\data\Connections::get PHP Method

get() public static method

Usage: Gets the names of all available configurations $configurations = Connections::get(); Gets the configuration array for the connection named 'db' $config = Connections::get('db', array('config' => true)); Gets the instance of the connection object, configured with the settings defined for this object in Connections::add() $dbConnection = Connections::get('db'); Gets the connection object, but only if it has already been built. Otherwise returns null. $dbConnection = Connections::get('db', array('autoCreate' => false));
public static get ( string $name = null, array $options = [] ) : mixed
$name string The name of the connection to get, as defined in the first parameter of `add()`, when the connection was initially created.
$options array Options to use when returning the connection: - `'autoCreate'`: If `false`, the connection object is only returned if it has already been instantiated by a previous call. - `'config'`: If `true`, returns an array representing the connection's internal configuration, instead of the connection itself.
return mixed A configured instance of the connection, or an array of the configuration used.
    public static function get($name = null, array $options = array())
    {
        static $mockAdapter;
        $defaults = array('config' => false, 'autoCreate' => true);
        $options += $defaults;
        if ($name === false) {
            if (!$mockAdapter) {
                $class = Libraries::locate('data.source', 'Mock');
                $mockAdapter = new $class();
            }
            return $mockAdapter;
        }
        if (!$name) {
            return array_keys(static::$_configurations);
        }
        if (!isset(static::$_configurations[$name])) {
            return null;
        }
        if ($options['config']) {
            return static::_config($name);
        }
        $settings = static::$_configurations[$name];
        if (!isset($settings[0]['object']) && !$options['autoCreate']) {
            return null;
        }
        return static::adapter($name);
    }

Usage Example

Beispiel #1
0
 /**
  * Check request reCAPTCHA validity
  * This method return `true` or `false` after validation, and set error in
  * helper. If `true` error is set to null, otherwise `'incorrect-captcha-sol'`
  * 
  * Example:
  * {{{
  *		class YourController extends \lithium\action\Controller {
  *			public function index() {
  *				if ($this->request->data) {
  *					if (!Recaptcha::check($this->request)) {
  *						return;
  *					}
  *				}
  *			}
  *		}
  * }}}
  * @param object $request Pass request object to check method
  * @return boolean
  * @throws ConfigException
  * @throws RuntimeException 
  */
 public static function check(\lithium\net\http\Request $request)
 {
     $config = Libraries::get('li3_recaptcha', 'keys');
     if (!$config['private']) {
         throw new ConfigException('To use reCAPTCHA you must get API key from' . 'https://www.google.com/recaptcha/admin/create');
     }
     if (!$request->env('SERVER_ADDR')) {
         throw new RuntimeException('For security reasons, you must pass the remote ip to reCAPTCHA');
     }
     $data = array('privatekey' => $config['private'], 'remoteip' => $request->env('SERVER_ADDR'), 'challenge' => null, 'response' => null);
     if ($request->data) {
         $data['challenge'] = $request->data['recaptcha_challenge_field'];
         $data['response'] = $request->data['recaptcha_response_field'];
     }
     if (!$data['challenge'] || !$data['response']) {
         RecaptchaHelper::$error = 'incorrect-captcha-sol';
         return false;
     }
     $service = Connections::get('recaptcha');
     $serviceRespond = explode("\n", $service->post('/recaptcha/api/verify', $data));
     if ($serviceRespond[0] == 'true') {
         RecaptchaHelper::$error = null;
         return true;
     } else {
         RecaptchaHelper::$error = 'incorrect-captcha-sol';
         return false;
     }
 }
All Usage Examples Of lithium\data\Connections::get