loading config class in start.php in Laravel - php

In start.php file in laravel4 ... have this code
$env = $app->detectEnvironment(function () {
$environmentList = array(
'staging' => Config::get('program.staging'),
'production' => Config::get('program.production'),
'development' => Config::get('program.development')
);
});
Fatal error: Class 'Config' not found in /var/www/vhosts/engine.domain.com/app/start.php on line 39
I was wondering how to load "Config" class before this ... in start file

What I was trying to do was loading different servers for staging and production for different partners based on url.
the way i was trying to do it created a paradox .. so I did this
$program = (object) include 'config/program.php';
$env = $app->detectEnvironment(function () use ($program){
$environmentList = array(
'staging' => array($program->staging),
'production' => array($program->production),
'development' => array($program->development)
);
});
just don't use the Config:: here ... store the values in a variable instead.

Related

Can't access CakePHP fixture db when triggering test with Travis CI

so I have a project in CakePHP. When pushing my code, Travis CI should run my test. My Test is named ToolTest and its Fixture is ToolFixture.
My .travis.yml looks as following:
language: php
php: 5.3
services:
- mysql
before_script:
- sh -c "mysql -e 'CREATE DATABASE test;'"
- chmod -R 777 project/tmp
- echo "<?php
class DATABASE_CONFIG {
public \$test = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'database' => 'test',
'host' => 'localhost',
'login' => 'travis'
);
}" > project/database.php
script:
sudo project/Console/cake test app Model/Tool --stderr
The error strack trace on travis says:
Fatal error: Uncaught exception 'MissingConnectionException' with message 'Database connection "Mysql" is missing, or could not be created.' in /home/travis/build/project/lib/Cake/Model/Datasource/Database/Mysql.php:194
Error: Database connection "Mysql" is missing, or could not be created.
I already tried '127.0.0.1' instead of localhost, same error messages. When running my test on the VM, the test passes.
What I've noticed:
If I'm not running the script command, travis is successful, so creating the db test and writing the database.php should work fine, right?
My test and fixture are pretty minimalistic.
ToolTest:
public function setUp()
{
parent::setUp();
$this->Tool = ClassRegistry::init('Tool');
}
public function testFindListById()
{
$result = $this->Tool->findListById(2);
$expected = array(
2 => 'Java'
);
$this->assertEquals($expected, $result);
}
ToolFixture:
class ToolFixture extends CakeTestFixture
{
public $useDbConfig = 'test';
public $fields = array(
'id' => 'string',
'name' => 'string'
);
public $records = array(
array(
'id' => 1,
'name' => 'HTML'
),
array(
'id' => 2,
'name' => 'Java'
)
);
}
What am I missing? I've been stuck with this problem for days..Any ideas? Glad for any help!
So I found out that I didn't write my database config in the correct folder..
Instead of
>project/database.php
It should have been
> project/Config/database.php
Jeez..

Laravel detectEnvironment - how to combine machine name and server variable?

In Laravel 4.2, I'm using App::detectEnvironment to change database according to machine name. I also now need to have it change environment according to an environment variable. I've tried to combine but it doesn't work at the moment and I'm not sure how to combine the two techniques.
Using machine name:
$env = $app->detectEnvironment(array(
'local' => array('.local', 'homestead'),
'staging' => array('ip-staging'),
'production' => array('ip-production')
)
);
Using a server environment variable:
$env = $app->detectEnvironment(function()
{
// Default to local if LARAVEL_ENV is not set
return getenv('LARAVEL_ENV') ?: 'local';
}
Non-working combined code looks like:
$env = $app->detectEnvironment(function()
{
// Default to machine name if LARAVEL_ENV is not set
return getenv('LARAVEL_ENV') ?: array(
'local' => array('.local', 'homestead'),
'staging' => array('ip-staging'),
'production' => array('ip-production')
);
});
Found the answer - thanks to #Marwelln's hint.
The $env variable needs to come from the detectEnvironment function for it to be recognised by Laravel.
if (getenv('LARAVEL_ENV'))
{
$env = $app->detectEnvironment(function()
{
return getenv('LARAVEL_ENV');
});
}
else
{
$env = $app->detectEnvironment(array(
// local development environments are set with machine host names
// developers find this out by typing 'hostname' at command line
'local' => array('*.local', 'homestead'),
'staging' => array('ip-staging'),
'production' => array('ip-production')
));
}
This should do it. It sets to environment to what you have in getenv('LARAVEL_ENV') if it's set, otherwise it uses the default $app->detectEnvironment method.
$env = getenv('LARAVEL_ENV') ?: $app->detectEnvironment(array(
'local' => array('.local', 'homestead'),
'staging' => array('ip-staging'),
'production' => array('ip-production')
));

Phpunit grabbing Doctrine2 configuration from unknown place

I am using ZF2 + Doctrine2 + PHPUNIT, when setting up Phpunit, it works fine for a basic test, however as soon as I try to run a test that invokes Doctrine2, I get:
PDOException: SQLSTATE[HY000] [1045] Access denied for user 'username'#'localhost' (using password: YES)
But, I have never specified "username" nor "localhost" nor any sort of password. In-fact, my application runs perfectly fine, and the configuration I have specifies completely different settings.
So where is PHPUnit getting those settings and how to fix it?
My global.php:
'doctrine' => array(
'connection' => array(
'orm_default' => array(
'driverClass' => 'Doctrine\DBAL\Driver\PDOMySql\Driver',
'params' => array(
'host' => '127.0.0.1',
'port' => '3306',
'user' => 'sbapp',
'password' => 'myp#ss',
'dbname' => 'root'
)
)
),
The Test:
class ApplicationControllerTest extends AbstractHttpControllerTestCase
{
protected $traceError = true;
public function setUp()
{
$this->setApplicationConfig(
include '../../../config/application.config.php'
);
parent::setUp();
}
public function testAuthActionCanBeAccessed()
{
$postData = new \stdClass();
$postData->username = "someAppUser";
$postData->password = "12345";
$postData = json_decode(json_encode($postData),true);
$this->dispatch('/auth', 'POST', $postData);
$response = $this->getResponse();
$this->assertResponseStatusCode(200);
$this->assertActionName('auth');
}
}
The relative paths used within the config on setUp when done incorrectly can seriously affect the loading of entities and so on. So I was chasing around the right path.. when calling phpunit from root..or from vendor..or from within the test folder, etc..
Solution:
Call it from project root, and leave the routes exactly as the main project are.

How do I turn on the PHP error reporting in Zend Framework 2?

Everytime I receive an error in Zend Framework 2, I get just 500 Internal Server Error displayed and have to search through the Zend Server error log.
I've tried putting this to my config/autoload/local.php file but it doesn't work:
return array(
'phpSettings' => array(
'display_startup_errors' => true,
'display_errors' => true,
),
);
There is no native support for that in zf2 (afaik). You'd either have to set them in php.ini itself, or set them in index.php
<?php
error_reporting(E_ALL);
ini_set('display_errors', true);
If you really want to be able to supply them as config settings, you could keep what you have and do that in a module bootstrap, get them from config, and call ini_set() on each key value pair
public function onBootstrap(EventInterface $e) {
$app = $e->getApplication();
$sm = $app->getServiceManager();
$config = $sm->get('Config');
$phpSettings = isset($config['phpSettings']) ? $config['phpSettings'] : array();
if(!empty($phpSettings)) {
foreach($phpSettings as $key => $value) {
ini_set($key, $value);
}
}
}
Edit: as #akond rightly points out in the comments, you could just add the ini_set lines to local.php which is a better solution.
To easilly configure phpSettings on your ZF2 app, you should consider using DluPhpSettings.
With this module, you can configure your settings for each environment you have:
/* Local application configuration in /config/autoload/phpsettings.local.php */
<?php
return array(
'phpSettings' => array(
'display_startup_errors' => false,
'display_errors' => false,
'max_execution_time' => 60,
'date.timezone' => 'Europe/Prague',
'mbstring.internal_encoding' => 'UTF-8',
),
);
Look this blog post for more info too!

Fatal error: Class 'Memcache' not found in Zend Framework + Wamp

I have following code :
in application.ini
cache.default.adapter = "memcached"
cache.default.params.host = "localhost"
cache.default.params.port = "11211"
in Bootstrap.php
$cache = new Memcache();
$cache->connect($cache_params['host'], $cache_params['port']);
Zend_Registry::set("cache", $cache);
and also I am having memcache installed on my machine by putting php_memcache.dll in wamp\bin\php\php5.3.9\ext and also extension=php_memcache.dll in php.ini
But still I am getting the following error :
( ! ) Fatal error: Class 'Memcache' not found in \wamp\www\projectname\application\Bootstrap.php on line 160
I have gone through google but still not able to solved the problem. What is the problem why it's not connecting to the memcache.
You are trying to cache your database?
You want to use Zend_Cache.
(From: http://zendcoding.com/how-to-use-memcached-in-the-zend-framework)
$frontendOpts = array(
'caching' => true,
'lifetime' => 1800, //how long in seconds to keep the cache for.
'automatic_serialization' => true //In order to store objects and arrays, Zend must first serialize data into a string. If this parameter is set to ‘true‘, this serialization will happen on the fly and behind the scenes.
);
$backendOpts = array(
'servers' =>array(
array(
'host' => $cache_params['host'],
'port' => $cache_params['port'],
'weight' => 1
)
),
'compression' => false
);
$cache = Zend_Cache::factory('Core', 'Memcached', $frontendOpts, $backendOpts);
This link also demonstrates how to load and update the cache, and how to make it accessible from everywhere in your application. A good read, to be sure.
Your setting is memcached
cache.default.adapter = "memcached"
but you want to use memcache
$cache = new Memcache();
Try this example
<?php
$mc = new Memcached('mc');
$mc->setOption(Memcached::OPT_LIBKETAMA_COMPATIBLE, true);
if (!count($mc->getServerList())) {
$mc->addServers(array(
array('127.0.0.1',11211),
array('127.0.0.1',11211),
));
}
$key = 'mykey';
$mc->add($key,'test for memcached not memcache');
$val = $mc->get($key);
echo $val;
?>

Categories