I just want to set the local environment into Laravel 4.
In bootstrap/start.php I have:
$env = $app->detectEnvironment(array(
'local' => ['laravel.dev', ''],
));
I tried change local to development index in array, but nothing works.
I tried some tips of this page: http://laravel.com/docs/configuration... nothing.
I'm using artisan in console, that always say me:
**************************************
* Application In Production! *
**************************************
Do you really wish to run this command?
What I might do to teach Lara that I'm on local environment?
You may try this (In bootstrap/start.php file):
$env = $app->detectEnvironment(array(
'local' => ['*.dev', gethostname()],
'production' => ['*.com', '*.net', '*.org']
));
Also this is possible:
$env = $app->detectEnvironment(function() {
return gethostname() == 'your local machine name' ? 'local' : 'production';
});
Following on from #The Alpha's great answer - here's a slight modification using array to check for local machines (when you work from more than one location):
$env = $app->detectEnvironment(function() {
return in_array(
gethostname(),
[
'first local machine name',
'second local machine name'
]
) ?
'local' :
'production';
});
$env = $app->detectEnvironment(function() {
$substr = substr(gethostname(), "-4");
return ($substr == ".com" || $substr == ".net" || $substr == ".org") ? 'production' : 'local';
});
Related
I have set up environment on my codeigniter project as development, production.
I have used this code.
switch($_SERVER["HTTP_HOST"])
{
case "localhost":
define('ENVIRONMENT', 'development');
break;
default:
define('ENVIRONMENT', 'production');
break;
}
Now i have to set different database connection for development and production.
Please help.
Database connections information defined in application/config/database.php file.
you can apply condition on bases of ENVIRONMENT
Open application/config/database.php file.
$db['database2'] = array(
'hostname' => (ENVIRONMENT !== 'production') ? 'localhost' : 'live_host',
'username' => (ENVIRONMENT !== 'production') ? 'root' : 'live_user',
'password' => (ENVIRONMENT !== 'production') ? 'local_password' : 'live_password',
'database' => (ENVIRONMENT !== 'production') ? 'local_db' : 'live_db',
'dbdriver' => 'mysqli',
.......
);
May it helps, Thanks
I installed and enabled APCu in my xampp server like:
downloaded file php_apcu.dll from (PHP 7: http://pecl.php.net/package/APCu/5.1.3/windows) and copy/paste it in the extension directory. also I enabled it from php.ini:
;;;;;;;;;;;;;;;;;;;;;;
; Dynamic Extensions ;
;;;;;;;;;;;;;;;;;;;;;;
[...]
extension=php_apcu.dll
so restart both apache server and symfony server.
phpinfo() show me that apc enabled correctly:
but symfony not recognized it:
I think your have successfully setup APC but symfony seems to somehow cache the results itself. Try following to see the updated status
depending on os restart apache (e.g on ubuntu/debian)
sudo service apache2 restart
cd to symfony project directory and clear cache
bin/console cache:clear
And reload the page, you should see the APC will be now green(hopefully)
The actual component class that is responsible to collect these states is
Symfony\Component\HttpKernel\DataCollector\ConfigDataCollector and as I browse through its not doing anything special when rendering those red/green boxes in browser, just checking if extension is loaded and its enabled e.g the collect function does look like:
/**
* {#inheritdoc}
*/
public function collect(Request $request, Response $response, \Exception $exception = null)
{
$this->data = array(
'app_name' => $this->name,
'app_version' => $this->version,
'token' => $response->headers->get('X-Debug-Token'),
'symfony_version' => Kernel::VERSION,
'symfony_state' => 'unknown',
'name' => isset($this->kernel) ? $this->kernel->getName() : 'n/a',
'env' => isset($this->kernel) ? $this->kernel->getEnvironment() : 'n/a',
'debug' => isset($this->kernel) ? $this->kernel->isDebug() : 'n/a',
'php_version' => PHP_VERSION,
'xdebug_enabled' => extension_loaded('xdebug'),
'eaccel_enabled' => extension_loaded('eaccelerator') && ini_get('eaccelerator.enable'),
'apc_enabled' => extension_loaded('apc') && ini_get('apc.enabled'),
'xcache_enabled' => extension_loaded('xcache') && ini_get('xcache.cacher'),
'wincache_enabled' => extension_loaded('wincache') && ini_get('wincache.ocenabled'),
'zend_opcache_enabled' => extension_loaded('Zend OPcache') && ini_get('opcache.enable'),
'bundles' => array(),
'sapi_name' => PHP_SAPI,
);
if (isset($this->kernel)) {
foreach ($this->kernel->getBundles() as $name => $bundle) {
$this->data['bundles'][$name] = $bundle->getPath();
}
$this->data['symfony_state'] = $this->determineSymfonyState();
}
}
The line in question is
'apc_enabled' => extension_loaded('apc') && ini_get('apc.enabled'),
I had same issue and my box is green after cache:clear. But there is more to it if you want to use apc caching e.g you have to enabled specifically for some component like doctrine and validations
I installed elasticsearch with the help of this link .
require 'vendor/autoload.php';
use Elasticsearch\ClientBuilder;
$client = ClientBuilder::create()->setHosts(["localhost:9200"])->build();
$response = '';
try{
$params = [
'index' => 'my_index',
'type' => 'my_type',
'id' => 'my_id',
'body' => ['testField' => 'abc']
];
$response = $client->indices()->create($params);
}catch(Exception $e){
echo "Exception : ".$e->getMessage();
}
print_r($response);
die('End : Elastic Search');
It returns me No alive nodes found in your cluster.
When I change the port to 80
$client = ClientBuilder::create()->setHosts(["localhost:80"])->build();
it gave me below error.
Method Not Allowed. The requested method PUT is not allowed for the URL /my_index.
check if your network is watching the ip of elastic service, if you are using docker, before composing container, check inside your web app server over shell if you are watching the ip of service and adding the ip in the .env file and execute again the docker compose.
in another cases check if you vps o development environment is pinging o watching the ip of elastic service
Did you try to restart the elasticsearch daemon? Like:
systemctl restart elasticsearch
Then, check if it's running:
systemctl status elasticsearch
If not, check the log files for any exceptions.
If you use Elasticsearch server in docker as the doc, https://www.elastic.co/guide/en/elasticsearch/reference/current/docker.html
But it uses a different network (networks: - esnet) with other services and it cannot talk to the application network. After remove the networks setting and it works well.
You mixed between index and document creation, first you need to create the index (like table in regular relational database), and then insert now document
require 'vendor/autoload.php';
use Elasticsearch\ClientBuilder;
$indexParams = [
'index' => 'my_index',
'body' => [
'settings' => [
'number_of_shards' => 5,
'number_of_replicas' => 1
]
]
];
$docParams = [
'index' => 'my_index',
'type' => 'my_type',
'id' => 'my_id',
'body' => ['testField' => 'abc']
];
$client = ClientBuilder::create()->setHosts(["localhost:9200"])->build();
$response = '';
try {
/* Create the index */
$response = $client->indices()->create($indexParams);
print_r($response);
/* put doc in the index */
$response = $client->index($docParams);
print_r($response);
} catch(Exception $e) {
echo "Exception : ".$e->getMessage();
}
die('End : Elastic Search');
set breackpoint on vendor\guzzlehttp\ringphp\src\Client\CurlHandler.php on method CurlHandler::_invokeAsArray and see $response
also use
$client = ClientBuilder::create()->setHosts([['host' =>self::$CONF['elasticSearchHost'],'port' => '80','scheme'=>'http']])->build();
My typical development pattern is:
1 - local work machine
2 - online testing server
3 - production server.
Each of these has a database with unique connection data.
So for my projects I use a config file that implements a switch( what_environment ){ // assign appropriate db connection info } that assigns the proper db connection info (host, username, password, db name, ...) depending on the environment.
However in codeigniter's /config/database.php, there is no such construct, but rather just one set of properties. Obviously I can put my own server-sniffing switch statement in there. But I am presuming that there is a codeigniter way of handling this.
Can anyone shed light on the proper codeigniter way of maintaining multiple db connection infos?
On your application/config/constants.php write:
define('ENVIRONMENT', 'development');
switch(ENVIRONMENT):
case 'development':
# DB
defined('DB_HOST') ? null : define('DB_HOST', 'localhost');
defined('DB_USER') ? null : define('DB_USER', 'root');
defined('DB_PASSWORD') ? null : define('DB_PASSWORD', '');
defined('DB_NAME') ? null : define('DB_NAME', 'dev_db');
break;
case 'production':
# DB
defined('DB_HOST') ? null : define('DB_HOST', 'production');
defined('DB_USER') ? null : define('DB_USER', 'production_user');
defined('DB_PASSWORD') ? null : define('DB_PASSWORD', '12345');
defined('DB_NAME') ? null : define('DB_NAME', 'production_db');
break;
endswitch;
Then on your application/config/database.php
$db['default'] = array(
'hostname' => DB_HOST,
'username' => DB_USER,
'password' => DB_PASSWORD,
'database' => DB_NAME,
);
I do not know if this is the best solution, however, it is the simplest to implement and always worked very well for me:
After the declaration of $db['default']:
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => '...',
'password' => '...',
...
);
I add this code:
if (ENVIRONMENT == 'development'){
$db['default']['username'] = 'root';
$db['default']['password'] = 'XXXXX';
$db['default']['database'] = 'developmentdatabasename';
}
In the index.php of your project:
define('ENVIRONMENT', isset($_SERVER['CI_ENV']) ? $_SERVER['CI_ENV'] : 'development');
if you use apache - activate SetEnvIf (if it itsn't already)
after that put in your htaccess
SetEnvIf Host localhost$ CI_ENV=development
SetEnvIf Host testhost$ CI_ENV=testing
SetEnvIf Host productionhost$ CI_ENV=production
now just simply create in your application/config/ folder 3 folder named development, testing and production
and if you put now your different config files in this folders - ci accesses it depending on your current working environment
please follow the CI Documentation for further Information
At your config/database.php you can set multiple environment like this
$active_group = 'local';
if(ENVIRONMENT=='development')
{
$active_group = 'online';
}
elseif(ENVIRONMENT=='production')
{
$active_group = 'production';
}
$db['local'] = array(
'dsn' => '',
'hostname' => 'xxxx',
'username' => '',
//other configs
);
$db['online'] = array(
'dsn' => '',
//other configs
);
$db['production'] = array(
'dsn' => '',
//other configs
);
You can do it lots of way.See documentation
I was using require-dev for my development tools dependencies. Like the laravel debugbar, the barryvdh ide-helper, etc...
When i get to my production server and run "composer update --no-dev --no-scripts" everything seems to be OK.
Then, you realize that you must remove your "dev providers" from the app.php array.
So whats the point of using require-dev? there isnt a "providers-dev" array?
UPDATE:
I was that i get it fixed but it's not working.
I create the file app/config/local/app.php with this:
<?php
return array(
'providers' => append_config(array(
'Barryvdh\Debugbar\ServiceProvider',
'Way\Generators\GeneratorsServiceProvider',
'Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider',
'Barryvdh\Debugbar\ServiceProvider',
))
);
and on the top of app/start/global.php
$env = $app->detectEnvironment(function(){
$hosts = array(
'localhost' => 'local',
'127.0.0.1' => 'local',
);
if(isset($hosts[$_SERVER['SERVER_NAME']])){
return $hosts[$_SERVER['SERVER_NAME']];
}
});
I tried echoing the $env variable and it returns 'local' so it's working. When i open my site i can't see the debugbar but everything else is working.
Any tip?
Just add your dev providers to app/config/local/app.php and use append_config:
'providers' => append_config(array(
'Barryvdh\Debugbar\ServiceProvider',
))